rlogh commited on
Commit
15a1ffc
·
verified ·
1 Parent(s): f22c741

Upload 4 files

Browse files
Files changed (1) hide show
  1. app.py +47 -27
app.py CHANGED
@@ -98,35 +98,60 @@ def handle_gps_location(json_str):
98
  return status_msg, f"Error parsing GPS data: {str(e)}", "", "", ""
99
 
100
  def get_gps_js():
101
- """JavaScript for GPS capture using hidden textbox approach"""
102
  return """
103
  () => {
104
- // find the textarea element inside Gradio textbox by its elem_id
105
- const textarea = document.querySelector('#hidden_gps_input textarea');
106
- if (!textarea) {
107
- console.log("Hidden GPS textbox not found");
108
- return;
109
- }
110
  if (!navigator.geolocation) {
111
- textarea.value = JSON.stringify({error: "Geolocation not supported"});
112
- textarea.dispatchEvent(new Event('input', { bubbles: true }));
113
  return;
114
  }
 
115
  navigator.geolocation.getCurrentPosition(
116
  function(position) {
117
- const data = {
118
- latitude: position.coords.latitude,
119
- longitude: position.coords.longitude,
120
- accuracy: position.coords.accuracy,
121
- timestamp: new Date().toISOString()
122
- };
123
- textarea.value = JSON.stringify(data);
124
- // dispatch 'input' event so Gradio notices the change
125
- textarea.dispatchEvent(new Event('input', { bubbles: true }));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  },
127
  function(err) {
128
- textarea.value = JSON.stringify({ error: err.message });
129
- textarea.dispatchEvent(new Event('input', { bubbles: true }));
 
 
 
 
 
 
 
 
 
 
130
  },
131
  { enableHighAccuracy: true, timeout: 10000 }
132
  );
@@ -460,8 +485,7 @@ def field_capture_ui(camera):
460
  # GPS Button (now functional)
461
  gps_btn = gr.Button("📍 Get GPS", variant="primary", elem_id="gps_btn_id")
462
 
463
- # Hidden Input (kept for UI layout and future restoration)
464
- hidden_gps_input = gr.Textbox(visible=False, elem_id="hidden_gps_input")
465
 
466
  with gr.Row():
467
  lat_box = gr.Textbox(label="Latitude", interactive=True, value="0.0", elem_id="lat")
@@ -484,11 +508,7 @@ def field_capture_ui(camera):
484
  fn=None, inputs=[], outputs=[], js=get_gps_js()
485
  )
486
 
487
- hidden_gps_input.change(
488
- fn=handle_gps_location,
489
- inputs=[hidden_gps_input],
490
- outputs=[status, lat_box, lon_box, accuracy_box, device_ts_box]
491
- )
492
 
493
  time_btn.click(
494
  fn=placeholder_time_capture,
 
98
  return status_msg, f"Error parsing GPS data: {str(e)}", "", "", ""
99
 
100
  def get_gps_js():
101
+ """JavaScript for GPS capture - direct approach to populate visible textboxes"""
102
  return """
103
  () => {
104
+ console.log("GPS button clicked - direct approach...");
105
+
 
 
 
 
106
  if (!navigator.geolocation) {
107
+ alert("Geolocation not supported by this browser");
 
108
  return;
109
  }
110
+
111
  navigator.geolocation.getCurrentPosition(
112
  function(position) {
113
+ console.log("GPS position received:", position);
114
+
115
+ // Find the visible textboxes directly
116
+ const latBox = document.querySelector('#lat textarea');
117
+ const lonBox = document.querySelector('#lon textarea');
118
+ const accuracyBox = document.querySelector('#accuracy textarea');
119
+ const timestampBox = document.querySelector('#device_ts textarea');
120
+
121
+ console.log("Found textboxes:", {latBox, lonBox, accuracyBox, timestampBox});
122
+
123
+ if (latBox && lonBox && accuracyBox && timestampBox) {
124
+ // Populate the textboxes directly
125
+ latBox.value = position.coords.latitude.toString();
126
+ lonBox.value = position.coords.longitude.toString();
127
+ accuracyBox.value = position.coords.accuracy.toString();
128
+ timestampBox.value = new Date().toISOString();
129
+
130
+ // Trigger change events
131
+ latBox.dispatchEvent(new Event('input', { bubbles: true }));
132
+ lonBox.dispatchEvent(new Event('input', { bubbles: true }));
133
+ accuracyBox.dispatchEvent(new Event('input', { bubbles: true }));
134
+ timestampBox.dispatchEvent(new Event('input', { bubbles: true }));
135
+
136
+ console.log("GPS data populated successfully");
137
+ } else {
138
+ console.error("Could not find all required textboxes");
139
+ alert("Error: Could not find GPS input fields");
140
+ }
141
  },
142
  function(err) {
143
+ console.error("GPS error:", err);
144
+ let errorMsg = "GPS Error: ";
145
+ if (err.code === 1) {
146
+ errorMsg += "Location access denied by user.";
147
+ } else if (err.code === 2) {
148
+ errorMsg += "Location information unavailable.";
149
+ } else if (err.code === 3) {
150
+ errorMsg += "Location request timed out.";
151
+ } else {
152
+ errorMsg += err.message;
153
+ }
154
+ alert(errorMsg);
155
  },
156
  { enableHighAccuracy: true, timeout: 10000 }
157
  );
 
485
  # GPS Button (now functional)
486
  gps_btn = gr.Button("📍 Get GPS", variant="primary", elem_id="gps_btn_id")
487
 
488
+ # Note: Using direct textbox population instead of hidden input
 
489
 
490
  with gr.Row():
491
  lat_box = gr.Textbox(label="Latitude", interactive=True, value="0.0", elem_id="lat")
 
508
  fn=None, inputs=[], outputs=[], js=get_gps_js()
509
  )
510
 
511
+ # Note: GPS data is now populated directly by JavaScript, no event handler needed
 
 
 
 
512
 
513
  time_btn.click(
514
  fn=placeholder_time_capture,