File size: 1,556 Bytes
7d1cb79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import googlemaps
import streamlit as st
from datetime import datetime

# Replace your Google Maps API key here
#gmaps = googlemaps.Client(key='Your-Google-Maps-API-Key')
gmaps = googlemaps.Client(key='AIzaSyDybq2mxujekZVivmr03Y5-GGHXesn4TLI')

def get_directions(source, destination):
    now = datetime.now()
    # Get all modes: driving, walking, bicycling, and transit
    modes = ['driving', 'walking', 'bicycling', 'transit']
    directions_info = {}
    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

# Streamlit App
st.title("πŸ—ΊοΈ Google Maps Directions")
st.sidebar.header('User Input Features')

# Input for source and destination
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)
    
    # Displaying the directions
    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']}")