2025-08-02 04:53:20 +02:00
|
|
|
export type Coordinates = {
|
|
|
|
latitude: number
|
|
|
|
longitude: number
|
|
|
|
}
|
|
|
|
|
|
|
|
function geolocation_to_simple_coords(coordinates: GeolocationCoordinates): Coordinates {
|
|
|
|
return { latitude: coordinates.latitude, longitude: coordinates.longitude }
|
|
|
|
}
|
|
|
|
|
2025-08-02 06:10:41 +02:00
|
|
|
export function get_coords(): Promise<{ coords: Coordinates | null; error: string | null }> {
|
2025-08-02 04:53:20 +02:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (!navigator.geolocation) {
|
|
|
|
resolve({ coords: null, error: "Geolocation is not supported by your browser" })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const error_callback = (gps_error: GeolocationPositionError) => {
|
|
|
|
console.log(gps_error)
|
|
|
|
resolve({
|
|
|
|
coords: null,
|
2025-08-02 06:10:41 +02:00
|
|
|
error: `Unable to retrieve your location: (${gps_error.message})`,
|
2025-08-02 04:53:20 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const success_callback = (gps_position: GeolocationPosition) => {
|
|
|
|
resolve({
|
|
|
|
coords: geolocation_to_simple_coords(gps_position.coords),
|
2025-08-02 06:10:41 +02:00
|
|
|
error: null,
|
2025-08-02 04:53:20 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
navigator.geolocation.getCurrentPosition(success_callback, error_callback)
|
|
|
|
})
|
|
|
|
}
|