1 | /**
|
2 | * Calculate the distance in Km
|
3 | * Sourced from StackOverflow (https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula)
|
4 | * This is the 'fastest' algorithm apparently
|
5 | *
|
6 | * @param lat1
|
7 | * @param lon1
|
8 | * @param lat2
|
9 | * @param lon2
|
10 | * @returns number The distance in km
|
11 | */
|
12 |
|
13 | export function getDistanceFromLatLonInKm(
|
14 | lat1: number,
|
15 | lon1: number,
|
16 | lat2: number,
|
17 | lon2: number
|
18 | ): number {
|
19 | var p = 0.017453292519943295; // Math.PI / 180
|
20 | var c = Math.cos;
|
21 | var a =
|
22 | 0.5 -
|
23 | c((lat2 - lat1) * p) / 2 +
|
24 | c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
|
25 |
|
26 | return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
|
27 | } |
\ | No newline at end of file |