Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. from kivy.lang import Builder
  2. from plyer import gps
  3. from kivy.app import App
  4. from kivy.properties import StringProperty
  5. from kivy.clock import Clock, mainthread
  6.  
  7. kv = '''
  8. BoxLayout:
  9. orientation: 'vertical'
  10.  
  11. Label:
  12. text: app.gps_location
  13.  
  14. Label:
  15. text: app.gps_status
  16.  
  17. BoxLayout:
  18. size_hint_y: None
  19. height: '48dp'
  20. padding: '4dp'
  21.  
  22. ToggleButton:
  23. text: 'Start' if self.state == 'normal' else 'Stop'
  24. on_state:
  25. app.gps.start() if self.state == 'down' else app.gps.stop()
  26. '''
  27.  
  28.  
  29. class GpsTest(App):
  30.  
  31. gps_location = StringProperty()
  32. gps_status = StringProperty('Click Start to get GPS location updates')
  33. gps_conn = False
  34.  
  35. def build(self):
  36. self.gps = gps
  37. try:
  38. self.gps.configure(on_location=self.on_location,
  39. on_status=self.on_status)
  40. except NotImplementedError:
  41. import traceback
  42. traceback.print_exc()
  43. self.gps_status = 'GPS is not implemented for your platform'
  44.  
  45. return Builder.load_string(kv)
  46.  
  47. def on_pause(self):
  48. '''
  49. called on appication pause
  50. '''
  51. if self.gps_conn:
  52. self.gps.stop()
  53.  
  54. return True
  55.  
  56. def on_resume(self):
  57. '''
  58. called on appication resume
  59. '''
  60. if self.gps_conn :
  61. self.gps.start()
  62.  
  63.  
  64. def gpsstart(self):
  65. if not self.gps_conn :
  66. self.gps.start()
  67. # self.gps_conn = True
  68.  
  69. def gpsstop(self):
  70. if self.gps_conn :
  71. self.gps.stop()
  72. self.gps_conn = False
  73.  
  74. @mainthread
  75. def on_location(self, **kwargs):
  76. self.gps_location = '\n'.join([
  77. '{}={}'.format(k, v) for k, v in kwargs.items()])
  78.  
  79. @mainthread
  80. def on_status(self, stype, status):
  81. self.gps_status = 'type={}\n{}'.format(stype, status)
  82.  
  83. if __name__ == '__main__':
  84. GpsTest().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement