Improvements on maps

This commit is contained in:
Sebastien Riviere
2025-08-31 17:33:18 +02:00
parent e5d90d824e
commit d088253758
13 changed files with 264 additions and 184 deletions

View File

@@ -2,23 +2,37 @@
import { useEffect, useState } from "react";
export default function useLocation(interval) {
const [location, setLocation] = useState();
const [location, setLocation] = useState(null);
useEffect(() => {
function update() {
navigator.geolocation.getCurrentPosition(
(position) => {
setLocation([position.coords.latitude, position.coords.longitude]);
if(interval != Infinity) {
setTimeout(update, interval);
}
},
() => { },
{ enableHighAccuracy: true, timeout: Infinity, maximumAge: 0 }
);
if (!navigator.geolocation) {
console.log('Geolocation not supported');
return;
}
if (interval < 1000 || interval == Infinity) {
console.log('Localisation interval no supported');
return;
}
update();
const watchId = navigator.geolocation.watchPosition(
(pos) => {
setLocation({
lat: pos.coords.latitude,
lng: pos.coords.longitude,
accuracy: pos.coords.accuracy,
timestamp: pos.timestamp
});
},
(err) => console.log("Error :", err),
{
enableHighAccuracy: true,
timeout: 10000,
maximumAge: interval,
}
);
return () => navigator.geolocation.clearWatch(watchId);
}, []);
return location;