44 lines
890 B
JavaScript
44 lines
890 B
JavaScript
import { useEffect, useState } from 'react';
|
|
|
|
export const useData = () => {
|
|
const [canUseGeolocation, setCanUseGeolocation] = useState(false);
|
|
const [geoError, setGeoError] = useState(null);
|
|
|
|
const [initialState, setInitialState] = useState({
|
|
longitude: 2.209666999999996,
|
|
latitude: 46.232192999999995,
|
|
zoom: 5,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if ('geolocation' in navigator == false) {
|
|
setCanUseGeolocation(false);
|
|
return;
|
|
}
|
|
|
|
setCanUseGeolocation(true);
|
|
|
|
navigator.geolocation.getCurrentPosition(
|
|
position => {
|
|
setInitialState(prev => ({
|
|
...prev,
|
|
latitude: position.coords.latitude,
|
|
longitude: position.coords.longitude,
|
|
}));
|
|
},
|
|
error => {
|
|
if (error.PERMISSION_DENIED) {
|
|
setGeoError('permission_denied');
|
|
console.warn(error.message);
|
|
}
|
|
}
|
|
);
|
|
}, []);
|
|
|
|
return {
|
|
initialState,
|
|
geoError,
|
|
canUseGeolocation,
|
|
};
|
|
};
|