Google maps plugin for Jupyter notebooks with interactive visualization capabilities
—
Generate and display routes between locations with support for multiple transportation modes, waypoints, and route customization options. Perfect for showing travel paths and analyzing routing scenarios.
Create route layers between start and end points with optional waypoints and travel preferences.
def directions_layer(start, end, waypoints=None, avoid_ferries=False, travel_mode='DRIVING', avoid_highways=False, avoid_tolls=False, optimize_waypoints=False, show_markers=True, show_route=True, stroke_color='#0088FF', stroke_weight=6.0, stroke_opacity=0.6):
"""
Create a directions layer showing routes.
Parameters:
- start (tuple): Starting location as (latitude, longitude)
- end (tuple): Ending location as (latitude, longitude)
- waypoints (list, optional): List of intermediate (latitude, longitude) points
- avoid_ferries (bool): Whether to avoid ferry routes
- travel_mode (str): Transportation mode ('BICYCLING', 'DRIVING', 'TRANSIT', 'WALKING')
- avoid_highways (bool): Whether to avoid highways
- avoid_tolls (bool): Whether to avoid toll roads
- optimize_waypoints (bool): Whether to optimize waypoint order
- show_markers (bool): Whether to show start/end markers
- show_route (bool): Whether to show the route line
- stroke_color (str): Route line color
- stroke_weight (float): Route line width in pixels
- stroke_opacity (float): Route line opacity (0.0-1.0)
Returns:
Directions: Directions layer instance
"""Widget for displaying and managing route information.
class Directions:
"""
Directions layer widget for route visualization.
Attributes:
- start (tuple): Starting location (latitude, longitude)
- end (tuple): Ending location (latitude, longitude)
- waypoints (list): List of intermediate points
- travel_mode (str): Transportation mode
- avoid_ferries (bool): Route preference for ferries
- avoid_highways (bool): Route preference for highways
- avoid_tolls (bool): Route preference for toll roads
- optimize_waypoints (bool): Whether waypoints are optimized
- show_markers (bool): Whether start/end markers are shown
- show_route (bool): Whether route line is shown
- stroke_color (str): Route line color
- stroke_weight (float): Route line width
- stroke_opacity (float): Route line opacity
"""class DirectionsServiceException(RuntimeError):
"""
Raised when directions service fails.
Common causes:
- Invalid locations (not geocodable)
- No route available between points
- API quota exceeded
- Network connectivity issues
"""import gmaps
gmaps.configure(api_key="YOUR_API_KEY")
# Define start and end points
start = (37.7749, -122.4194) # San Francisco
end = (34.0522, -118.2437) # Los Angeles
# Create directions layer
fig = gmaps.figure()
directions_layer = gmaps.directions_layer(start, end)
fig.add_layer(directions_layer)
figimport gmaps
gmaps.configure(api_key="YOUR_API_KEY")
# Define route points
start = (37.7749, -122.4194) # San Francisco City Hall
waypoints = [
(37.7849, -122.4094), # Golden Gate Park
(37.8024, -122.4058) # Presidio
]
end = (37.8199, -122.4783) # Golden Gate Bridge
# Create walking directions
fig = gmaps.figure()
directions_layer = gmaps.directions_layer(
start,
end,
waypoints=waypoints,
travel_mode='WALKING',
stroke_color='green',
stroke_weight=4.0
)
fig.add_layer(directions_layer)
figimport gmaps
gmaps.configure(api_key="YOUR_API_KEY")
# Public transit route
start = (37.7749, -122.4194) # Downtown SF
end = (37.7849, -122.4094) # Mission District
fig = gmaps.figure()
directions_layer = gmaps.directions_layer(
start,
end,
travel_mode='TRANSIT',
stroke_color='blue',
stroke_weight=5.0,
stroke_opacity=0.8
)
fig.add_layer(directions_layer)
figimport gmaps
gmaps.configure(api_key="YOUR_API_KEY")
# Multiple delivery stops
start = (37.7749, -122.4194) # Warehouse
waypoints = [
(37.7649, -122.4294), # Stop 1
(37.7849, -122.4094), # Stop 2
(37.7949, -122.3994), # Stop 3
(37.7549, -122.4494) # Stop 4
]
end = (37.7749, -122.4194) # Return to warehouse
fig = gmaps.figure()
directions_layer = gmaps.directions_layer(
start,
end,
waypoints=waypoints,
travel_mode='DRIVING',
optimize_waypoints=True, # Find best order
avoid_tolls=True, # Avoid toll roads
avoid_highways=False, # Allow highways
stroke_color='red',
stroke_weight=4.0
)
fig.add_layer(directions_layer)
figimport gmaps
gmaps.configure(api_key="YOUR_API_KEY")
start = (37.7749, -122.4194) # Start point
end = (37.7649, -122.4094) # End point
fig = gmaps.figure()
directions_layer = gmaps.directions_layer(
start,
end,
travel_mode='BICYCLING',
stroke_color='orange',
stroke_weight=3.0,
stroke_opacity=0.7
)
fig.add_layer(directions_layer)
figimport gmaps
gmaps.configure(api_key="YOUR_API_KEY")
start = (37.7749, -122.4194)
end = (37.6879, -122.4702)
fig = gmaps.figure()
# Driving route
driving_layer = gmaps.directions_layer(
start, end,
travel_mode='DRIVING',
stroke_color='blue',
stroke_weight=4.0
)
fig.add_layer(driving_layer)
# Transit route
transit_layer = gmaps.directions_layer(
start, end,
travel_mode='TRANSIT',
stroke_color='green',
stroke_weight=4.0
)
fig.add_layer(transit_layer)
# Walking route
walking_layer = gmaps.directions_layer(
start, end,
travel_mode='WALKING',
stroke_color='red',
stroke_weight=3.0
)
fig.add_layer(walking_layer)
figimport gmaps
gmaps.configure(api_key="YOUR_API_KEY")
start = (37.7749, -122.4194)
end = (37.7849, -122.4094)
fig = gmaps.figure()
directions_layer = gmaps.directions_layer(
start,
end,
travel_mode='DRIVING',
show_markers=True, # Show start/end markers
show_route=True, # Show route line
stroke_color='purple', # Custom color
stroke_weight=8.0, # Thick line
stroke_opacity=0.9, # High opacity
avoid_highways=True # Route preference
)
fig.add_layer(directions_layer)
figimport gmaps
gmaps.configure(api_key="YOUR_API_KEY")
try:
# Invalid coordinates might cause an error
start = (0, 0) # Middle of ocean
end = (90, 180) # Invalid location
fig = gmaps.figure()
directions_layer = gmaps.directions_layer(start, end)
fig.add_layer(directions_layer)
fig
except gmaps.DirectionsServiceException as e:
print(f"Directions error: {e}")
# Handle error appropriatelyInstall with Tessl CLI
npx tessl i tessl/pypi-gmaps