Spaces:
Runtime error
Runtime error
import streamlit as st | |
import folium | |
from folium.plugins import MarkerCluster | |
from streamlit_folium import folium_static | |
import googlemaps | |
from datetime import datetime | |
import os | |
# Initialize Google Maps | |
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_KEY')) | |
# Function to fetch directions | |
def get_directions_and_coords(source, destination): | |
now = datetime.now() | |
directions_info = gmaps.directions(source, destination, mode='driving', departure_time=now) | |
if directions_info: | |
steps = directions_info[0]['legs'][0]['steps'] | |
coords = [(step['start_location']['lat'], step['start_location']['lng']) for step in steps] | |
return steps, coords | |
else: | |
return None, None | |
# Function to render map with directions | |
def render_folium_map(coords): | |
m = folium.Map(location=[coords[0][0], coords[0][1]], zoom_start=13) | |
folium.PolyLine(coords, color="blue", weight=2.5, opacity=1).add_to(m) | |
return m | |
# Function to add medical center paths and annotate distance | |
def add_medical_center_paths(m, source, med_centers): | |
for name, lat, lon, specialty, city in med_centers: | |
_, coords = get_directions_and_coords(source, (lat, lon)) | |
if coords: | |
folium.PolyLine(coords, color="red", weight=2.5, opacity=1).add_to(m) | |
folium.Marker([lat, lon], popup=name).add_to(m) | |
distance_info = gmaps.distance_matrix(source, (lat, lon), mode='driving') | |
distance = distance_info['rows'][0]['elements'][0]['distance']['text'] | |
folium.map.Marker( | |
[coords[-1][0], coords[-1][1]], | |
icon=folium.DivIcon( | |
icon_size=(150, 36), | |
icon_anchor=(0, 0), | |
html=f'<div style="font-size: 10pt; color : red;">{distance}</div>', | |
) | |
).add_to(m) | |
# Streamlit UI | |
st.title('Google Maps and Washington Medical Centers π²') | |
st.sidebar.header('Directions') | |
source_location = st.sidebar.text_input("Source Location", "Space Needle, Seattle, WA") | |
destination_location = st.sidebar.text_input("Destination Location", "Seattle-Tacoma International Airport, WA") | |
if st.sidebar.button('Get Directions'): | |
steps, coords = get_directions_and_coords(source_location, destination_location) | |
if steps and coords: | |
st.subheader('Driving Directions:') | |
for i, step in enumerate(steps): | |
st.write(f"{i+1}. {step['html_instructions']}") | |
st.subheader('Route on Map:') | |
m1 = render_folium_map(coords) | |
folium_static(m1) | |
else: | |
st.write("No available routes.") | |
# Top 10 medical centers in Washington | |
washington_med_centers = [ | |
('University of Washington Medical Center', 47.6496, -122.3096, 'Teaching hospital', 'Seattle'), | |
('Virginia Mason Hospital and Seattle Medical Center', 47.6097, -122.3265, 'General medical and surgical', 'Seattle'), | |
('Swedish First Hill Campus', 47.6092, -122.3244, 'General medical and surgical', 'Seattle'), | |
('Providence Regional Medical Center Everett', 47.9770, -122.2015, 'Community hospital', 'Everett'), | |
('Overlake Medical Center', 47.6153, -122.2039, 'General medical and surgical', 'Bellevue'), | |
('St. Joseph Medical Center', 47.2442, -122.4358, 'Community hospital', 'Tacoma'), | |
('Kadlec Regional Medical Center', 46.2566, -119.2833, 'Community hospital', 'Richland'), | |
('MultiCare Tacoma General Hospital', 47.2590, -122.4543, 'General medical and surgical', 'Tacoma'), | |
('EvergreenHealth Medical Center', 47.7154, -122.1786, 'Community hospital', 'Kirkland'), | |
('Swedish Issaquah Campus', 47.5659, -122.0136, 'Community hospital', 'Issaquah') | |
] | |
# Annotate distances and paths for each medical center | |
st.markdown("## π₯ Washington Medical Centers π²") | |
m2 = folium.Map(location=[47.6062, -122.3321], zoom_start=6) | |
marker_cluster = MarkerCluster().add_to(m2) | |
add_medical_center_paths(m2, source_location, washington_med_centers) | |
folium_static(m2) | |