Spaces:
Runtime error
Runtime error
File size: 2,797 Bytes
7367deb 2eabf34 7367deb 2eabf34 | 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 | import streamlit as st
from streamlit_folium import st_folium
import folium
import requests
from app.hooks.use_distance import haversine, estimate_time
from app.hooks.use_alert import check_alert
from app.components.bus_stop_search import bus_stop_search_ui
from streamlit_autorefresh import st_autorefresh
# --- ืงืื ืคืื ---
IP_GEO_URL = "http://ip-api.com/json"
API_URL = "http://127.0.0.1:8000/location" # ืืคืฉืจ ืืฉืืื ืืฉืจืช
# --- Streamlit Setup ---
st.set_page_config(page_title="๐ Bus Stop Alert", layout="wide")
st.title("๐ Bus Stop Alert")
st.write("Welcome! Enter a destination and start tracking your route. ๐")
# --- Destination ---
destination_coords = bus_stop_search_ui()
if not destination_coords:
st.info("Please enter a destination to start tracking.")
st.stop()
else:
st.success(f"โ
Destination set at: {destination_coords[0]:.6f}, {destination_coords[1]:.6f}")
# --- ืขืืืื ืืืืืืื ืื 15 ืฉื ืืืช ---
st_autorefresh(interval=15000, limit=None, key="gps_refresh") # 15,000ms = 15 ืฉื ืืืช
# --- ืคืื ืงืฆืื ืืงืืืช ืืืงืื ืืคื IP ---
def get_location_from_ip():
try:
res = requests.get(IP_GEO_URL, timeout=5)
data = res.json()
if data.get("status") == "success":
lat, lon = data["lat"], data["lon"]
# ืืคืฉืจ ืืฉืืื ืืฉืจืช
try:
requests.post(API_URL, json={"lat": lat, "lon": lon}, timeout=3)
except:
pass
return (lat, lon)
except:
pass
# fallback Tel Aviv ืื ื ืืฉื
return (32.0853, 34.7818)
# --- ืงืืืช ืืืงืื ื ืืืื ---
user_coords = get_location_from_ip()
st.success(f"๐ Your current location (IP-based): {user_coords[0]:.6f}, {user_coords[1]:.6f}")
# --- Map ---
m = folium.Map(location=user_coords, zoom_start=13)
folium.Marker(user_coords, tooltip="You are here", icon=folium.Icon(color="blue")).add_to(m)
folium.Marker(destination_coords, tooltip="Destination", icon=folium.Icon(color="red")).add_to(m)
folium.PolyLine([user_coords, destination_coords], color="green", weight=3, opacity=0.7).add_to(m)
st_folium(m, width=700, height=500)
# --- Distance & Time ---
distance_km = haversine(user_coords, destination_coords)
st.metric("Distance to destination", f"{distance_km:.2f} km")
speed_kmh = st.number_input("Estimated speed (km/h)", value=30.0, min_value=1.0)
time_min = estimate_time(distance_km, speed_kmh)
st.metric("Estimated travel time", f"{time_min:.1f} min")
# --- Alert ---
arrived = check_alert(time_min)
if arrived:
st.balloons()
st.success("๐ Congratulations! You have arrived at your destination.")
else:
st.info("Tracking in progress... Keep moving towards your destination! ๐ถโโ๏ธ๐")
|