File size: 2,646 Bytes
f573549
 
3a270e4
f573549
 
f49e985
 
f573549
 
 
 
 
 
 
 
 
 
 
 
 
3a270e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f573549
 
 
 
 
 
 
 
 
3a270e4
 
f573549
 
 
 
 
 
 
 
3a270e4
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import googlemaps
import streamlit as st
import json
from datetime import datetime

key=os.getenv('GOOGLE_KEY')
gmaps = googlemaps.Client(key)

def get_directions(source, destination):
    now = datetime.now()
    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

# Function to generate the Google Map HTML
def generate_map(source, destination):
    map_code = f"""
    <div id="map" style="height: 400px; width: 100%;"></div>
    <script>
      function initMap() {{
        var directionsService = new google.maps.DirectionsService();
        var directionsRenderer = new google.maps.DirectionsRenderer();
        var map = new google.maps.Map(document.getElementById('map'), {{
          zoom: 7,
          center: {{lat: 41.85, lng: -87.65}}
        }});
        directionsRenderer.setMap(map);

        var request = {{
          origin: '{source}',
          destination: '{destination}',
          travelMode: 'DRIVING'
        }};
        directionsService.route(request, function(result, status) {{
          if (status == 'OK') {{
            directionsRenderer.setDirections(result);
          }}
        }});
      }}
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=Your-Google-Maps-API-Key&callback=initMap" async defer></script>
    """
    return map_code

# Streamlit App
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)

    # Displaying the directions in text
    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 Directions on Map Button
    if st.sidebar.button('Show Directions on Map'):
        # Embed the Google Map HTML
        map_code = generate_map(source_location, destination_location)
        st.markdown(map_code, unsafe_allow_html=True)