Google Maps Places API is a powerful tool that allows developers to access detailed information about places around the world. In this tutorial, we will guide you through the process of using the new Google Maps Places API in Python, from setting up your Google Cloud project to making API requests and handling responses.
What is Google Maps Places API?
Google Maps has two versions of the Places API:
- Legacy API – Allows connection with a simple API key.
- Next-Generation API – Requires OAuth 2.0 for authentication and provides more advanced features.
Setting Up Your Google Cloud Project
- Navigate to Google Cloud Console: Go to Google Cloud Console.
- Create a Project: If you don’t have a project, create one by clicking on the drop-down menu and selecting “New Project”.
- Enable APIs and Services: Go to APIs and Services > Dashboard, and enable the Places API.
- Set Up OAuth 2.0: Go to APIs and Services > OAuth Consent Screen, set up the consent screen, and create OAuth 2.0 credentials.
Authenticating with OAuth 2.0
- Create OAuth Client ID: Choose “Desktop App” as the application type and download the client secret file.
- Install Required Libraries: Use pip to install the Google Python client library and Python dotenv package.
pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client python-dotenv
Making API Requests
Initializing the API Client
Create a Python script (e.g., maps_textsearch.py
) and initialize the API client using the downloaded credentials.
from google.oauth2 import service_account
from googleapiclient.discovery import build
import json
def create_service(client_secret_file, api_name, api_version, scopes):
credentials = service_account.Credentials.from_service_account_file(client_secret_file, scopes=scopes)
return build(api_name, api_version, credentials=credentials)
CLIENT_SECRET_FILE = 'path/to/client_secret.json'
API_SERVICE_NAME = 'places'
API_VERSION = 'v1'
SCOPES = ['https://www.googleapis.com/auth/maps']
service = create_service(CLIENT_SECRET_FILE, API_SERVICE_NAME, API_VERSION, SCOPES)
Example: Text Search
Define Search Parameters: Create a query to search for specific places.
query = 'ramen places in New York'
response = service.places().textSearch(query=query).execute()
print(json.dumps(response, indent=4))
Handle the Response: Extract and display relevant information.
for place in response['results']:
print(place['name'], place['formatted_address'])
Example: Place Details
Get Place Details: Retrieve detailed information about a specific place using its place ID.
place_id = 'ChIJN1t_tDeuEmsRUsoyG83frY4'
response = service.places().get(place_id=place_id).execute()
print(json.dumps(response, indent=4))
Display Details: Extract and print details such as reviews, opening hours, etc.
print('Name:', response['name'])
print('Address:', response['formatted_address'])
print('Phone:', response['formatted_phone_number'])
Pricing and Quotas
Google offers $200 of free credit per month for all Maps products. Monitor your usage to avoid unexpected charges.
Conclusion
By following these steps, you can successfully integrate Google Maps Places API into your Python applications, providing rich place-related data to enhance user experience.
Table: Key Points
Step | Description |
---|---|
Project Setup | Create a project in Google Cloud Console, enable Places API, and set up OAuth 2.0 credentials. |
Authentication | Use OAuth 2.0 to authenticate API requests. |
API Requests | Make API requests using the Python client library. |
Response Handling | Extract and display relevant information from API responses. |
Pricing | Utilize the $200 free credit provided by Google Maps. |
Example Code Snippet
from google.oauth2 import service_account
from googleapiclient.discovery import build
def create_service(client_secret_file, api_name, api_version, scopes):
credentials = service_account.Credentials.from_service_account_file(client_secret_file, scopes=scopes)
return build(api_name, api_version, credentials=credentials)
CLIENT_SECRET_FILE = 'path/to/client_secret.json'
API_SERVICE_NAME = 'places'
API_VERSION = 'v1'
SCOPES = ['https://www.googleapis.com/auth/maps']
service = create_service(CLIENT_SECRET_FILE, API_SERVICE_NAME, API_VERSION, SCOPES)
query = 'ramen places in New York'
response = service.places().textSearch(query=query).execute()
print(json.dumps(response, indent=4))
for place in response['results']:
print(place['name'], place['formatted_address'])
By following this guide, you can easily get started with Google Maps Places API and unlock a wealth of location-based data for your applications. Enjoy exploring the world of Google Maps!
Comments (0)
There are no comments here yet, you can be the first!