diff --git a/frontend/src/lib/gps.ts b/frontend/src/lib/gps.ts new file mode 100644 index 0000000..7287833 --- /dev/null +++ b/frontend/src/lib/gps.ts @@ -0,0 +1,62 @@ +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 }> { + 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})` + }) + return + } + + const success_callback = (gps_position: GeolocationPosition) => { + resolve({ + coords: geolocation_to_simple_coords(gps_position.coords), + error: null + }) + return + } + + navigator.geolocation.getCurrentPosition(success_callback, error_callback) + }) +} + +function distance_between_coords( + lhs: Coordinates, + rhs: Coordinates +): number { + const R = 6371000; // earth radius in meters + const to_rad = (deg: number) => (deg * Math.PI) / 180; + + const d_lat = to_rad(rhs.latitude - lhs.latitude); + const d_lon = to_rad(rhs.longitude - lhs.longitude); + + const a = + Math.sin(d_lat / 2) ** 2 + + Math.cos(to_rad(lhs.latitude)) * + Math.cos(to_rad(rhs.latitude)) * + Math.sin(d_lon / 2) ** 2; + + const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + + return R * c; // distance in meters +} + +export function is_within_range(my_coords: Coordinates, target_coords: Coordinates, max_range: number): boolean { + const distance = distance_between_coords(my_coords, target_coords) + return distance <= max_range +} diff --git a/frontend/src/routes/zesty/gps/+page.svelte b/frontend/src/routes/zesty/gps/+page.svelte new file mode 100644 index 0000000..d5b25c0 --- /dev/null +++ b/frontend/src/routes/zesty/gps/+page.svelte @@ -0,0 +1,17 @@ +