From 6c0dc9a4dcd9c1b9b4b6009555a92fcf429afed2 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 13 May 2018 21:42:26 +0200 Subject: [PATCH] Added GMaps geocode plugin and geo update event --- platypush/message/event/geo.py | 10 +++++++ platypush/plugins/google/maps.py | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 platypush/message/event/geo.py create mode 100644 platypush/plugins/google/maps.py diff --git a/platypush/message/event/geo.py b/platypush/message/event/geo.py new file mode 100644 index 00000000..e699dc2e --- /dev/null +++ b/platypush/message/event/geo.py @@ -0,0 +1,10 @@ +from platypush.message.event import Event + + +class LatLongUpdateEvent(Event): + def __init__(self, latitude, longitude, *args, **kwargs): + super().__init__(latitude=latitude, longitude=longitude, *args, **kwargs) + + +# vim:sw=4:ts=4:et: + diff --git a/platypush/plugins/google/maps.py b/platypush/plugins/google/maps.py new file mode 100644 index 00000000..84511d52 --- /dev/null +++ b/platypush/plugins/google/maps.py @@ -0,0 +1,50 @@ +import json +import requests + +from platypush.message.response import Response +from platypush.plugins.google import GooglePlugin + + +class GoogleMapsPlugin(GooglePlugin): + scopes = [] + + def __init__(self, api_key, *args, **kwargs): + super().__init__(scopes=self.scopes, *args, **kwargs) + self.api_key = api_key + + + def get_address_from_latlng(self, latitude, longitude): + response = requests.get('https://maps.googleapis.com/maps/api/geocode/json', + params = { + 'latlng': '{},{}'.format(latitude, longitude), + 'key': self.api_key, + }).json() + + address = dict( + (t, None) for t in ['street_number', 'street', 'locality', 'country', 'postal_code'] + ) + + address['latitude'] = latitude + address['longitude'] = longitude + + if 'results' in response and response['results']: + result = response['results'][0] + + for addr_component in result['address_components']: + for component_type in addr_component['types']: + if component_type == 'street_number': + address['street_number'] = addr_component['long_name'] + elif component_type == 'route': + address['street'] = addr_component['long_name'] + elif component_type == 'locality': + address['locality'] = addr_component['long_name'] + elif component_type == 'country': + address['country'] = addr_component['short_name'] + elif component_type == 'postal_code': + address['postal_code'] = addr_component['long_name'] + + return Response(output=address) + + +# vim:sw=4:ts=4:et: +