Skip to content

Instantly share code, notes, and snippets.

@key
Created November 2, 2011 04:30
Show Gist options
  • Save key/1332860 to your computer and use it in GitHub Desktop.
Save key/1332860 to your computer and use it in GitHub Desktop.
python geocoding (using Google Maps API service)
def geocoding(address):
from httplib2 import Http
import simplejson
url_prefix = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='
(header, response) = Http().request(url_prefix + address)
decoder = simplejson.JSONDecoder()
dict_ = decoder.decode(response)
try:
location = dict_['results'][0]['geometry']['location']
except IndexError:
return None
lat = location['lat']
lng = location['lng']
return (lat, lng)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment