FerISIS's picture
Upload Geolocation.js
1d71065 verified
// 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
}