awacke1 commited on
Commit
66ab386
1 Parent(s): d3854a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -36
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
- import streamlit.components.v1 as components
3
  import googlemaps
 
4
  import os
5
  from datetime import datetime
6
 
@@ -16,40 +17,19 @@ def get_directions(source, destination):
16
  directions_info[mode] = "No available routes."
17
  return directions_info
18
 
19
- def show_map(source, destination):
20
- html_code = f"""
21
- <html>
22
- <head>
23
- <script src="https://maps.googleapis.com/maps/api/js?key={os.getenv('GOOGLE_KEY')}&callback=initMap" async defer></script>
24
- <script>
25
- var map;
26
- function initMap() {{
27
- var directionsService = new google.maps.DirectionsService();
28
- var directionsRenderer = new google.maps.DirectionsRenderer();
29
- map = new google.maps.Map(document.getElementById('map'), {{
30
- zoom: 7,
31
- center: {{lat: 41.85, lng: -87.65}}
32
- }});
33
- directionsRenderer.setMap(map);
34
- var request = {{
35
- origin: '{source}',
36
- destination: '{destination}',
37
- travelMode: 'DRIVING'
38
- }};
39
- directionsService.route(request, function(result, status) {{
40
- if (status == 'OK') {{
41
- directionsRenderer.setDirections(result);
42
- }}
43
- }});
44
- }}
45
- </script>
46
- </head>
47
- <body onload="initMap()">
48
- <div id="map" style="height: 500px;"></div>
49
- </body>
50
- </html>
51
- """
52
- components.html(html_code, height=600, scrolling=False)
53
 
54
  # Initialize Google Maps
55
  gmaps = googlemaps.Client(key=os.getenv('GOOGLE_KEY'))
@@ -73,4 +53,4 @@ if st.sidebar.button('Get Directions'):
73
 
74
  show_map_button = st.button('Show Directions on Map 🗺️')
75
  if show_map_button:
76
- show_map(source_location, destination_location)
 
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
 
 
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'))
 
53
 
54
  show_map_button = st.button('Show Directions on Map 🗺️')
55
  if show_map_button:
56
+ render_folium_map(source_location, destination_location)