awacke1 commited on
Commit
d64aee9
Β·
1 Parent(s): 91a2678

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -42
app.py CHANGED
@@ -1,56 +1,74 @@
1
  import streamlit as st
 
 
2
  from streamlit_folium import folium_static
3
  import googlemaps
4
- import folium
5
- import os
6
  from datetime import datetime
7
 
8
- def get_directions(source, destination):
9
- now = datetime.now()
10
- directions_info = {}
11
- modes = ['driving', 'walking', 'bicycling', 'transit']
12
- for mode in modes:
13
- directions_result = gmaps.directions(source, destination, mode=mode, departure_time=now)
14
- if directions_result:
15
- directions_info[mode] = directions_result[0]['legs'][0]['steps']
16
- else:
17
- directions_info[mode] = "No available routes."
18
- return directions_info
19
-
20
- def render_folium_map(source, destination):
21
- source_location = gmaps.geocode(source)
22
- destination_location = gmaps.geocode(destination)
23
-
24
- source_coords = source_location[0]['geometry']['location']
25
- dest_coords = destination_location[0]['geometry']['location']
26
-
27
- m = folium.Map(location=[source_coords['lat'], source_coords['lng']], zoom_start=10)
28
-
29
- folium.Marker([source_coords['lat'], source_coords['lng']], tooltip='Source').add_to(m)
30
- folium.Marker([dest_coords['lat'], dest_coords['lng']], tooltip='Destination').add_to(m)
31
-
32
- folium_static(m)
33
-
34
  # Initialize Google Maps
35
  gmaps = googlemaps.Client(key=os.getenv('GOOGLE_KEY'))
36
 
37
- # Streamlit app
38
- st.title('πŸ—ΊοΈ Google Maps Directions')
39
- st.sidebar.header('πŸ“Œ User Input Features')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  source_location = st.sidebar.text_input("Source Location", "Mound, MN")
42
  destination_location = st.sidebar.text_input("Destination Location", "Minneapolis, MN")
43
 
44
  if st.sidebar.button('Get Directions'):
45
- directions_info = get_directions(source_location, destination_location)
46
- for mode, directions in directions_info.items():
47
- st.write(f"## Directions by {mode.capitalize()} πŸ›£οΈ")
48
- if directions == "No available routes.":
49
- st.write("❌ " + directions)
50
- else:
51
- for i, step in enumerate(directions):
52
- st.write(f"πŸ‘£ {i + 1}. {step['html_instructions']}")
 
 
53
 
54
- show_map_button = st.button('Show Directions on Map πŸ—ΊοΈ')
55
- if show_map_button:
56
- render_folium_map(source_location, destination_location)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import folium
3
+ from folium.plugins import MarkerCluster
4
  from streamlit_folium import folium_static
5
  import googlemaps
 
 
6
  from datetime import datetime
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Initialize Google Maps
9
  gmaps = googlemaps.Client(key=os.getenv('GOOGLE_KEY'))
10
 
11
+ # Function to fetch directions
12
+ def get_directions_and_coords(source, destination):
13
+ now = datetime.now()
14
+ directions_info = gmaps.directions(source, destination, mode='driving', departure_time=now)
15
+ if directions_info:
16
+ steps = directions_info[0]['legs'][0]['steps']
17
+ coords = [(step['start_location']['lat'], step['start_location']['lng']) for step in steps]
18
+ return steps, coords
19
+ else:
20
+ return None, None
21
+
22
+ # Function to render map with directions
23
+ def render_folium_map(coords):
24
+ m = folium.Map(location=[coords[0][0], coords[0][1]], zoom_start=13)
25
+ folium.PolyLine(coords, color="blue", weight=2.5, opacity=1).add_to(m)
26
+ return m
27
+
28
+ # Streamlit UI
29
+ st.title('Google Maps and Minnesota Medical Centers')
30
+ st.sidebar.header('Directions')
31
 
32
  source_location = st.sidebar.text_input("Source Location", "Mound, MN")
33
  destination_location = st.sidebar.text_input("Destination Location", "Minneapolis, MN")
34
 
35
  if st.sidebar.button('Get Directions'):
36
+ steps, coords = get_directions_and_coords(source_location, destination_location)
37
+ if steps and coords:
38
+ st.subheader('Driving Directions:')
39
+ for i, step in enumerate(steps):
40
+ st.write(f"{i+1}. {step['html_instructions']}")
41
+ st.subheader('Route on Map:')
42
+ m1 = render_folium_map(coords)
43
+ folium_static(m1)
44
+ else:
45
+ st.write("No available routes.")
46
 
47
+ # The existing code for Minnesota medical centers
48
+ st.markdown("## πŸ₯ Minnesota Medical Centers 🌳")
49
+ m2 = folium.Map(location=[45.6945, -93.9002], zoom_start=6)
50
+ marker_cluster = MarkerCluster().add_to(m2)
51
+ # Define Minnesota medical centers data
52
+ minnesota_med_centers = [
53
+ ('Mayo Clinic', 44.0224, -92.4658, 'General medical and surgical', 'Rochester'),
54
+ ('University of Minnesota Medical Center', 44.9721, -93.2595, 'Teaching hospital', 'Minneapolis'),
55
+ ('Abbott Northwestern Hospital', 44.9526, -93.2622, 'Heart specialty', 'Minneapolis'),
56
+ ('Regions Hospital', 44.9558, -93.0942, 'Trauma center', 'St. Paul'),
57
+ ('St. Cloud Hospital', 45.5671, -94.1989, 'Multiple specialties', 'St. Cloud'),
58
+ ('Park Nicollet Methodist Hospital', 44.9304, -93.3640, 'General medical and surgical', 'St. Louis Park'),
59
+ ('Fairview Ridges Hospital', 44.7391, -93.2777, 'Community hospital', 'Burnsville'),
60
+ ('Mercy Hospital', 45.1761, -93.3099, 'Acute care', 'Coon Rapids'),
61
+ ('North Memorial Health Hospital', 45.0131, -93.3246, 'General medical and surgical', 'Robbinsdale'),
62
+ ('Essentia Health-Duluth', 46.7860, -92.1011, 'Community hospital', 'Duluth'),
63
+ ('M Health Fairview Southdale Hospital', 44.8806, -93.3241, 'Community hospital', 'Edina'),
64
+ ('Woodwinds Health Campus', 44.9272, -92.9689, 'Community hospital', 'Woodbury'),
65
+ ('United Hospital', 44.9460, -93.1052, 'Acute care', 'St. Paul'),
66
+ ('Buffalo Hospital', 45.1831, -93.8772, 'Community hospital', 'Buffalo'),
67
+ ('Maple Grove Hospital', 45.1206, -93.4790, 'Community hospital', 'Maple Grove'),
68
+ ('Olmsted Medical Center', 44.0234, -92.4610, 'General medical and surgical', 'Rochester'),
69
+ ('Hennepin Healthcare', 44.9738, -93.2605, 'Teaching hospital', 'Minneapolis'),
70
+ ('St. Francis Regional Medical Center', 44.7658, -93.5143, 'Community hospital', 'Shakopee'),
71
+ ('Lakeview Hospital', 45.0422, -92.8137, 'Community hospital', 'Stillwater'),
72
+ ('St. Luke\'s Hospital', 46.7831, -92.1043, 'Community hospital', 'Duluth')
73
+ ]
74
+ folium_static(m2)