Spaces:
Running
Running
File size: 2,941 Bytes
b89a86e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
import { useState, useCallback } from "react";
interface LocationData {
latitude: number;
longitude: number;
accuracy?: number;
googleMapsUrl: string;
timestamp: Date;
}
interface LocationError {
code: number;
message: string;
}
export const useLocation = () => {
const [isLoading, setIsLoading] = useState(false);
const [location, setLocation] = useState<LocationData | null>(null);
const [error, setError] = useState<LocationError | null>(null);
const getLocation = useCallback(() => {
if (!navigator.geolocation) {
setError({
code: 0,
message: "Geolocation is not supported by this browser."
});
return;
}
setIsLoading(true);
setError(null);
navigator.geolocation.getCurrentPosition(
(position) => {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
const accuracy = position.coords.accuracy;
const googleMapsUrl = `https://maps.google.com/?q=${lat},${lng}`;
const locationData: LocationData = {
latitude: lat,
longitude: lng,
accuracy,
googleMapsUrl,
timestamp: new Date(position.timestamp)
};
setLocation(locationData);
setIsLoading(false);
},
(err) => {
let errorMessage: string;
switch (err.code) {
case err.PERMISSION_DENIED:
errorMessage = "User denied the location request. Please enable location services and try again.";
break;
case err.POSITION_UNAVAILABLE:
errorMessage = "Location information is unavailable. Please check your GPS/network connection.";
break;
case err.TIMEOUT:
errorMessage = "Location request timed out. Please try again.";
break;
default:
errorMessage = "An unknown error occurred while retrieving location.";
break;
}
setError({
code: err.code,
message: errorMessage
});
setIsLoading(false);
},
{
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 60000
}
);
}, []);
const copyToClipboard = useCallback((url: string) => {
if (navigator.clipboard) {
return navigator.clipboard.writeText(url);
}
return Promise.reject("Clipboard not supported");
}, []);
const shareLocation = useCallback((locationData: LocationData) => {
if (navigator.share) {
return navigator.share({
title: "My Location",
url: locationData.googleMapsUrl
});
}
return Promise.reject("Web Share API not supported");
}, []);
const clearLocation = useCallback(() => {
setLocation(null);
setError(null);
}, []);
return {
location,
error,
isLoading,
getLocation,
copyToClipboard,
shareLocation,
clearLocation
};
}; |