File size: 3,569 Bytes
f573549
d64aee9
0aeb981
66ab386
03a899a
f573549
6aa52db
f573549
03a899a
 
 
d64aee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fa2633
 
 
 
 
 
0aeb981
0fa2633
 
 
 
 
 
 
 
 
 
 
d64aee9
0aeb981
 
f573549
 
 
 
 
d64aee9
 
0aeb981
d64aee9
 
0aeb981
d64aee9
 
 
 
f573549
d64aee9
 
 
0fa2633
d64aee9
 
0aeb981
d64aee9
0fa2633
 
 
0aeb981
 
 
 
 
 
 
 
 
 
 
 
 
d64aee9
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streamlit as st
import folium
from folium.plugins import MarkerCluster, Search
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, icon=folium.Icon(color="red")).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 Minnesota Medical Centers πŸ—ΊοΈ')
st.sidebar.header('Directions πŸš—')

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'):
    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.")

st.markdown("## πŸ₯ Minnesota Medical Centers 🌳")
m2 = folium.Map(location=[45.6945, -93.9002], zoom_start=6)
marker_cluster = MarkerCluster().add_to(m2)

# Define Minnesota medical centers data
minnesota_med_centers = [
    # ... (your existing medical centers)
]

# Annotate distances and paths for each medical center
add_medical_center_paths(m2, source_location, minnesota_med_centers)

# Adding a Search bar to the map
geolocator = Nominatim(user_agent="geoapiExercises")
search = Search(layer=marker_cluster, geom_type='Point').add_to(m2)

# Add a layer control
folium.LayerControl().add_to(m2)

# Points of Interest around medical centers
for name, lat, lon, specialty, city in minnesota_med_centers:
    folium.Marker([lat+0.01, lon], icon=folium.Icon(color="blue", icon="cloud"), popup="Park").add_to(m2)
    folium.Marker([lat-0.01, lon], icon=folium.Icon(color="green", icon="leaf"), popup="Cafe").add_to(m2)

folium_static(m2)