show routes in map in python

To show routes on a map in Python, we can use the Google Maps API to create a map and plot the relevant routes on it. The gmaps library allows us to do this.

Here's an example code snippet that shows you how to display two locations on a map and draw a route between them using the Google Maps API:

main.py
import gmaps
import gmaps.datasets
import gmaps.directions

# Define the two locations as tuples
start_location = (37.7749, -122.4194) # San Francisco, CA
end_location = (40.7128, -74.0060) # New York, NY

# Create the map and set the center and zoom level
fig = gmaps.figure(center=start_location, zoom_level=12)

# Get the directions between the two locations using the Google Maps API
directions = gmaps.directions.Directions(start_location, end_location, mode='car')

# Add the directions layer to the map
fig.add_layer(gmaps.directions.DirectionsRenderer(directions))

# Display the map
fig
596 chars
20 lines

The above code will create a map that displays the route between San Francisco and New York by car. You can change the mode parameter to 'walking', 'bicycling', or 'transit' to get directions for those modes of transportation instead.

gistlibby LogSnag