python - python kivy garden mapview经纬度不更新
问题描述
我正在尝试使用来自 kivy garden 的 MapView 小部件,但我的标记位置没有更新。我在文件中添加了MapView
一个。这些具有属性和。我尝试为它们分配变量并使用and形成变量。MapMarker
main.kv
lat
lon
latitude
longitude
main.py
app.latitude
app.longitude
在我的 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 文件的标题。
#: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
,and lon
ofMapView
是只读的,所以赋值:
MapView:
id: map_view
zoom: 17
lat: app.latitude # <---
lon: app.longitude # <---
设置起始值但您不能更新它,要更新您必须使用的 MapView 的中心center_on()
。
主文件
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()
主文件
#: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)
推荐阅读
- android - 如何设置工具栏内的约束布局宽度以匹配父级?
- java - 无法为 unitName 检索 EntityManagerFactory
- amazon-ecs - 在 ECS Fargate 中从 Dockerhub 拉取公共镜像
- java - 带有 Spring Boot 和 Data JPA 的 RESTful Web 服务始终公开活动的 oracle 表
- php - 如何使下拉菜单反映 PHP 和 HTML 中的表单动作?
- python - 每组另一个常见的数据框中查找最常见的单元格
- kotlin - 在 ArrayList 中组合元素 [Kotlin]
- javascript - 通过 QiMessaging JavaScript 连接到虚拟机器人
- c# - 在 httplisteners authenticationselectordelegate 中给出拒绝身份验证消息
- arrays - 如何将第二个viewcontroller文本字段值传递给第一个viewcontroller数组并在swift4中点击按钮时关闭