|
import streamlit as st |
|
from streamlit_folium import folium_static |
|
import googlemaps |
|
import folium |
|
import os |
|
from datetime import datetime |
|
|
|
def get_directions(source, destination): |
|
now = datetime.now() |
|
directions_info = {} |
|
modes = ['driving', 'walking', 'bicycling', 'transit'] |
|
for mode in modes: |
|
directions_result = gmaps.directions(source, destination, mode=mode, departure_time=now) |
|
if directions_result: |
|
directions_info[mode] = directions_result[0]['legs'][0]['steps'] |
|
else: |
|
directions_info[mode] = "No available routes." |
|
return directions_info |
|
|
|
def render_folium_map(source, destination): |
|
source_location = gmaps.geocode(source) |
|
destination_location = gmaps.geocode(destination) |
|
|
|
source_coords = source_location[0]['geometry']['location'] |
|
dest_coords = destination_location[0]['geometry']['location'] |
|
|
|
m = folium.Map(location=[source_coords['lat'], source_coords['lng']], zoom_start=10) |
|
|
|
folium.Marker([source_coords['lat'], source_coords['lng']], tooltip='Source').add_to(m) |
|
folium.Marker([dest_coords['lat'], dest_coords['lng']], tooltip='Destination').add_to(m) |
|
|
|
folium_static(m) |
|
|
|
|
|
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_KEY')) |
|
|
|
|
|
st.title('πΊοΈ Google Maps Directions') |
|
st.sidebar.header('π User Input Features') |
|
|
|
source_location = st.sidebar.text_input("Source Location", "Mound, MN") |
|
destination_location = st.sidebar.text_input("Destination Location", "Minneapolis, MN") |
|
|
|
if st.sidebar.button('Get Directions'): |
|
directions_info = get_directions(source_location, destination_location) |
|
for mode, directions in directions_info.items(): |
|
st.write(f"## Directions by {mode.capitalize()} π£οΈ") |
|
if directions == "No available routes.": |
|
st.write("β " + directions) |
|
else: |
|
for i, step in enumerate(directions): |
|
st.write(f"π£ {i + 1}. {step['html_instructions']}") |
|
|
|
show_map_button = st.button('Show Directions on Map πΊοΈ') |
|
if show_map_button: |
|
render_folium_map(source_location, destination_location) |
|
|