How to make a GPS Tracking App with Kivy and using it on android

37 views
Skip to first unread message

Marco Lange

unread,
Jun 6, 2024, 5:07:28 AMJun 6
to Kivy users support
I'm not sure what the issue is, but the app is active on my phone, however it won't display my location. Any ideas?

Source Code, only one class:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.utils import platform
from jnius import autoclass, PythonJavaClass, java_method
import traceback

# Prüfe, ob die App auf Android läuft
if platform == 'android':
    # Importiere benötigte Klassen aus der Android-API
    PythonActivity = autoclass('org.kivy.android.PythonActivity')
    LocationManager = autoclass('android.location.LocationManager')
    Criteria = autoclass('android.location.Criteria')

class LocationApp(App):
    def build(self):
        layout = BoxLayout(orientation='vertical')

        self.label = Label(text="Standort wird ermittelt...")
        layout.add_widget(self.label)

        # Füge einen Button hinzu, um den Standort manuell abzurufen
        self.button = Button(text="Standort abrufen", on_press=self.get_location)
        layout.add_widget(self.button)

        return layout

    def get_location(self, instance):
        try:
            # Erstelle einen LocationManager
            self.location_manager = PythonActivity.mActivity.getSystemService(PythonActivity.LOCATION_SERVICE)

            # Erstelle ein Criteria-Objekt, um den besten Anbieter zu wählen
            criteria = Criteria()
            best_provider = self.location_manager.getBestProvider(criteria, True)

            # Fordere die Standortaktualisierungen an
            self.location_listener = MyLocationListener(self.label)
            self.location_manager.requestLocationUpdates(best_provider, 0, 0, self.location_listener)
        except Exception as e:
            traceback.print_exc()

class MyLocationListener(PythonJavaClass):
    __javainterfaces__ = ['android/location/LocationListener']

    def __init__(self, label):
        super().__init__()
        self.label = label

    # Implementiere die Methode onLocationChanged
    @java_method(' (Landroid/location/Location;)V')
    def onLocationChanged(self, location):
        try:
            # Aktualisiere die Label-Anzeige mit den aktuellen Koordinaten
            self.label.text = f"Breitengrad: {location.getLatitude()}\nLängengrad: {location.getLongitude()}"
        except Exception as e:
            traceback.print_exc()

    # Weitere Methoden wie onStatusChanged, onProviderEnabled, onProviderDisabled können ebenfalls implementiert werden, je nach Bedarf

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


Screenshot_20240605_205729_LocationApp[1].jpg

Tomek CEDRO

unread,
Jun 6, 2024, 5:57:06 AMJun 6
to kivy-...@googlegroups.com
Can you make sure that application has been granted location
permission (Settings / Applications)?

--
CeDeROM, SQ7MHZ, http://www.tomek.cedro.info

Marco Lange

unread,
Jun 6, 2024, 8:22:14 AMJun 6
to kivy-...@googlegroups.com
Yes, when i start the app for the first time i will asked to give permission for location.

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/c2LEoyT3IcY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAFYkXj%3D%3Dz7ctXYLftP9yekha7RdNcm3cQBRv3ygA65joP-7JfA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages