@mediarithmics/plugins-nodejs-sdk
Version:
This is the mediarithmics nodejs to help plugin developers bootstrapping their plugin without having to deal with most of the plugin boilerplate
28 lines (25 loc) • 691 B
text/typescript
/**
* Calculate the distance in Km
* Sourced from StackOverflow (https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula)
* This is the 'fastest' algorithm apparently
*
* @param lat1
* @param lon1
* @param lat2
* @param lon2
* @returns number The distance in km
*/
export function getDistanceFromLatLonInKm(
lat1: number,
lon1: number,
lat2: number,
lon2: number
): number {
var p = 0.017453292519943295; // Math.PI / 180
var c = Math.cos;
var a =
0.5 -
c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
}