在点击地图上放置标记时,会将标记设置在地图窗口的一个角落。

huangapple go评论67阅读模式
英文:

Placing a mapmarker by klick results in setting the marker on acorner of the map-window

问题

我想通过在地图上点击来设置标记。当我点击时,会设置一个标记。然而,标记被放置在地图的左下角。我做错了什么?

marker = MapMarker(lat=lat, lon=lon, source='custom_marker.png')  # 添加这一行以指定标记图像
英文:

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.

答案1

得分: 0

你混淆了on_map_touch_down()方法的参数。为了纠正这个问题,请将:

on_touch_down: root.on_map_touch_down(self, *args)

改为:

on_touch_down: root.on_map_touch_down(*args)

self 参数不需要,因为它已经自动包含在 args 中。

然后,方法本身可以定义为:

def on_map_touch_down(self, mapview, touch):

英文:

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):

huangapple
  • 本文由 发表于 2023年5月17日 17:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定