Rahulk2197 commited on
Commit
7b57927
Β·
verified Β·
1 Parent(s): e173a16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -36
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
- # Inject JavaScript to get location from the browser
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
- # Hidden input field to store coordinates
23
- st.markdown('<input type="hidden" id="location-data">', unsafe_allow_html=True)
24
 
25
- # Create a form to capture location data
26
- with st.form("location_form"):
27
- location = st.text_input("Your Coordinates", key="location", disabled=True)
28
- submitted = st.form_submit_button("Submit") # Fixed: Removed 'key' argument
29
 
30
- # Run the JavaScript when the user clicks the button
31
- st.markdown(get_location_js, unsafe_allow_html=True)
 
 
 
 
 
 
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
+