Google Maps Platform Python Client

4.10.0 · active · verified Fri Apr 10

The `googlemaps` Python client library provides a convenient interface for accessing Google Maps Platform web services, including Directions, Distance Matrix, Elevation, Geocoding, Places, Roads, and Time Zone APIs. It simplifies common tasks such as authentication, automatic rate limiting, and retries on failure. The library is actively maintained, with regular releases (current version 4.10.0) adding new features and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates initializing the Google Maps client with an API key (preferably from an environment variable) and performing a basic geocoding request to convert an address into geographic coordinates. Ensure your Google Maps API key is enabled for the Geocoding API and properly restricted.

import os
import googlemaps

# Set your API key as an environment variable (e.g., GOOGLE_MAPS_API_KEY)
api_key = os.environ.get('GOOGLE_MAPS_API_KEY', 'YOUR_API_KEY')

if api_key == 'YOUR_API_KEY' or not api_key:
    print("WARNING: Please set the GOOGLE_MAPS_API_KEY environment variable or replace 'YOUR_API_KEY' in the code.")
    exit()

gmaps = googlemaps.Client(key=api_key)

# Geocoding an address
address = "1600 Amphitheatre Parkway, Mountain View, CA"
geocode_result = gmaps.geocode(address)

if geocode_result:
    print(f"Geocoding for '{address}':")
    for result in geocode_result:
        location = result['geometry']['location']
        print(f"  Latitude: {location['lat']}, Longitude: {location['lng']}")
else:
    print(f"No geocoding results found for '{address}'.")

view raw JSON →