-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
27 lines (23 loc) · 859 Bytes
/
utils.py
File metadata and controls
27 lines (23 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import json
import urllib
import urllib2
def geolocate(location, sensor=False):
"""
Take a "location" and return its latitude and longitude
Keyword arguments:
location - String defining a geographical location (address, zip code, etc)
sensor - Boolean defining whether the location was taken from
an on-device sensor
Output:
latitude and logitude in an dict
"""
sensor = str(sensor).lower()
url = "http://maps.googleapis.com/maps/api/geocode/json?"
url += urllib.urlencode({'address': location, 'sensor': sensor})
data = urllib2.urlopen(url).read()
data = json.loads(data)
if data['status'] == 'OK':
return({
'latitude': data['results'][0]['geometry']['location']['lat'],
'longitude': data['results'][0]['geometry']['location']['lng']
})