googlemaps – Google Maps and Local Search APIs in Python

class GoogleMaps

An easy-to-use Python wrapper for the Google Maps and Local Search APIs.

Geocoding: convert a postal address to latitude and longitude

>>> from googlemaps import GoogleMaps
>>> gmaps = GoogleMaps(api_key)
>>> address = 'Constitution Ave NW & 10th St NW, Washington, DC'
>>> lat, lng = gmaps.address_to_latlng(address)
>>> print lat, lng
38.8921021 -77.0260358

Reverse Geocoding: find the nearest address to (lat, lng)

>>> destination = gmaps.latlng_to_address(38.887563, -77.019929)
>>> print destination
Independence and 6th SW, Washington, DC 20024, USA

Local Search: find places matching a query near a given location

>>> local = gmaps.local_search('cafe near ' + destination)
>>> print local['responseData']['results'][0]['titleNoFormatting']
Vie De France Bakery & Cafe

Directions: turn-by-turn directions, distance, time, etc. from point A to point B

>>> directions = gmaps.directions(address, destination)
>>> print directions['Directions']['Distance']['meters']
>>> print directions['Directions']['Duration']['seconds']
>>> for step in directions['Directions']['Routes'][0]['Steps']:
...     print step['descriptionHtml']
Head <b>east</b> on <b>Constitution Ave NW</b> toward <b>9th St NW</b>
Take the 2nd <b>right</b> onto <b>7th St NW</b>
Turn <b>left</b> at <b>Independence Ave SW</b>

This software is in no way associated with or endorsed by Google Inc. Use of the Google Maps API is governed by its Terms of Service: Note in particular that you will need your own Google Maps API key to use this service, and that there are rate limits to the number of requests you can make.

GoogleMaps methods

GoogleMaps.__init__(api_key='', referrer_url='')

Create a new GoogleMaps object using the given api_key and referrer_url.

  • api_key (string) – Google Maps API key
  • referrer_url (string) – URL of the website using or displaying information from this module.

Google requires API users to register for an API key before using the geocoding service; this can be done at If you are not geocoding, you do not need an API key.

Use of Google Local Search requires a referrer URL, generally the website where the retrieved information will be used. If you are not using Local Search, you do not need a referrer URL.



Given a string address, return a (latitude, longitude) pair.

This is a simplified wrapper for geocode().

Parameter:address (string) – The postal address to geocode.
Returns:(latitude, longitude) of address.
Return type:(float, float)
Raises GoogleMapsError:
 If the address could not be geocoded.
GoogleMaps.geocode(query, sensor='false', oe='utf8', ll='', spn='', gl='')

Given a string address query, return a dictionary of information about that location, including its latitude and longitude.

Interesting bits:

>>> gmaps = GoogleMaps(api_key)
>>> address = '350 Fifth Avenue New York, NY'
>>> result = gmaps.geocode(address)
>>> placemark = result['Placemark'][0]
>>> lng, lat = placemark['Point']['coordinates'][0:2]    # Note these are backwards from usual
>>> print lat, lng
40.6721118 -73.9838823
>>> details = placemark['AddressDetails']['Country']['AdministrativeArea']
>>> street = details['Locality']['Thoroughfare']['ThoroughfareName']
>>> city = details['Locality']['LocalityName']
>>> state = details['AdministrativeAreaName']
>>> zipcode = details['Locality']['PostalCode']['PostalCodeNumber']
>>> print ', '.join((street, city, state, zipcode))
350 5th Ave, Brooklyn, NY, 11215

More documentation on the format of the return value can be found at Google’s geocoder return value reference. (Note: Some places have a ‘SubAdministrativeArea’ and some don’t; sometimes a ‘Locality’ will have a ‘DependentLocality’ and some don’t.)

  • query (string) – Address of location to be geocoded.
  • sensor (string) – 'true' if the address is coming from, say, a GPS device.
  • oe (string) – Output Encoding; best left at 'utf8'.
  • ll (string) – lat,lng of the viewport center as comma-separated string; must be used with spn for Viewport Biasing
  • spn (string) – The “span” of the viewport; must be used with ll.
  • gl (string) – Two-character ccTLD for country code biasing.

geocoder return value dictionary

Return type:


Raises GoogleMapsError:

if there is something wrong with the query.

More information on the types and meaning of the parameters can be found at the Google HTTP Geocoder site.

Reverse Geocoding

GoogleMaps.latlng_to_address(lat, lng)

Given a latitude lat and longitude lng, return the closest address.

This is a simplified wrapper for reverse_geocode().

  • lat (float) – latitude
  • lng (float) – longitude

Closest postal address to (lat, lng), if any.

Return type:


Raises GoogleMapsError:

if the coordinates could not be converted to an address.

GoogleMaps.reverse_geocode(lat, lng, sensor='false', oe='utf8', ll='', spn='', gl='')

Converts a (latitude, longitude) pair to an address.

Interesting bits:

>>> gmaps = GoogleMaps(api_key)
>>> reverse = gmaps.reverse_geocode(38.887563, -77.019929)
>>> address = reverse['Placemark'][0]['address']
>>> print address
Independence and 6th SW, Washington, DC 20024, USA
>>> accuracy = reverse['Placemark'][0]['AddressDetails']['Accuracy']
>>> print accuracy
  • lat (float) – latitude
  • lng (float) – longitude

Reverse geocoder return value dictionary giving closest address(es) to (lat, lng)

Return type:


Raises GoogleMapsError:

If the coordinates could not be reverse geocoded.

Keyword arguments and return value are identical to those of geocode().


GoogleMaps.directions(origin, destination, **kwargs)

Get driving directions from origin to destination.

Interesting bits:

>>> gmaps = GoogleMaps(api_key)
>>> start = 'Constitution Ave NW & 10th St NW, Washington, DC'
>>> end   = 'Independence and 6th SW, Washington, DC 20024, USA'
>>> dirs  = gmaps.directions(start, end) 
>>> time  = dirs['Directions']['Duration']['seconds']
>>> dist  = dirs['Directions']['Distance']['meters']
>>> route = dirs['Directions']['Routes'][0]
>>> for step in route['Steps']:
...    print step['Point']['coordinates'][1], step['Point']['coordinates'][0] 
...    print step['descriptionHtml']
38.8921 -77.02604
Head <b>east</b> on <b>Constitution Ave NW</b> toward <b>9th St NW</b>
38.89208 -77.02191
Take the 2nd <b>right</b> onto <b>7th St NW</b>
38.88757 -77.02191
Turn <b>left</b> at <b>Independence Ave SW</b>
  • origin (string) – Starting address
  • destination (string) – Ending address
  • kwargs – You can pass additional URL parameters as keyword arguments, but this functionality is not documented.

Dictionary containing driving directions.

Return type:


Raises GoogleMapsError:

If Google Maps was unable to find directions.


It’s as easy as:

sudo easy_install googlemaps

For Python versions prior to 2.6, you may also need the simplejson module.

Not got root? googlemaps plays nice with virtualenv.

You can also download the source from; packs all this delicious functionality into a single, self-contained module that can be used as a script for command-line geocoding.

easy_install is available from PyPI if you don’t have it already.


You will need your own Google Maps API key to use the geocoding functions of this module. There are rate limits to the number of requests per day from a single IP address. If you make too many requests, or you use an invalid API key, or something else is wrong with your request, a GoogleMapsError will be raised containing a status code and brief description of the error; more information can be found at the linked reference.

All of the data returned by this module is in JSON-compatible format, making it easy to combine with other web services.


Author:John Kleint
License:Lesser Affero General Public License v3
Python Versions:
 2.3 - 2.6+

This software comes with no warranty and is in no way associated with Google Inc. or the Google Maps™ mapping service. Google does not approve or endorse this software. GOOGLE is a trademark of Google Inc.