Python Function to Geocode an Address Using Google

7 Feb

I stole this from a python networking book – a great book!!- and made it into a function by allowing the user to enter an address and with a return value array contains the longitude and latitude. Enter an address in the format STREET, CITY,STATE ABBREVIATION:

>>>Import google
Address: 100 10th st, Albuquerque,nm
>>>A[0] # returns the longitude
>>>A[1] # returns the latitude

Here is the function-I saved it as

import urllib, urllib2, json

def latlong():
param = {‘q’: address,’output’: ‘json’, ‘oe’: ‘utf8’}
url = ‘’ + urllib.urlencode(param)
rawreply = urllib2.urlopen(url).read()
reply = json.loads(rawreply)
return a


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: