Google Maps Places API Client Library

0.8.0 · active · verified Fri Apr 17

The `google-maps-places` client library provides a Python interface for the Google Maps Places API (v1). It allows developers to programmatically access place information, including searches, details, and photos. This library is part of the broader `google-cloud-python` ecosystem and currently at version `0.8.0`, implying it's in a preview or early access stage. Google Cloud client libraries typically receive frequent updates for bug fixes and new features, often on a weekly or bi-weekly basis for individual sub-libraries.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `PlacesClient` and perform a basic `FindPlaceFromText` query. Authentication is handled by Google's Application Default Credentials (ADC), which requires prior setup (e.g., `gcloud auth application-default login` for local development or setting `GOOGLE_APPLICATION_CREDENTIALS`). The Places API must also be enabled in your Google Cloud project.

import os
from google.maps.places_v1 import PlacesClient
from google.api_core.exceptions import GoogleAPIError

# Ensure your environment is authenticated. For local development, run:
# `gcloud auth application-default login`
# Or set GOOGLE_APPLICATION_CREDENTIALS environment variable to a service account key file.
# The Google Maps Places API also needs to be enabled for your GCP project.

try:
    # Initialize the client. It uses Application Default Credentials by default.
    client = PlacesClient()

    # Construct a request to find places from a text query.
    # Specify 'fields' to control the data returned and reduce costs.
    request = PlacesClient.FindPlaceFromTextRequest(
        text_query="restaurants near Times Square, New York",
        fields=["id", "displayName", "formattedAddress", "rating"],
        language_code="en", # Optional: influence results language
        region_code="us"    # Optional: bias results towards a region
    )

    response = client.find_place_from_text(request=request)

    if response.places:
        print("Found Places:")
        for place in response.places:
            print(f"- Name: {place.display_name.text}")
            print(f"  Address: {place.formatted_address}")
            print(f"  Rating: {place.rating if place.rating else 'N/A'}")
            print(f"  ID: {place.id}")
    else:
        print("No places found for the query.")

except GoogleAPIError as e:
    print(f"An API error occurred: {e}")
    print("Common causes: Places API not enabled, incorrect authentication, or rate limits.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →