Here's what i've been using to geocode locations that I plan to place
on a map later on. I hope it simplifies the task for anyone else in
the future. I'm a python newbie, so it might have a few inefficiencies
that need to be taken care of.
from google.appengine.api import urlfetch
from xml.dom import minidom
def geocode(city,state,street):
#=============================================
#GEOCODE apartment address via google maps api
#==============================================
address= street+", "+city+", "+state;
location=address.replace(" ","+")
key=#PUT YOUR API KEY HERE
url='
http://maps.google.com/maps/geo?q='+location
+'&output=xml&key='+key
result2 = urlfetch.fetch(url)
dom2 = minidom.parseString(result2.content)
geo_status = int(dom2.getElementsByTagName("code")
[0].childNodes[0].data)
#check to see if google was able to find the coordinates for the
street address
if geo_status == 200:
coord = dom2.getElementsByTagName("coordinates")
[0].childNodes[0].data
split_coord = coord.rsplit(",")
longitude=(split_coord[0])
latitude=(split_coord[1])
coord=latitude+","+longitude
return coord
it returns the coordinates with latitude first and then longitude.
Hope it helps.