mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 10:20:16 +01:00
Improvements on maps
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user