Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,32 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
st.title("π Get Your Device Location")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
get_location_js = """
|
| 8 |
-
<script>
|
| 9 |
-
navigator.geolocation.getCurrentPosition(
|
| 10 |
-
(position) => {
|
| 11 |
-
const coords = position.coords.latitude + "," + position.coords.longitude;
|
| 12 |
-
document.getElementById("location-data").value = coords;
|
| 13 |
-
document.getElementById("submit-btn").click();
|
| 14 |
-
},
|
| 15 |
-
(error) => {
|
| 16 |
-
alert("Geolocation not available. Please allow location access.");
|
| 17 |
-
}
|
| 18 |
-
);
|
| 19 |
-
</script>
|
| 20 |
-
"""
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
submitted = st.form_submit_button("Submit") # Fixed: Removed 'key' argument
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
# Reverse Geocoding using OpenStreetMap (Nominatim API)
|
| 34 |
-
def get_address(lat, lon):
|
| 35 |
-
url = f"https://nominatim.openstreetmap.org/reverse?format=json&lat={lat}&lon={lon}"
|
| 36 |
-
response = requests.get(url)
|
| 37 |
-
if response.status_code == 200:
|
| 38 |
-
data = response.json()
|
| 39 |
-
return data.get("display_name", "Address not found")
|
| 40 |
-
return "Failed to fetch address"
|
| 41 |
-
|
| 42 |
-
# Display the location details
|
| 43 |
-
if submitted and location:
|
| 44 |
-
lat, lon = map(str.strip, location.split(","))
|
| 45 |
address = get_address(lat, lon)
|
| 46 |
|
| 47 |
-
st.success("Your Location Details:")
|
| 48 |
st.write(f"**Latitude:** {lat}")
|
| 49 |
st.write(f"**Longitude:** {lon}")
|
| 50 |
st.write(f"**Address:** {address}")
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
+
from streamlit_javascript import st_js_eval
|
| 4 |
|
| 5 |
st.title("π Get Your Device Location")
|
| 6 |
|
| 7 |
+
st.write("Click the button below to get your **real device location** (not server location).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Use JavaScript to fetch location
|
| 10 |
+
location = st_js_eval(js_expressions="navigator.geolocation.getCurrentPosition((pos) => pos.coords.latitude + ',' + pos.coords.longitude)", key="get_location")
|
| 11 |
|
| 12 |
+
if location:
|
| 13 |
+
st.success("β
Location Retrieved!")
|
| 14 |
+
lat, lon = location.split(",")
|
|
|
|
| 15 |
|
| 16 |
+
# Reverse Geocoding using OpenStreetMap (Nominatim API)
|
| 17 |
+
def get_address(lat, lon):
|
| 18 |
+
url = f"https://nominatim.openstreetmap.org/reverse?format=json&lat={lat}&lon={lon}"
|
| 19 |
+
response = requests.get(url)
|
| 20 |
+
if response.status_code == 200:
|
| 21 |
+
data = response.json()
|
| 22 |
+
return data.get("display_name", "Address not found")
|
| 23 |
+
return "Failed to fetch address"
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
address = get_address(lat, lon)
|
| 26 |
|
|
|
|
| 27 |
st.write(f"**Latitude:** {lat}")
|
| 28 |
st.write(f"**Longitude:** {lon}")
|
| 29 |
st.write(f"**Address:** {address}")
|
| 30 |
+
else:
|
| 31 |
+
st.warning("Click the button and allow location access!")
|
| 32 |
+
|