dn-scribe commited on
Commit
26044b0
·
unverified ·
1 Parent(s): 6c6e517
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -1,48 +1,49 @@
1
  import gradio as gr
2
  import requests
3
 
4
- def get_address(lat, lon):
5
- url = f"https://nominatim.openstreetmap.org/reverse"
 
6
  params = {
7
  "format": "jsonv2",
8
  "lat": lat,
9
  "lon": lon
10
  }
11
- response = requests.get(url, params=params)
12
- if response.ok:
13
- return response.json().get("display_name", "Address not found.")
14
  else:
15
- return "Failed to get address."
16
 
17
  with gr.Blocks() as demo:
18
- gr.Markdown("## GPS to Address")
 
 
 
19
 
20
- lat = gr.Textbox(label="Latitude", interactive=True)
21
- lon = gr.Textbox(label="Longitude", interactive=True)
22
- address = gr.Textbox(label="Your Address")
23
 
24
- btn = gr.Button("Get Address")
25
- btn.click(fn=get_address, inputs=[lat, lon], outputs=address)
26
-
27
- # Hidden load trigger to run JS on page load
28
- dummy = gr.Textbox(visible=False)
29
- dummy.load(None, js="""
30
- () => {
31
  navigator.geolocation.getCurrentPosition(
32
  (pos) => {
33
- const latEl = document.querySelector('textarea[aria-label="Latitude"]');
34
- const lonEl = document.querySelector('textarea[aria-label="Longitude"]');
35
- if (latEl && lonEl) {
36
- latEl.value = pos.coords.latitude;
37
- lonEl.value = pos.coords.longitude;
 
38
  }
39
  },
40
  (err) => {
41
- alert("Failed to get location: " + err.message);
42
  }
43
  );
44
- return '';
45
  }
46
- """, outputs=dummy)
 
47
 
48
  demo.launch()
 
1
  import gradio as gr
2
  import requests
3
 
4
+ def get_address_from_coords(coords):
5
+ lat, lon = coords.split(",")
6
+ url = "https://nominatim.openstreetmap.org/reverse"
7
  params = {
8
  "format": "jsonv2",
9
  "lat": lat,
10
  "lon": lon
11
  }
12
+ r = requests.get(url, params=params)
13
+ if r.ok:
14
+ return r.json().get("display_name", "Address not found.")
15
  else:
16
+ return "Error fetching address."
17
 
18
  with gr.Blocks() as demo:
19
+ gr.Markdown("### 📍 GPS to Address (Works on Mobile)")
20
+
21
+ coords_box = gr.Textbox(label="Lat,Lng", visible=False)
22
+ address_box = gr.Textbox(label="Resolved Address")
23
 
24
+ # When coords are filled in JS, trigger backend to fetch address
25
+ coords_box.change(get_address_from_coords, inputs=coords_box, outputs=address_box)
 
26
 
27
+ # Inject safe JS to get GPS and update the coords_box
28
+ gr.HTML("""
29
+ <script>
30
+ window.onload = () => {
 
 
 
31
  navigator.geolocation.getCurrentPosition(
32
  (pos) => {
33
+ const coords = pos.coords.latitude + "," + pos.coords.longitude;
34
+ const textarea = document.querySelector('textarea[aria-label="Lat,Lng"]');
35
+ if (textarea) {
36
+ textarea.value = coords;
37
+ textarea.dispatchEvent(new Event("input", { bubbles: true }));
38
+ textarea.dispatchEvent(new Event("change", { bubbles: true }));
39
  }
40
  },
41
  (err) => {
42
+ alert("Location access failed: " + err.message);
43
  }
44
  );
 
45
  }
46
+ </script>
47
+ """)
48
 
49
  demo.launch()