-
-
Save LCPallares/41c4d59930eab5c8fd3610f93aebb1ff to your computer and use it in GitHub Desktop.
Probando el gps con mapview en kivy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
from kivy.lang import Builder | |
from plyer import gps | |
from kivy.app import App | |
from kivy.properties import StringProperty, NumericProperty, ObjectProperty, ListProperty | |
from kivy.clock import Clock, mainthread | |
import kivy.garden.mapview | |
from kivy.garden.mapview import MapView, MapMarker | |
import threading | |
import time | |
kv = ''' | |
#:import hex kivy.utils.get_color_from_hex | |
#:import sys sys | |
#:import MapSource mapview.MapSource | |
<Mapa@BoxLayout>: | |
orientation: 'vertical' | |
MapView: | |
lat: app.app_lat | |
lon: app.app_long | |
zoom: 13 | |
map_source: MapSource(sys.argv[1], attribution="") if len(sys.argv) > 1 else "osm" | |
MapMarkerPopup: | |
lat: app.app_lat | |
lon: app.app_long | |
# lat: app.on_location(self.lat) | |
# lon: app.on_location(self.long) | |
popup_size: dp(230), dp(130) | |
Bubble: | |
BoxLayout: | |
orientation: "horizontal" | |
padding: "5dp" | |
Label: | |
text: "[b]Usted está aqui![/b]" | |
markup: True | |
halign: "center" | |
BoxLayout: | |
orientation: 'vertical' | |
Mapa: | |
size_hint: 1, 1 | |
Label: | |
text: f'lat = {app.app_lat} long = {app.app_long}' | |
#text: f'lat = {app.gps_localizacion2[0]} long = {app.gps_localizacion2[1]}' | |
#text: f'lat = {str(app.gps_localizacion["lat"])} long = {str(app.gps_localizacion["long"])}' | |
color: hex("#4E5862") | |
size_hint: 1, .15 | |
Label: | |
text: app.gps_localizacion | |
size_hint: 1, .3 | |
canvas.before: | |
Color: | |
rgb: hex("#1b2936") | |
Rectangle: | |
size: self.size | |
pos: self.pos | |
Label: | |
text: app.gps_estado | |
color: hex("#4E5862") | |
size_hint: 1, .25 | |
BoxLayout: | |
size_hint_y: None | |
height: '48dp' | |
padding: '4dp' | |
ToggleButton: | |
text: 'Iniciar' if self.state == 'normal' else 'Parar' | |
on_state: | |
app.start(1000, 0) if self.state == 'down' else \ | |
app.stop() | |
''' | |
class GpsTest(App): | |
gps_localizacion = StringProperty() | |
gps_estado = StringProperty('Preciona iniciar para obtener\nactualizaciones de tu localizacion GPS') | |
#gps_localizacion2 = ListProperty() | |
app_lat = 7.059639 | |
app_long = -73.846174 | |
#app_lat = StringProperty('7.059639') | |
#app_long = StringProperty('-73.846174') | |
#latitud = StringProperty() | |
#longitud = StringProperty() | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
#threading.Thread(target=self.actualizar).start() | |
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_estado = 'el GPS no esta implementado en su plataforma\no tiene el GPS apagado' | |
#Clock.schedule_interval(self.actualizar, 1) | |
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.lat = kwargs.get('lat') | |
self.long = kwargs.get('lon') | |
#self.lat = str(kwargs.get('lat')) | |
#self.long = str(kwargs.get('lon')) | |
self.app_lat = self.lat | |
self.app_long = self.long | |
print(self.app_lat, self.app_long) | |
#self.gps_localizacion2 = [kwargs.get('lat'), kwargs.get('lon')] | |
self.gps_localizacion = '\n'.join([ | |
f'{k}={v}' for k, v in kwargs.items()]) | |
@mainthread | |
def on_status(self, stype, status): | |
self.gps_estado = f'type={stype}\n{status}' | |
''' | |
def actualizar(self, **kwargs): | |
self.on_location(kwargs) | |
''' | |
def on_pause(self): | |
gps.stop() | |
return True | |
def on_resume(self): | |
gps.start(1000, 0) | |
pass | |
if __name__ == '__main__': | |
GpsTest().run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment