From 54ad0dcc17763316d236aa85acbc14acbddf7d81 Mon Sep 17 00:00:00 2001 From: orson Date: Sat, 15 Mar 2025 20:17:27 -0600 Subject: [PATCH] verbose errors for geolocation --- templates/index.html | 68 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/templates/index.html b/templates/index.html index c7051cf..175cc9e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -55,32 +55,70 @@ {% endfor %} map.addLayer(markerCluster); + // Improved geolocation using direct browser API + function getLocation() { + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition( + onLocationFound, + onLocationError, + { + enableHighAccuracy: true, + timeout: 10000, + maximumAge: 0 + } + ); + } else { + alert("La geolocalización no está soportada en este navegador."); + map.setView([20.57, -100.38], 13); // Default to Querétaro's coordinates + } + } + // Geolocation function - function onLocationFound(e) { - var radius = e.accuracy / 2; // Accuracy of the location + function onLocationFound(position) { + var lat = position.coords.latitude; + var lng = position.coords.longitude; + var latlng = L.latLng(lat, lng); + var radius = position.coords.accuracy / 2; // Accuracy of the location + // Add a pin (marker) at the user's location - user_marker = L.marker(e.latlng).addTo(map) + user_marker = L.marker(latlng).addTo(map) .bindPopup("Te detectamos en un radio de " + parseInt(radius) + " metros de este punto").openPopup(); - document.getElementById('lat').value = e.latlng.lat; - document.getElementById('lng').value = e.latlng.lng; + + if (document.getElementById('lat') && document.getElementById('lng')) { + document.getElementById('lat').value = lat; + document.getElementById('lng').value = lng; + } + // Add a circle around the user's location - user_radial = L.circle(e.latlng, radius).addTo(map); + user_radial = L.circle(latlng, radius).addTo(map); + // Center the map on the user's location - map.setView(e.latlng, 18); // Adjust zoom level as needed + map.setView(latlng, 18); // Adjust zoom level as needed } // Error handling for geolocation - function onLocationError(e) { - alert(e.message); + function onLocationError(error) { + var errorMsg; + switch(error.code) { + case error.PERMISSION_DENIED: + errorMsg = "Usuario denegó la solicitud de geolocalización."; + break; + case error.POSITION_UNAVAILABLE: + errorMsg = "La información de ubicación no está disponible."; + break; + case error.TIMEOUT: + errorMsg = "Se agotó el tiempo de espera para la solicitud de ubicación."; + break; + default: + errorMsg = "Ocurrió un error desconocido al obtener la ubicación."; + } + console.error("Error de geolocalización:", errorMsg); + alert(errorMsg); map.setView([20.57, -100.38], 13); // Default to Querétaro's coordinates } - // Use Leaflet's built-in location detection - map.on('locationfound', onLocationFound); - map.on('locationerror', onLocationError); - - // Start the location detection - map.locate({setView: true, zoom: 16}); + // Start location detection + getLocation(); map.on('click', function(e) { var latlng = e.latlng; document.getElementById('lat').value = latlng.lat;