forked from simonw/geocoders
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgoogle.py
More file actions
23 lines (20 loc) · 718 Bytes
/
google.py
File metadata and controls
23 lines (20 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import urllib
from utils import simplejson, partial2, RichResult
# http://code.google.com/apis/maps/documentation/geocoding/index.html
def geocode(q, api_key):
json = simplejson.load(urllib.urlopen(
'http://maps.google.com/maps/geo?' + urllib.urlencode({
'q': q,
'output': 'json',
'oe': 'utf8',
'sensor': 'false',
'key': api_key
})
))
try:
lon, lat = json['Placemark'][0]['Point']['coordinates'][:2]
except (KeyError, IndexError):
return RichResult((None, (None, None)), data=None)
name = json['Placemark'][0]['address']
return RichResult((name, (lat, lon)), data=json)
geocoder = partial2(geocode)