awacke1's picture
Update backupapp.py
d3854a7
raw
history blame
No virus
2.86 kB
gmaps = googlemaps.Client(key='AIzaSyDybq2mxujekZVivmr03Y5-GGHXesn4TLI')
import streamlit as st
import streamlit.components.v1 as components
import googlemaps
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 show_map(source, destination):
html_code = f"""
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?key={os.getenv('GOOGLE_KEY')}&callback=initMap" async defer></script>
<script>
var map;
function initMap() {{
var directionsService = new google.maps.DirectionsService();
var directionsRenderer = new google.maps.DirectionsRenderer();
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>
</head>
<body onload="initMap()">
<div id="map" style="height: 500px;"></div>
</body>
</html>
"""
components.html(html_code, height=600, scrolling=False)
# Initialize Google Maps
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_KEY'))
# 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)
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:
show_map(source_location, destination_location)