redThread / client /src /hooks /useGeolocation.js
3v324v23's picture
Initial commit of RedThread project
0dd2082
import { useState } from 'react';
export function useGeolocation() {
const [location, setLocation] = useState(null);
const [error, setError] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const requestLocation = () => {
setIsLoading(true);
setError(null);
if (!navigator.geolocation) {
setError('Geolocation is not supported by your browser');
setIsLoading(false);
return;
}
navigator.geolocation.getCurrentPosition(
(position) => {
setLocation({
lat: position.coords.latitude,
lng: position.coords.longitude
});
setIsLoading(false);
},
(err) => {
setError(err.message || 'Failed to retrieve location');
setIsLoading(false);
},
{
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
}
);
};
const clearLocation = () => {
setLocation(null);
setError(null);
};
return { location, error, isLoading, requestLocation, clearLocation };
}