Search code examples
pythonkivygps

Cannot update the marker on Kivy-Mapview Python


Hello guys. Hope you are all doing well.

I have just started using Kivy Mapview on Python and done some examples. Now, I am trying to create a dynamic marker which is able to move/reposition itself in every 0.5 seconds. I am conducting a project, and at the end of it I need to provide a real-time GPS tracking. That is the reason why I am trying to create a dynamic marker. Here is what I have tried:

.py file:

from kivy.garden.mapview import MapView, MapMarker
from kivy.app import App
import time

class MapViewApp(App):
    def build(self):
        while True:
            increment = 0.0001
            mapview = MapView(zoom=19, lat=40.219867, lon=28.964641)
        
            marker_1 = MapMarker(lon=28.964641 + increment, lat=40.219867 + increment, source="circle_PNG36.png")
            mapview.add_marker(marker_1)
            increment = increment + 0.01
            mapview.remove_marker(marker1)
            time.sleep(0.5)
        
MapViewApp().run()

.kv file:

#:import MapView kivy.garden.mapview.MapView

MapView:
    MapMarkerPopup:
    lat: self.lat
    lon: self.lon

Solution

  • Several problems with your code, but the main issue is that your MapViewApp will never run, because there is an infinite loop in your build() method. Since the build() method never returns, the runTouchApp() method never gets executed.

    On a related note, you also do not want to put that infinite loop on the main thread of a kivy App. The updating of an App GUI takes place on the main thread and if some method is hogging the main thread, the GUI will be frozen. Such a loop should be run on a separate thread, with any modifications that it makes to the GUI accomplished with something like Clock.schedule_once() to get those GUI modifications back on the main thread.