Python kivy garden mapview经纬度不更新

Python kivy garden mapview longitude and latitude do not update

我正在尝试使用来自 kivy garden 的 MapView 小部件,但我的标记位置没有更新。我在 main.kv 文件中添加了 MapViewMapMarker。它们具有属性 latlon。我试图通过使用 app.latitudeapp.longitude.

为它们分配变量 latitudelongitude 形成 main.py 变量

在我的 main.py 中,我在 on_start 方法中使用更新函数。 update 函数只是调用两个辅助函数来获取经度和纬度坐标(目前只是随机值)。

问题是当我 运行 代码时,我的地图视图和标记没有更新。我做错了什么?

# section of main.kv
MapView:
    id: map_view
    zoom: 17
    lat: app.latitude
    lon: app.longitude
    MapMarker:
        id: map_view_marker
        lat: app.latitude
        lon: app.longitude

这是 main.py

的部分
# main.py
… 
class MainApp(App):
…
    # map parameters
    latitude = 50
    longitude = 3

    # Getting latitude and longitude (at the moment just random stuff
    def get_gps_latitude(self):
        self.latitude = self.decimal_precision(0.01 * random.random() + 50.6394, DECIMAL_PRECISION)
        return self.latitude # rounding

    def get_gps_longitude(self):
        self.longitude = self.decimal_precision(0.01 * random.random() + 50.6394, DECIMAL_PRECISION)
        return self.longitude

    def update(self, _):
        self.latitude = self.get_gps_latitude()
        self.longitude = self.get_gps_longitude()

    def on_start(self):
        Clock.schedule_interval(self.update, 1)

更新: 添加我的 kv 文件的 header。

#:kivy 1.10.1
#:import Toolbar kivymd.toolbar.Toolbar
#:import MDNavigationDrawer kivymd.navigationdrawer.MDNavigationDrawer
#:import NavigationLayout kivymd.navigationdrawer.NavigationLayout
#:import NavigationDrawerDivider kivymd.navigationdrawer.NavigationDrawerDivider
#:import NavigationDrawerToolbar kivymd.navigationdrawer.NavigationDrawerToolbar
#:import NavigationDrawerSubheader kivymd.navigationdrawer.NavigationDrawerSubheader
#:import MDTextField kivymd.textfields.MDTextField
#:import MDSwitch kivymd.selectioncontrols.MDSwitch
#:import labels application.labels
#:import MapView kivy.garden.mapview
#:import MapMarkerPopup kivy.garden.mapview

NavigationLayout:
    id: nav_layout
    MDNavigationDrawer:
        id: nav_drawer
        NavigationDrawerToolbar:
            title: labels.NAVIGATION
        NavigationDrawerIconButton:
            icon: 'checkbox-blank-circle'
            text: labels.OPERATING_MODE
            on_release: app.root.ids.scr_mngr.current = 'operating_mode'
    BoxLayout:
        orientation: 'vertical'
        halign: "center"
        Toolbar:
            id: toolbar
            title: labels.APPLICATION_NAME
            md_bg_color: app.theme_cls.primary_color
            background_palette: 'Primary'
            background_hue: '500'
            left_action_items: [['menu', lambda x: app.root.toggle_nav_drawer()]]
            #right_action_items: [['dots-vertical', lambda x: app.root.toggle_nav_drawer()]]
        ScreenManager:
            id: scr_mngr
            Screen:
                name: 'operating_mode'
                BoxLayout:
                    orientation: "vertical"
                    padding: dp(48)
                    width: dp(100)
                    spacing: 24
                    BoxLayout:
                        orientation: "horizontal"
                        spacing: 24
                        BoxLayout:
                            orientation: "horizontal"
                            MapView:
                                id: map_view
                                zoom: 10
                                lat: app.latitude
                                lon: app.longitude
                                MapMarkerPopup:
                                    id: map_view_marker
                                    lat: app.latitude
                                    lon: app.longitude

只能在使用Properties, in your case latitude and longitude are not Properties, so they do not generate the change. In this case you must use NumericProperty时进行绑定:

另一方面lat and lon of MapView只读所以赋值:

MapView:
    id: map_view
    zoom: 17
    lat: app.latitude  # <---
    lon: app.longitude # <---

设置起始值但不能更新它,要更新 MapView 的中心必须使用 center_on()

main.py

from kivy.app import App
from kivy.clock import Clock
from kivy.properties import NumericProperty
import random

DECIMAL_PRECISION = 2

class MainApp(App):
    # map parameters
    latitude = NumericProperty(50)
    longitude = NumericProperty(3)

    def decimal_precision(self, val, precision):
        # foo process
        return val

    # Getting latitude and longitude (at the moment just random stuff
    def get_gps_latitude(self):
        self.latitude = self.decimal_precision(0.01 * random.random() + 50.6394, DECIMAL_PRECISION)
        return self.latitude # rounding

    def get_gps_longitude(self):
        self.longitude = self.decimal_precision(0.01 * random.random() + 50.6394, DECIMAL_PRECISION)
        return self.longitude

    def update(self, _):
        self.latitude = self.get_gps_latitude()
        self.longitude = self.get_gps_longitude()
        self.root.center_on(self.latitude, self.longitude)

    def on_start(self):
        Clock.schedule_interval(self.update, 1)

if __name__ == '__main__':
    MainApp().run()

main.kv

#:import MapView kivy.garden.mapview.MapView

# section of main.kv
MapView:
    id: map_view
    zoom: 17
    lat: app.latitude
    lon: app.longitude
    MapMarker:
        id: map_view_marker
        lat: app.latitude
        lon: app.longitude

更新:

map_view 是一个非常深的层次结构的根子节点,因此要以简单的方式访问它,会创建一个 属性:map_view: map_view,然后我们通过 self.root.map_view:

*.kv

NavigationLayout:
    id: nav_layout
    map_view: map_view # <---

*.py

def update(self, _):
    self.latitude = self.get_gps_latitude()
    self.longitude = self.get_gps_longitude()
    self.root.map_view.center_on(self.latitude, self.longitude)