Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from kivy.lang import Builder
- from plyer import gps
- from kivy.app import App
- from kivy.properties import StringProperty
- from kivy.clock import Clock, mainthread
- kv = '''
- BoxLayout:
- orientation: 'vertical'
- Label:
- text: app.gps_location
- Label:
- text: app.gps_status
- BoxLayout:
- size_hint_y: None
- height: '48dp'
- padding: '4dp'
- ToggleButton:
- text: 'Start' if self.state == 'normal' else 'Stop'
- on_state:
- app.gps.start() if self.state == 'down' else app.gps.stop()
- '''
- class GpsTest(App):
- gps_location = StringProperty()
- gps_status = StringProperty('Click Start to get GPS location updates')
- gps_conn = False
- def build(self):
- self.gps = gps
- try:
- self.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'
- return Builder.load_string(kv)
- def on_pause(self):
- '''
- called on appication pause
- '''
- if self.gps_conn:
- self.gps.stop()
- return True
- def on_resume(self):
- '''
- called on appication resume
- '''
- if self.gps_conn :
- self.gps.start()
- def gpsstart(self):
- if not self.gps_conn :
- self.gps.start()
- # self.gps_conn = True
- def gpsstop(self):
- if self.gps_conn :
- self.gps.stop()
- self.gps_conn = False
- @mainthread
- def on_location(self, **kwargs):
- self.gps_location = '\n'.join([
- '{}={}'.format(k, v) for k, v in kwargs.items()])
- @mainthread
- def on_status(self, stype, status):
- self.gps_status = 'type={}\n{}'.format(stype, status)
- if __name__ == '__main__':
- GpsTest().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement