How to Use Google Maps API with Python
Learn how to use Google Maps APIs with Python to geocode addresses, search places, collect business location data, and work with structured JSON responses for apps, local SEO, and data workflows.
Google Maps data is useful when your application needs addresses, coordinates, places, business details, routes, or location-based search.
With Python, you can call Google Maps APIs from the server side, store structured JSON responses, and connect the data to dashboards, internal tools, local search workflows, or AI systems.
This guide shows a practical way to use Google Maps APIs with Python, including geocoding an address and searching places with the Places API.
What “Google Maps API” Means
People often say “Google Maps API,” but Google Maps Platform includes several APIs and SDKs. For Python server-side work, common options include Geocoding API, Places API, Directions API, Distance Matrix API, Time Zone API, and other web services. Google also provides client libraries for Java, Python, Go, and Node.js to work with Google Maps web services on the server.
For example:
|
Task |
API to Use |
|
Convert address to latitude / longitude |
Geocoding API |
|
Find places by keyword |
Places API Text Search |
|
Search nearby businesses |
Places API Nearby Search |
|
Get place details |
Places API Place Details |
|
Calculate routes or travel distance |
Routes / Directions-related APIs |
|
Show a map in frontend UI |
Maps JavaScript API |
Before building, create a billing-enabled Google Cloud project, enable the API you need, and generate an API key. Google’s getting-started documentation says an API key is required to authenticate requests and track usage, and it also recommends restricting API keys for production use.
Preparation
Install the Python packages:
pip install googlemaps requests python-dotenv
Store your API key as an environment variable:
export GOOGLE_MAPS_API_KEY="YOUR_API_KEY"
On Windows PowerShell:
setx GOOGLE_MAPS_API_KEY "YOUR_API_KEY"
Do not hard-code your API key in public code. Keep it on the server side and restrict it in Google Cloud Console.
Example 1: Geocode an Address with Python
Geocoding converts a human-readable address into latitude and longitude. Google’s Geocoding API is designed to convert addresses or Place IDs into geographic coordinates and vice versa.
import os
import googlemaps
api_key = os.environ["GOOGLE_MAPS_API_KEY"]
gmaps = googlemaps.Client(key=api_key)
address = "1600 Amphitheatre Parkway, Mountain View, CA"
results = gmaps.geocode(address)
if not results:
print("No results found")
else:
first = results[0]
location = first["geometry"]["location"]
print({
"formatted_address": first["formatted_address"],
"latitude": location["lat"],
"longitude": location["lng"],
"place_id": first["place_id"]
})
Example output:
{
"formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"latitude": 37.4223878,
"longitude": -122.0841877,
"place_id": "ChIJ..."
}
This is useful for store locators, delivery coverage, customer address validation, real estate tools, logistics dashboards, and local business matching.
Example 2: Search Places with Places API Text Search
If you want to search for businesses or points of interest, use Places API. Google’s Places API accepts HTTP requests for location data and returns formatted location data about establishments, geographic locations, or points of interest.
The newer Text Search API can find places from text queries like “pizza in New York.” Google’s documentation notes that Text Search requires a textQuery and a FieldMask to specify which fields should be returned.
import os
import json
import requests
api_key = os.environ["GOOGLE_MAPS_API_KEY"]
url = "https://places.googleapis.com/v1/places:searchText"
headers = {
"Content-Type": "application/json",
"X-Goog-Api-Key": api_key,
"X-Goog-FieldMask": (
"places.displayName,"
"places.formattedAddress,"
"places.location,"
"places.rating,"
"places.userRatingCount,"
"places.googleMapsUri,"
"places.websiteUri"
)
}
payload = {
"textQuery": "coffee shops in Seattle",
"languageCode": "en"
}
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
data = response.json()
for place in data.get("places", []):
print(json.dumps({
"name": place.get("displayName", {}).get("text"),
"address": place.get("formattedAddress"),
"rating": place.get("rating"),
"reviews": place.get("userRatingCount"),
"maps_url": place.get("googleMapsUri"),
"website": place.get("websiteUri"),
"location": place.get("location")
}, indent=2))
Example output:
{
"name": "Example Coffee",
"address": "123 Example St, Seattle, WA",
"rating": 4.6,
"reviews": 842,
"maps_url": "https://maps.google.com/?cid=...",
"website": "https://examplecoffee.com",
"location": {
"latitude": 47.609,
"longitude": -122.333
}
}
This type of data can support local business directories, location-based apps, market research, store expansion analysis, and local SEO workflows.
Common Request Parameters
When using Google Maps APIs with Python, the most important controls are usually:
|
Parameter |
Why It Matters |
|
Query / textQuery |
Defines what you are searching for |
|
Location |
Helps target a city, country, or coordinate area |
|
Language |
Controls localized response text |
|
Field mask |
Limits returned fields and response size |
|
Place ID |
Helps fetch stable details for a specific place |
|
Radius / bounding area |
Useful for nearby search |
|
API key |
Authenticates the request |
For Places API, field masks are especially important. Do not request every possible field if you only need name, address, coordinates, rating, and website.
When Google Maps API Is Not Enough
Google Maps APIs are good for place lookup, geocoding, place details, and location-based app features.
But if your goal is to track how map or local results appear in search, you may need a local SERP or map results API instead. For example, a local SEO team may care about ranking position, query, market, timestamp, and competitor visibility, not only whether a place exists.
You can test this workflow with tools like SerpApi, SearchAPI, Bright Data, or Talordata (Upon successful registration, you can test 1,000 API responses for free). The important part is whether the response includes clean fields such as business name, address, rating, review count, ranking position, query, location, and timestamp, so your SEO tool or AI workflow can use the data directly.
Best Practices
Keep API keys out of frontend code unless the key is properly restricted for browser usage.
Use environment variables or a secret manager for backend projects.
Request only the fields you need. Smaller responses are easier to parse and maintain.
Store the search context with every result: query, location, language, device if relevant, and timestamp.
Add error handling for timeouts, empty results, and quota-related errors.
Cache stable results when possible. Repeated place lookups can become expensive if your app runs them too often.
For production, monitor usage and billing in Google Cloud Console. Google Maps Platform provides billing and usage tools to review monthly costs and savings.
Final Thoughts
Using Google Maps API with Python is straightforward once you know which API matches your task.
Use Geocoding API when you need coordinates from addresses. Use Places API when you need business or point-of-interest data. Use route-related APIs when you need travel time or route planning. Use a local SERP API when your goal is search visibility, local ranking, or competitor monitoring.
The key is not just making the API call. The key is designing a clean data workflow: define the query, set the location, request only the fields you need, store structured JSON, and keep enough metadata to make the result useful later.