mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 10:04:44 +00:00
Compute nearby-brewery distance in SQL instead of client-side Haversine, and add the spatial index it relies on.
This commit is contained in:
@@ -1,24 +1,3 @@
|
||||
const EARTH_RADIUS_METRES = 6_371_000;
|
||||
|
||||
export interface Coordinates {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
}
|
||||
|
||||
export const haversineDistanceMetres = (a: Coordinates, b: Coordinates): number => {
|
||||
const toRadians = (degrees: number) => (degrees * Math.PI) / 180;
|
||||
const deltaLat = toRadians(b.latitude - a.latitude);
|
||||
const deltaLng = toRadians(b.longitude - a.longitude);
|
||||
const lat1 = toRadians(a.latitude);
|
||||
const lat2 = toRadians(b.latitude);
|
||||
|
||||
const h =
|
||||
Math.sin(deltaLat / 2) ** 2 +
|
||||
Math.cos(lat1) * Math.cos(lat2) * Math.sin(deltaLng / 2) ** 2;
|
||||
|
||||
return 2 * EARTH_RADIUS_METRES * Math.asin(Math.sqrt(h));
|
||||
};
|
||||
|
||||
export type DistanceUnit = 'km' | 'mi';
|
||||
|
||||
const METRES_PER_MILE = 1609.344;
|
||||
|
||||
Reference in New Issue
Block a user