I need to geocode an address to a latitude, longitude pair to display on Google Maps, but I need to do this server-side in Django. I could only find reference to the Javascript V3 API. How do I do it from Python?
5 Answers
I would strongly recommend to use geopy. It will return the latitude and longitude, you can use it in the Google JS client afterwards.
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("175 5th Avenue NYC")
>>> print(location.address)
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, ...
>>> print((location.latitude, location.longitude))
(40.7410861, -73.9896297241625)
Additionally you can specifically define you want to use Google services by using GoogleV3
class as a geolocator
>>> from geopy.geocoders import GoogleV3
>>> geolocator = GoogleV3()

- 3,666
- 26
- 23
I would suggest using Py-Googlemaps. To use it is easy:
from googlemaps import GoogleMaps
gmaps = GoogleMaps(API_KEY)
lat, lng = gmaps.address_to_latlng(address)
EDIT: If necessary, install Py-Googlemaps
via: sudo easy_install googlemaps
.

- 2,298
- 19
- 30

- 46,821
- 21
- 100
- 139
-
1Is it possible to get API keys that work with this library anymore? I generated a Javascript v3 API key but it doesn't seem to be working. – powerj1984 Apr 23 '13 at 14:57
-
@powerj1984 I get a warning for malware when I try to access your link, is that normal or should I be worried? – Dennis Jaheruddin Feb 23 '17 at 20:22
-
When I posted the link there was no warning for malware. Not sure what's going on now. – powerj1984 Feb 24 '17 at 05:01
Google Data has an API for Maps has a REST-ful API - and they also have a Python library built around it already.

- 155,703
- 32
- 311
- 293
Here is the code working for Google Maps API v3 (based on this answer):
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, "<no results>"
return latitude, longitude
See official documentation for the complete list of parameters and additional information.

- 1
- 1

- 16,269
- 5
- 73
- 81
-
this doesn't work anymore because google quires a personal API key to work: http://g.co/dev/maps-no-account – skjerns Mar 04 '20 at 11:55
-
@skjerns thanks for the update! if it's easy to fix, feel free to update the answer – Dennis Golomazov Mar 04 '20 at 15:22
Python package googlemaps
seems to be very able to do geocoding and reverse geocoding.
The latest version of googlemaps package is available at: https://pypi.python.org/pypi/googlemaps

- 1,745
- 21
- 35