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