Advertisement
Guest User

mapview

a guest
Mar 30th, 2017
2,342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.35 KB | None | 0 0
  1. #-*- coding: utf-8 -*-
  2. from kivy.lang import Builder
  3. from plyer import gps
  4. from kivy.app import App
  5. from kivy.properties import StringProperty
  6. from kivy.clock import Clock, mainthread
  7. from kivy.uix.popup import Popup
  8. from kivy.network.urlrequest import UrlRequest
  9. from kivy.uix.label import Label
  10. import json
  11. import kivy.garden.mapview
  12.  
  13. kv = '''
  14. #:import C kivy.utils.get_color_from_hex
  15. #:import sys sys
  16. #:import MapSource mapview.MapSource
  17.  
  18. BoxLayout:
  19.    canvas:
  20.        Color:
  21.            rgb: C('#ffffff')
  22.        Rectangle:
  23.            pos: self.pos
  24.            size: self.size
  25.    orientation: 'vertical'
  26.    BoxLayout:
  27.        orientation: 'vertical'
  28.        MapView:
  29.            lat: app.app_lat
  30.            lon: app.app_long
  31.            zoom: 13
  32.            map_source: MapSource(sys.argv[1], attribution="") if len(sys.argv) > 1 else "osm"
  33.            MapMarkerPopup:
  34.                lat: app.app_lat
  35.                lon: app.app_long
  36.                # lat: app.on_location(self.lat)
  37.                # lon: app.on_location(self.long)
  38.                popup_size: dp(230), dp(130)
  39.                Bubble:
  40.                    BoxLayout:
  41.                        orientation: "horizontal"
  42.                        padding: "5dp"
  43.                        Label:
  44.                            text: "[b]Você está aqui![/b]"
  45.                            markup: True
  46.                            halign: "center"
  47.    BoxLayout:
  48.        assunto: assunto
  49.        orientation: 'vertical'
  50.        padding: [30, 30, 30, 100]
  51.        spacing: 10
  52.        Spinner:
  53.            id: assunto
  54.            text: 'Selecionar Assunto'
  55.            background_color: C('#1180c4')
  56.            background_normal: ''
  57.            values: ('Buraco e Pavimentação', 'Calçadas guias e Postes', 'Ciclovias, Faixas e outros', 'Semáforos, sinalização e outros.', 'Iluminação Pública')
  58.            size_hint_y: None
  59.            height: '40dp'
  60.  
  61.        BoxLayout:
  62.            orientation: 'horizontal'
  63.            height: '40dp'
  64.            size_hint_y: None
  65.            ToggleButton:
  66.                text: 'Ler Localizacao' if self.state == 'normal' else 'Parar Leitura'
  67.                on_state:
  68.                    app.start(1000, 0) if self.state == 'down' else \
  69.                    app.stop()
  70.            Button:
  71.                text: 'Salvar'
  72.                on_press: app.salvarLocalizacao()
  73. '''
  74.  
  75. class GpsTest(App):
  76.     gps_get = StringProperty()
  77.     gps_location = StringProperty()
  78.     gps_status = StringProperty()
  79.     detail_text = StringProperty()
  80.  
  81.     app_lat = 50.6394 #i placed this default values here but you can take them out and use '' instead when u are ready
  82.     app_long = 3.057 #i placed this default values here but you can take them out and use '' instead when u are ready
  83.  
  84.     def build(self):
  85.         try:
  86.             gps.configure(on_location=self.on_location,
  87.                           on_status=self.on_status)
  88.  
  89.         except NotImplementedError:
  90.             import traceback
  91.             traceback.print_exc()
  92.             self.gps_status = 'Por favor, ative o GPS'
  93.         return Builder.load_string(kv)
  94.  
  95.     def start(self, minTime, minDistance):
  96.         gps.start(minTime, minDistance)
  97.  
  98.     def stop(self):
  99.         gps.stop()
  100.  
  101.     @mainthread
  102.     def on_location(self, **kwargs):
  103.         self.lat = kwargs.get('lat')
  104.         self.long = kwargs.get('lon')
  105.  
  106.         self.app_lat = self.lat
  107.         self.app_long = self.long
  108.  
  109.  
  110.     @mainthread
  111.     def on_status(self, stype, status):
  112.         self.gps_status = 'type={}\n{}'.format(stype, status)
  113.  
  114.     def on_pause(self):
  115.         gps.stop()
  116.         return True
  117.  
  118.     def on_resume(self):
  119.         gps.start(1000, 0)
  120.         pass
  121.  
  122.     def salvarLocalizacao(self):
  123.  
  124.         if self.root.ids.assunto.text == "Buraco e Pavimentação":
  125.             self.assunto = "pavimentacao"
  126.         elif self.root.ids.assunto.text == "Calçadas guias e Postes":
  127.             self.assunto = "calcadasguias"
  128.         elif self.root.ids.assunto.text == "Ciclovias, Faixas e outros":
  129.             self.assunto = "cicliovia"
  130.         elif self.root.ids.assunto.text == "Semáforos, sinalização e outros.":
  131.             self.assunto = "semaforos"
  132.         else:
  133.             self.assunto = "iluminacao"
  134.  
  135.         self.localizacao = "POINT(" + self.gps_location + ")"
  136.         self.params = json.dumps({"assunto": self.assunto, "localizacao": self.localizacao})
  137.         self.headers = {'Content-type': 'application/json',
  138.                         'Accept': 'application/json; charset=UTF-8',
  139.                         'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'}
  140.         self.req = UrlRequest('http://aaa.aaa.com:9000/api/add/', req_body=self.params, req_headers=self.headers, on_success=self.postSucess, on_error=self.postFail)
  141.  
  142.     def postSucess(self, req, result):
  143.         text = Label(text="Enviado com sucesso!".format())
  144.         pop_up = Popup(title="Sucesso", content=text, size_hint=(.7, .7))
  145.         pop_up.open()
  146.  
  147.     def postFail(self, req, result):
  148.         text = Label(text="Erro de conexão, verifique sua internet!".format())
  149.         pop_up = Popup(title="Erro de conexão", content=text, size_hint=(.7, .7))
  150.         pop_up.open()
  151.  
  152. if __name__ == '__main__':
  153.     GpsTest().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement