File size: 1,138 Bytes
49d4f7c
 
7b57927
49d4f7c
f543551
 
7b57927
f543551
7b57927
 
f543551
7b57927
 
 
f543551
7b57927
 
 
 
 
 
 
 
f543551
 
 
 
 
 
7b57927
 
 
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
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!")