Python client library for Google Maps Platform providing access to mapping services, geocoding, routing, and location APIs
—
Convert between addresses and geographic coordinates using Google's geocoding and reverse geocoding APIs with support for component filtering, bounds restriction, and localization.
Convert addresses to geographic coordinates (latitude/longitude) with support for various address formats and filtering options.
def geocode(client, address=None, place_id=None, components=None,
bounds=None, region=None, language=None):
"""
Convert address to coordinates using Google's geocoding API.
Args:
client (Client): Google Maps API client instance
address (str): Street address to geocode (e.g., "1600 Amphitheatre Parkway")
place_id (str): Place ID to geocode instead of address
components (dict): Component filters to restrict results by country,
postal code, etc. Format: {"country": "US", "postal_code": "94043"}
bounds (dict): Bounding box to bias results. Format with southwest/northeast
lat/lng coordinates
region (str): Region code for result biasing (ISO 3166-1 Alpha-2)
language (str): Language code for returned results (ISO 639-1)
Returns:
list: List of geocoding result dictionaries containing geometry,
formatted address, address components, and place information
Raises:
googlemaps.exceptions.ApiError: When API returns an error
googlemaps.exceptions.TransportError: When HTTP request fails
googlemaps.exceptions.Timeout: When request times out
"""Convert geographic coordinates to human-readable addresses with filtering options for result types and location precision.
def reverse_geocode(client, latlng, result_type=None, location_type=None,
language=None):
"""
Convert coordinates to address using Google's reverse geocoding API.
Args:
client (Client): Google Maps API client instance
latlng (tuple): Latitude/longitude coordinates as (lat, lng) tuple
or {"lat": latitude, "lng": longitude} dict
result_type (list): Filter results by address type (e.g., ["street_address"])
location_type (list): Filter by location type precision
(e.g., ["APPROXIMATE", "GEOMETRIC_CENTER"])
language (str): Language code for returned results (ISO 639-1)
Returns:
list: List of reverse geocoding result dictionaries containing
formatted addresses, address components, and geometry
Raises:
googlemaps.exceptions.ApiError: When API returns an error
googlemaps.exceptions.TransportError: When HTTP request fails
googlemaps.exceptions.Timeout: When request times out
"""import googlemaps
gmaps = googlemaps.Client(key='YOUR_API_KEY')
# Geocode a simple address
geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
# Extract coordinates
location = geocode_result[0]['geometry']['location']
lat = location['lat']
lng = location['lng']
print(f"Coordinates: {lat}, {lng}")
# Get formatted address
formatted_address = geocode_result[0]['formatted_address']
print(f"Formatted: {formatted_address}")import googlemaps
gmaps = googlemaps.Client(key='YOUR_API_KEY')
# Geocode with component restrictions
geocode_result = gmaps.geocode(
address='Main Street',
components={
'country': 'US',
'administrative_area': 'CA',
'locality': 'Mountain View'
}
)
for result in geocode_result:
print(f"Address: {result['formatted_address']}")
print(f"Location: {result['geometry']['location']}")import googlemaps
gmaps = googlemaps.Client(key='YOUR_API_KEY')
# Define bounds for San Francisco Bay Area
bounds = {
'southwest': {'lat': 37.4, 'lng': -122.5},
'northeast': {'lat': 37.8, 'lng': -122.0}
}
# Geocode with bounds biasing
geocode_result = gmaps.geocode(
address='Main Street',
bounds=bounds,
region='us'
)import googlemaps
gmaps = googlemaps.Client(key='YOUR_API_KEY')
# Geocode using a place ID
geocode_result = gmaps.geocode(
place_id='ChIJVVVVVVVQwokRGjGGGGGGGG'
)
location = geocode_result[0]['geometry']['location']
print(f"Place coordinates: {location['lat']}, {location['lng']}")import googlemaps
gmaps = googlemaps.Client(key='YOUR_API_KEY')
# Convert coordinates to address
latlng = (37.4224764, -122.0842499)
reverse_result = gmaps.reverse_geocode(latlng)
# Get the most precise address
best_result = reverse_result[0]
print(f"Address: {best_result['formatted_address']}")
# Get address components
components = best_result['address_components']
for component in components:
types = component['types']
name = component['long_name']
print(f"{types[0]}: {name}")import googlemaps
gmaps = googlemaps.Client(key='YOUR_API_KEY')
# Get only street addresses
coordinates = (37.4224764, -122.0842499)
street_addresses = gmaps.reverse_geocode(
latlng=coordinates,
result_type=['street_address']
)
# Get only administrative areas
admin_areas = gmaps.reverse_geocode(
latlng=coordinates,
result_type=['administrative_area_level_1', 'administrative_area_level_2']
)
# Filter by location type precision
precise_results = gmaps.reverse_geocode(
latlng=coordinates,
location_type=['ROOFTOP', 'RANGE_INTERPOLATED']
)import googlemaps
gmaps = googlemaps.Client(key='YOUR_API_KEY')
# Get results in different languages
coordinates = (35.6762, 139.6503) # Tokyo coordinates
# Japanese results
japanese_result = gmaps.reverse_geocode(
latlng=coordinates,
language='ja'
)
print(f"Japanese: {japanese_result[0]['formatted_address']}")
# English results
english_result = gmaps.reverse_geocode(
latlng=coordinates,
language='en'
)
print(f"English: {english_result[0]['formatted_address']}")Geocoding results include standardized address components that can be extracted:
# Example of parsing address components
for component in result['address_components']:
component_type = component['types'][0]
if component_type == 'street_number':
street_number = component['long_name']
elif component_type == 'route':
street_name = component['long_name']
elif component_type == 'locality':
city = component['long_name']
elif component_type == 'administrative_area_level_1':
state = component['short_name']
elif component_type == 'country':
country = component['long_name']
elif component_type == 'postal_code':
zip_code = component['long_name']Install with Tessl CLI
npx tessl i tessl/pypi-googlemaps