I try to get android gps info (longitude and latitude) using Kivy and plyer but it doesnt work. My code is a main.py file:
from kivy.lang import Builder
from plyer import gps
from kivy.app import App
from kivy.properties import StringProperty
from kivy.clock import mainthread
from kivy.utils import platform
class GpsTest(App):
latitude = StringProperty("Unknown")
longitude = StringProperty("Unknown")
gps_status = StringProperty('Click Start to get GPS location updates')
def request_android_permissions(self):
"""
Since API 23, Android requires permission to be requested at runtime.
This function requests permission and handles the response via a
callback.
The request will produce a popup if permissions have not already been
been granted, otherwise it will do nothing.
"""
from android.permissions import request_permissions, Permission
def callback(permissions, results):
"""
Defines the callback to be fired when runtime permission
has been granted or denied. This is not strictly required,
but added for the sake of completeness.
"""
if all([res for res in results]):
print("callback. All permissions granted.")
else:
print("callback. Some permissions refused.")
request_permissions([Permission.ACCESS_COARSE_LOCATION,
Permission.ACCESS_FINE_LOCATION], callback)
# # To request permissions without a callback, do:
# request_permissions([Permission.ACCESS_COARSE_LOCATION,
# Permission.ACCESS_FINE_LOCATION])
def build(self):
try:
gps.configure(on_location=self.on_location,
on_status=self.on_status)
except NotImplementedError:
import traceback
traceback.print_exc()
self.gps_status = 'GPS is not implemented for your platform'
if platform == "android":
print("gps.py: Android detected. Requesting permissions")
self.request_android_permissions()
return Builder.load_string(kv)
def start(self, minTime, minDistance):
gps.start(minTime, minDistance)
def stop(self):
gps.stop()
@mainthread
def on_location(self, **kwargs):
self.latitude = str(kwargs.get('lat', 'Unknown'))
self.longitude = str(kwargs.get('lon', 'Unknown'))
@mainthread
def on_status(self, stype, status):
self.gps_status = 'type={}n{}'.format(stype, status)
def on_pause(self):
gps.stop()
return True
def on_resume(self):
gps.start(1000, 0)
pass
if __name__ == '__main__':
GpsTest().run()
and the buildozer.spce file which is set as follows:
[app]
# (list) Permissions
android.permissions = INTERNET,ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION
[buildozer]
log_level = 2
However it doesnot provide the gps coordinations. The app runs but it doesnt provide gps coordinations such as longitude and latitude.