File size: 1,926 Bytes
1d71065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// This function will be called by the button's onclick event in Shiny
function sendLocationToShiny() {
  // Check if Geolocation is supported
  if (navigator.geolocation) {
    // Set a status message while we wait for user permission
    Shiny.setInputValue("geolocation_status", "Esperando permiso del usuario...");
    navigator.geolocation.getCurrentPosition(showPosition, showError, {
      enableHighAccuracy: true,
      timeout: 10000, // 10 segundos de tiempo de espera
      maximumAge: 0 // No usar una ubicaci贸n en cach茅
    });
  } else {
    // Handle case where geolocation is not supported
    Shiny.setInputValue("geolocation_status", "La geolocalizaci贸n no es soportada por este navegador.");
  }
}

function showPosition(position) {
  const coords = {
    lat: position.coords.latitude,
    lon: position.coords.longitude,
    accuracy: position.coords.accuracy,
    timestamp: new Date(position.timestamp).toISOString()
  };
  // Send the coordinates object to the Shiny server
  Shiny.setInputValue("geolocation_data", coords, {priority: "event"});
  Shiny.setInputValue("geolocation_status", "Ubicaci贸n recibida exitosamente.");
}

function showError(error) {
  let errorMessage;
  switch(error.code) {
    case error.PERMISSION_DENIED:
      errorMessage = "El usuario deneg贸 la solicitud de geolocalizaci贸n."; break;
    case error.POSITION_UNAVAILABLE:
      errorMessage = "La informaci贸n de la ubicaci贸n no est谩 disponible."; break;
    case error.TIMEOUT:
      errorMessage = "La solicitud para obtener la ubicaci贸n del usuario ha caducado."; break;
    case error.UNKNOWN_ERROR:
      errorMessage = "Ocurri贸 un error desconocido."; break;
  }
  // Send the error message to the Shiny server
  Shiny.setInputValue("geolocation_status", "Error: " + errorMessage);
  Shiny.setInputValue("geolocation_data", null); // Clear data on error
}