Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import googlemaps
|
2 |
import streamlit as st
|
|
|
3 |
from datetime import datetime
|
4 |
|
5 |
key=os.getenv('GOOGLE_KEY')
|
@@ -7,7 +8,6 @@ gmaps = googlemaps.Client(key)
|
|
7 |
|
8 |
def get_directions(source, destination):
|
9 |
now = datetime.now()
|
10 |
-
# Get all modes: driving, walking, bicycling, and transit
|
11 |
modes = ['driving', 'walking', 'bicycling', 'transit']
|
12 |
directions_info = {}
|
13 |
for mode in modes:
|
@@ -18,18 +18,47 @@ def get_directions(source, destination):
|
|
18 |
directions_info[mode] = "No available routes."
|
19 |
return directions_info
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# Streamlit App
|
22 |
st.title("🗺️ Google Maps Directions")
|
23 |
st.sidebar.header('User Input Features')
|
24 |
|
25 |
-
# Input for source and destination
|
26 |
source_location = st.sidebar.text_input("Source Location", "Mound, MN")
|
27 |
destination_location = st.sidebar.text_input("Destination Location", "Minneapolis, MN")
|
28 |
|
29 |
if st.sidebar.button('Get Directions'):
|
30 |
directions_info = get_directions(source_location, destination_location)
|
31 |
-
|
32 |
-
# Displaying the directions
|
33 |
for mode, directions in directions_info.items():
|
34 |
st.write(f"## Directions by {mode.capitalize()}")
|
35 |
if directions == "No available routes.":
|
@@ -38,3 +67,8 @@ if st.sidebar.button('Get Directions'):
|
|
38 |
for i, step in enumerate(directions):
|
39 |
st.write(f"{i+1}. {step['html_instructions']}")
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import googlemaps
|
2 |
import streamlit as st
|
3 |
+
import json
|
4 |
from datetime import datetime
|
5 |
|
6 |
key=os.getenv('GOOGLE_KEY')
|
|
|
8 |
|
9 |
def get_directions(source, destination):
|
10 |
now = datetime.now()
|
|
|
11 |
modes = ['driving', 'walking', 'bicycling', 'transit']
|
12 |
directions_info = {}
|
13 |
for mode in modes:
|
|
|
18 |
directions_info[mode] = "No available routes."
|
19 |
return directions_info
|
20 |
|
21 |
+
# Function to generate the Google Map HTML
|
22 |
+
def generate_map(source, destination):
|
23 |
+
map_code = f"""
|
24 |
+
<div id="map" style="height: 400px; width: 100%;"></div>
|
25 |
+
<script>
|
26 |
+
function initMap() {{
|
27 |
+
var directionsService = new google.maps.DirectionsService();
|
28 |
+
var directionsRenderer = new google.maps.DirectionsRenderer();
|
29 |
+
var map = new google.maps.Map(document.getElementById('map'), {{
|
30 |
+
zoom: 7,
|
31 |
+
center: {{lat: 41.85, lng: -87.65}}
|
32 |
+
}});
|
33 |
+
directionsRenderer.setMap(map);
|
34 |
+
|
35 |
+
var request = {{
|
36 |
+
origin: '{source}',
|
37 |
+
destination: '{destination}',
|
38 |
+
travelMode: 'DRIVING'
|
39 |
+
}};
|
40 |
+
directionsService.route(request, function(result, status) {{
|
41 |
+
if (status == 'OK') {{
|
42 |
+
directionsRenderer.setDirections(result);
|
43 |
+
}}
|
44 |
+
}});
|
45 |
+
}}
|
46 |
+
</script>
|
47 |
+
<script src="https://maps.googleapis.com/maps/api/js?key=Your-Google-Maps-API-Key&callback=initMap" async defer></script>
|
48 |
+
"""
|
49 |
+
return map_code
|
50 |
+
|
51 |
# Streamlit App
|
52 |
st.title("🗺️ Google Maps Directions")
|
53 |
st.sidebar.header('User Input Features')
|
54 |
|
|
|
55 |
source_location = st.sidebar.text_input("Source Location", "Mound, MN")
|
56 |
destination_location = st.sidebar.text_input("Destination Location", "Minneapolis, MN")
|
57 |
|
58 |
if st.sidebar.button('Get Directions'):
|
59 |
directions_info = get_directions(source_location, destination_location)
|
60 |
+
|
61 |
+
# Displaying the directions in text
|
62 |
for mode, directions in directions_info.items():
|
63 |
st.write(f"## Directions by {mode.capitalize()}")
|
64 |
if directions == "No available routes.":
|
|
|
67 |
for i, step in enumerate(directions):
|
68 |
st.write(f"{i+1}. {step['html_instructions']}")
|
69 |
|
70 |
+
# Show Directions on Map Button
|
71 |
+
if st.sidebar.button('Show Directions on Map'):
|
72 |
+
# Embed the Google Map HTML
|
73 |
+
map_code = generate_map(source_location, destination_location)
|
74 |
+
st.markdown(map_code, unsafe_allow_html=True)
|