📅  最后修改于: 2023-12-03 14:45:55.222000             🧑  作者: Mango
Python - Google Maps is a powerful library that allows developers to interact with various Google Maps services using Python programming language. It provides an easy-to-use interface for performing geocoding, reverse geocoding, distance calculations, and other location-based operations.
This library utilizes the Google Maps API, which offers a wide range of features and services including geocoding addresses, displaying maps, finding directions, and more. By using Python - Google Maps, developers can integrate these functionalities into their Python applications and create location-aware applications.
from googlemaps import geocoding
# Geocoding an address
location = geocoding.geocode("1600 Amphitheatre Parkway, Mountain View, CA")
# Accessing latitude and longitude
latitude = location[0]['geometry']['location']['lat']
longitude = location[0]['geometry']['location']['lng']
# Accessing formatted address
formatted_address = location[0]['formatted_address']
from googlemaps import geocoding
# Geocoding coordinates
location = geocoding.reverse_geocode(37.4224764, -122.0842499)
# Accessing formatted address
formatted_address = location[0]['formatted_address']
from googlemaps import distance_matrix
# Calculating distance and duration
result = distance_matrix.distance_matrix(
["Seattle, WA", "San Francisco, CA", "Los Angeles, CA"],
["New York, NY", "Chicago, IL"],
mode="driving")
# Accessing distance and duration values
distance = result['rows'][0]['elements'][0]['distance']['text']
duration = result['rows'][0]['elements'][0]['duration']['text']
from googlemaps import directions
# Requesting directions
result = directions.directions(
"Seattle, WA",
"San Francisco, CA")
# Accessing step-by-step instructions
for step in result[0]['legs'][0]['steps']:
print(step['html_instructions'])
from googlemaps import static_maps
# Generating static map image
image_url = static_maps.static_map_url(
["Seattle, WA", "San Francisco, CA"],
zoom=10,
size=(400, 400),
markers=["Seattle, WA", "San Francisco, CA"])
from googlemaps import places
# Searching for places
results = places.places_nearby(
location=(37.4224764, -122.0842499),
radius=500,
type="restaurant")
# Accessing place details
for place in results['results']:
print(place['name'])
Python - Google Maps library is a valuable tool for developers to utilize the capabilities of Google Maps services in their Python applications. With its extensive range of features, developers can integrate geocoding, reverse geocoding, distance calculations, directions, static maps, and place search functionalities seamlessly. By leveraging the power of Python and the Google Maps API, developers can create location-aware applications with ease.
For more information and detailed documentation, please refer to the official Python - Google Maps library documentation.