I want to set markers on the map by clicking on it. When I click a marker is set. However, the marker is placed on the left lower corner of the map. What am I doing wrong?
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy_garden.mapview import MapMarker
Builder.load_string(
'''
#:import MapView kivy_garden.mapview.MapView
#:import MapMarker kivy_garden.mapview.MapMarker
<SimpleRec>
name: "simple_rec"
MDBoxLayout:
MapView:
id: smap
lat: 48
lon: 7.82
zoom: 11
on_touch_down: root.on_map_touch_down(self, *args)
'''
)
class SimpleRec(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def on_map_touch_down(self, touch, *args):
lat, lon = self.ids.smap.get_latlon_at(*touch.pos)
print(str(lat) + " " + str(lon))
marker = MapMarker(lat=lat, lon=lon)
self.ids.smap.add_marker(marker)
class MainApp(MDApp):
def build(self):
screen_manager = ScreenManager()
simple_rec = SimpleRec(name='simple_rec')
screen_manager.add_widget(simple_rec)
return screen_manager
if __name__ == '__main__':
MainApp().run()
Surprisingly, when I extracted the necessary snippets out of my code the position where the markers are set changed from upper left to lower left corner.
You are confusing the arguments to your on_map_touch_down()
method. To correct that problem, change:
on_touch_down: root.on_map_touch_down(self, *args)
to:
on_touch_down: root.on_map_touch_down(*args)
The self
arg is not needed as it is automatically included in the args
.
Then the method itself can be defined as:
def on_map_touch_down(self, mapview, touch):