By using kivy and plyer GPS an app to fetch current location and store in firebase database can be made using below code.
Code For GPS App using kivy in Python:
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.lang import Builder
import requests,json
from kivy.properties import StringProperty
from plyer import gps
from kivy.utils import platform
from kivy.clock import mainthread
from kivymd.uix.floatlayout import MDFloatLayout
from datetime import date
KV='''
MDScreenManager:
MDScreen:
name:"screen A"
md_bg_color: "lightblue"
MDLabel:
id: welcome_label
text: "test"
font_size: 50
halign:"center"
pos_hint: {"center_x": .5,"center_y": .9}
MDTextField:
id: user
hint_text: "username"
icon_right: "account"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": .5,"center_y": .7}
MDTextField:
id: password
hint_text: "password"
icon_right: "eye-off"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": .5,"center_y": .5}
password: True
MDLabel:
id: wel
text: "Enter Details"
font_size: 20
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
padding_y: 10
MDRaisedButton:
text: "Login"
pos_hint: {"center_x": .5,"center_y": .3}
y: "36dp"
on_press:app.logger()
on_release:
if app.st_u=="Success":\
root.current="screenB"
else:\
wel.text==app.st_u
MDScreen:
name:"screenB"
md_bg_color: "cadetblue"
MDRaisedButton:
text: "Logout"
on_press:
root.current = "screen A"
pos_hint:{"center_x": .9,"center_y": .9}
MDLabel:
text: app.gps_location
halign:"center"
MDLabel:
text: app.gps_status
halign:"center"
MDRaisedButton:
text: "Start GPS"
on_press:
app.start(1000,0)
pos_hint: {"center_x": .5,"center_y": .1}
MDLabel:
id:new
text: "No data"
halign:"center"
pos_hint: {"center_x": .5,"center_y": .9}
MDRaisedButton:
id:ld
text:'Loading'
on_press:app.store_ld()
on_release:root.ids.new.text='Current Location Captured'
pos_hint:{"center_x": .5,"center_y": .8}
MDRaisedButton:
id:unl
text:'Unloading'
on_press:app.store_unl()
on_release:root.ids.new.text='Current Location Captured'
pos_hint:{"center_x": .5,"center_y": .7}
'''
class Example(MDApp):
st_u=StringProperty()
f_url="firebaseurl.json"
gps_location = StringProperty()
gps_status = StringProperty('Click Start to get GPS location updates')
m_lat=ObjectProperty()
m_lon=ObjectProperty()
def request_android_permissions(self):
from android.permissions import request_permissions, Permission
def callback(permissions, results):
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)
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.m_lat=kwargs['lat']
self.m_lon=kwargs['lon']
@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
def store_ld(self):
a='https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat={}&lon={}'.format(self.m_lat,self.m_lon)
d1=date.today()
c_date=d1.strftime("%d/%m/%y")
r=requests.get(a)
d=json.loads(r.content)
det={self.root.ids.user.text+'-'+self.root.ids.ld.text:[c_date,self.m_lat,self.m_lon,d['address']]}
x=json.dumps(det)
requests.post(url=self.f_url,json=x)
self.gps_location = '\n'.join([
'{}={}'.format(k, v) for k, v in d['address'].items()])
def store_unl(self):
a='https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat={}&lon={}'.format(self.m_lat,self.m_lon)
d1=date.today()
c_date=d1.strftime("%d/%m/%y")
r=requests.get(a)
d=json.loads(r.content)
det={self.root.ids.user.text+'-'+self.root.ids.unl.text:[c_date,self.m_lat,self.m_lon,d['address']]}
x=json.dumps(det)
requests.post(url=self.f_url,json=x)
self.gps_location = '\n'.join([
'{}={}'.format(k, v) for k, v in d['address'].items()])
def logger(self):
r=requests.get('firebaseurl.json')
d=json.loads(r.content.decode('utf-8'))
e=json.loads(d)
if self.root.ids.user.text in e['uname'] and self.root.ids.password.text==e['psw'][e['uname'].index(self.root.ids.user.text)] :
self.st_u="Success"
else:
self.root.ids.wel.text="Incorrect user name and password"
Example().run()
Thus By writing above code and building an apk file using buildozer.we can make GPS app using kivy in python.