team-1/frontend/src/lib/gps.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

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 }
}
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,
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),
error: null,
2025-08-02 04:53:20 +02:00
})
return
}
navigator.geolocation.getCurrentPosition(success_callback, error_callback)
})
}