UNPKG

measure-geolocation

Version:

Utilty pacakage to find distance between different geographical locations

33 lines (27 loc) 994 B
'use strict'; class DistanceCalculator{ /** * getDistanceBetweenLocations measure distance between two geoLocations in KM * @param {Object{lat, lon}} source * @param {Object{lat, lon}} destination */ getDistanceBetweenLocations(source, destination) { let R = 6371; // Radius of the earth in km let sLat = source.lat, sLon = source.lon; let destLat = destination.lat, destLon = destination.lon; let dLat = this.deg2rad(destLat - sLat); // deg2rad below let dLon = this.deg2rad(destLon - sLon); let a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(this.deg2rad(sLat)) * Math.cos(this.deg2rad(destLat)) * Math.sin(dLon/2) * Math.sin(dLon/2) ; var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; // Distance in km return d; } // degree to radian converter deg2rad(deg) { return deg * (Math.PI/180) } } module.exports = new DistanceCalculator();