Retrieving Google geocoded co-ordinates in Python

Import the json and the urllib2 modules:

import json, urllib2

Programmatically contruct the URL to meet the requirements of the the geocoding webservice, i.e. make it look like this (click to view the JSON response):

http://maps.google.com/maps/api/geocode/json?address=An+der+Bonnesse+18,+26725+Emden&sensor=false

In Python, the JSON object will be returned as a dict of nested lists and dicts. Data can be retrieved like this:

data = json.load(urllib2.urlopen(url))
lat = data['results'][0]['geometry']['location']['lat']
lng = data['results'][0]['geometry']['location']['lng']

Leave a Reply