Spaces:
Sleeping
Sleeping
import streamlit as st | |
import folium | |
from folium.plugins import MarkerCluster | |
from streamlit_folium import st_folium # Change here: import st_folium instead of folium_static | |
import googlemaps | |
from datetime import datetime | |
import os | |
# Initialize Google Maps API client securely | |
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_API_KEY')) # Fetch API key from Hugging Face secrets | |
# 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.PolyLine(coords, color='red').add_to(m) | |
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) | |
# Function to search nearby medical centers using Google Places API | |
def search_medical_centers(query, location, radius=10000): | |
# Search for places like hospitals or medical centers nearby | |
places_result = gmaps.places_nearby(location, radius=radius, type='hospital', keyword=query) | |
return places_result.get('results', []) | |
# Driving Directions Sidebar | |
st.sidebar.header('Directions π') | |
source_location = st.sidebar.text_input("Source Location", "Honolulu, HI") | |
destination_location = st.sidebar.text_input("Destination Location", "Maui, HI") | |
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) | |
st_folium(m1) # Changed here to use st_folium instead of folium_static | |
else: | |
st.write("No available routes.") | |
# Medical Center Search Section | |
st.markdown("### π₯ Search for Medical Centers in Hawaii πΊοΈ") | |
medical_center_query = st.text_input("Enter Medical Center Name or Specialty", "Hawaii Medical Center") | |
location_input = st.text_input("Enter your location (within Hawaii)", "Honolulu, HI") | |
if location_input and medical_center_query: | |
# Fetch the coordinates of the source location using Google Geocoding | |
geocode_result = gmaps.geocode(location_input) | |
if geocode_result: | |
location_coords = geocode_result[0]['geometry']['location'] | |
lat, lon = location_coords['lat'], location_coords['lng'] | |
# Fetch nearby medical centers in Hawaii | |
medical_centers = search_medical_centers(medical_center_query, (lat, lon)) | |
if medical_centers: | |
st.subheader('Found Medical Centers:') | |
for center in medical_centers: | |
name = center['name'] | |
vicinity = center.get('vicinity', 'N/A') | |
rating = center.get('rating', 'N/A') | |
st.write(f"**{name}** - {vicinity} - Rating: {rating}") | |
# Initialize the map object here before adding markers | |
if 'm2' not in locals(): | |
m2 = folium.Map(location=[lat, lon], zoom_start=13) | |
# Add marker for each medical center on map | |
folium.Marker([center['geometry']['location']['lat'], center['geometry']['location']['lng']], | |
popup=f"{name}\n{vicinity}\nRating: {rating}").add_to(m2) | |
# Render the map with medical centers | |
st_folium(m2) # Changed here to use st_folium instead of folium_static | |
else: | |
st.write("No medical centers found matching your query.") | |
else: | |
st.write("Could not retrieve location coordinates.") | |