public
Fork of simonw/geocoders
Description: Ultra simple API for geocoding a single string against various web services.
Homepage:
Clone URL: git://github.com/jacobian/geocoders.git
geocoders / google.py
100644 24 lines (20 sloc) 0.656 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import urllib
from utils import simplejson, partial2
 
# 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 None, (None, None)
    name = json['Placemark'][0]['address']
    return name, (lat, lon)
 
geocoder = partial2(geocode)