location / app.py
Rahulk2197's picture
Update app.py
7b57927 verified
import streamlit as st
import requests
from streamlit_javascript import st_js_eval
st.title("πŸ“ Get Your Device Location")
st.write("Click the button below to get your **real device location** (not server location).")
# Use JavaScript to fetch location
location = st_js_eval(js_expressions="navigator.geolocation.getCurrentPosition((pos) => pos.coords.latitude + ',' + pos.coords.longitude)", key="get_location")
if location:
st.success("βœ… Location Retrieved!")
lat, lon = location.split(",")
# Reverse Geocoding using OpenStreetMap (Nominatim API)
def get_address(lat, lon):
url = f"https://nominatim.openstreetmap.org/reverse?format=json&lat={lat}&lon={lon}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data.get("display_name", "Address not found")
return "Failed to fetch address"
address = get_address(lat, lon)
st.write(f"**Latitude:** {lat}")
st.write(f"**Longitude:** {lon}")
st.write(f"**Address:** {address}")
else:
st.warning("Click the button and allow location access!")