Python kivy garden mapview经纬度不更新

问题描述 投票:0回答:1

我正在尝试使用来自 kivy garden 的 MapView 小部件,但我的标记位置没有更新。我在

MapView
文件中添加了一个带有
MapMarker
main.kv
。它们具有
lat
lon
的属性。我试着给他们分配变量
latitude
longitude
通过使用
main.py
app.latitude
.

在我的 main.py 中,我在

app.longitude

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


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

on_start

这里是 main.py 的部分

# 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

更新:

添加我的 kv 文件的标题。 # 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)


python kivy kivy-language
1个回答
2
投票

#: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
另一方面,

NumericProperty

lat
lon
read-only
所以作业: MapView

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

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

.

main.py

center_on()

main.kv

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



更新:

map_view 是一个非常深的层次结构的根孩子,所以要以简单的方式访问它,一个属性被创建:

#: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
访问:

*.kv

self.root.map_view

*.py

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

© www.soinside.com 2019 - 2024. All rights reserved.