from kivy_garden.mapview import MapView, MapMarker
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.button import MDRectangleFlatButton
from kivymd.uix.screen import Screen
from helpers import nav_input
from one_map_api import get_lat_lng2
class MapViewApp(MDApp):
def build(self):
self.screen = Screen()
self.theme_cls.primary_palette = "Red"
self.button = MDRectangleFlatButton(text='Go',pos_hint= {'center_x':0.45,'center_y':0.95}, on_release =self.change_coord)
self.navBar = Builder.load_string(nav_input)
mapping = MapView(zoom=11, lat=1.3521, lon=103.8198)
marker_main = MapMarker(lat=35, lon=100, source='marker.png')
mapping.add_marker(marker_main)
self.screen.add_widget(mapping)
self.screen.add_widget(self.navBar)
self.screen.add_widget(self.button)
return self.screen
def change_coord(self,obj):
#changes the coordinates of the MapView by overlaying the screen with a new screen
try:
coord_result = get_lat_lng2(self.navBar.text)
mapping = MapView(zoom=17, lat=coord_result[0], lon=coord_result[1])
marker_main = MapMarker(lat=coord_result[0], lon=coord_result[1], source='marker.png')
mapping.add_marker(marker_main)
self.button = MDRectangleFlatButton(text='Go', pos_hint={'center_x': 0.45, 'center_y': 0.95},on_release=self.change_coord)
self.navBar = Builder.load_string(nav_input)
self.screen.add_widget(mapping)
self.screen.add_widget(self.navBar)
self.screen.add_widget(self.button)
except:
pass
if __name__ == '__main__':
MapViewApp().run()
如您所见,地图可以运行,但是当用户单击按钮时,它会创建更多图层。无论如何只覆盖 MapView 模块中的坐标并显示新位置?