fix
Browse files
app.py
CHANGED
|
@@ -1,48 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
|
|
|
|
| 6 |
params = {
|
| 7 |
"format": "jsonv2",
|
| 8 |
"lat": lat,
|
| 9 |
"lon": lon
|
| 10 |
}
|
| 11 |
-
|
| 12 |
-
if
|
| 13 |
-
return
|
| 14 |
else:
|
| 15 |
-
return "
|
| 16 |
|
| 17 |
with gr.Blocks() as demo:
|
| 18 |
-
gr.Markdown("## GPS to Address")
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
address = gr.Textbox(label="Your Address")
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
dummy = gr.Textbox(visible=False)
|
| 29 |
-
dummy.load(None, js="""
|
| 30 |
-
() => {
|
| 31 |
navigator.geolocation.getCurrentPosition(
|
| 32 |
(pos) => {
|
| 33 |
-
const
|
| 34 |
-
const
|
| 35 |
-
if (
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
}
|
| 39 |
},
|
| 40 |
(err) => {
|
| 41 |
-
alert("
|
| 42 |
}
|
| 43 |
);
|
| 44 |
-
return '';
|
| 45 |
}
|
| 46 |
-
|
|
|
|
| 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()
|