Here is my problem. I have a sample text file where I store the text data by crawling various html pages. This text contains information about various events and its time an
Since September 2013, Google Maps API v2 no longer works. Here is an updated version of great @jimhark's code, working for API v3 (I left out the __main__ part):
import urllib
import simplejson
googleGeocodeUrl = 'http://maps.googleapis.com/maps/api/geocode/json?'
def get_coordinates(query, from_sensor=False):
query = query.encode('utf-8')
params = {
'address': query,
'sensor': "true" if from_sensor else "false"
}
url = googleGeocodeUrl + urllib.urlencode(params)
json_response = urllib.urlopen(url)
response = simplejson.loads(json_response.read())
if response['results']:
location = response['results'][0]['geometry']['location']
latitude, longitude = location['lat'], location['lng']
print query, latitude, longitude
else:
latitude, longitude = None, None
print query, ""
return latitude, longitude
See official documentation for the complete list of parameters and additional information.