|
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 |
|
|
|
|
|
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_KEY')) |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
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) |
|
|
|
|
|
st.title('Google Maps and Massachusetts Medical Centers π') |
|
st.sidebar.header('Directions') |
|
|
|
source_location = st.sidebar.text_input("Source Location", "Logan International Airport, Boston, MA") |
|
destination_location = st.sidebar.text_input("Destination Location", "Boston Harbor, MA") |
|
|
|
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.") |
|
|
|
|
|
massachusetts_med_centers = [ |
|
('Massachusetts General Hospital', 42.3626, -71.0704, 'Teaching hospital', 'Boston'), |
|
('Brigham and Women\'s Hospital', 42.3356, -71.1077, 'Teaching hospital', 'Boston'), |
|
('Dana-Farber Cancer Institute', 42.3371, -71.1081, 'Cancer specialty', 'Boston'), |
|
('Beth Israel Deaconess Medical Center', 42.3394, -71.1043, 'Teaching hospital', 'Boston'), |
|
('Boston Children\'s Hospital', 42.3671, -71.1077, 'Pediatrics', 'Boston'), |
|
('Tufts Medical Center', 42.3495, -71.0634, 'Teaching hospital', 'Boston'), |
|
('Lahey Hospital and Medical Center', 42.5106, -71.2187, 'Community hospital', 'Burlington'), |
|
('Baystate Medical Center', 42.1208, -72.6051, 'Teaching hospital', 'Springfield'), |
|
('Newton-Wellesley Hospital', 42.3304, -71.2436, 'Community hospital', 'Newton'), |
|
('South Shore Hospital', 42.1751, -70.9545, 'Community hospital', 'South Weymouth') |
|
] |
|
|
|
|
|
st.markdown("## π₯ Massachusetts Medical Centers π") |
|
m2 = folium.Map(location=[42.3601, -71.0589], zoom_start=6) |
|
marker_cluster = MarkerCluster().add_to(m2) |
|
add_medical_center_paths(m2, source_location, massachusetts_med_centers) |
|
folium_static(m2) |
|
|