lib/geo.js
function getDistanceInKilometers(p1, p2) {
  // Credit: adapts code for Haversine formula and degrees-to-radian conversion found at:
  // http://www.movable-type.co.uk/scripts/latlong.html

  var R = 6371; // earth's radius in kilometers
  var dLat = degreesToRadians(p2.lat - p1.lat);
  var dLon = degreesToRadians(p2.lng - p1.lng); 
  var lat1 = degreesToRadians(p1.lat);
  var lat2 = degreesToRadians(p2.lat);

  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2)
    + Math.sin(dLon/2) * Math.sin(dLon/2)
    * Math.cos(lat1) * Math.cos(lat2);

  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var distance = R * c; // kilomter
  return distance;
}
function getDistanceInKilometers(p1, p2) {
  // Credit: adapts code for Haversine formula and degrees-to-radian conversion found at:
  // http://www.movable-type.co.uk/scripts/latlong.html

  var R = 6371; // earth's radius in kilometers
  var dLat = degreesToRadians(p2.lat - p1.lat);
  var dLon = degreesToRadians(p2.lng - p1.lng); 
  var lat1 = degreesToRadians(p1.lat);
  var lat2 = degreesToRadians(p2.lat);

  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2)
    + Math.sin(dLon/2) * Math.sin(dLon/2)
    * Math.cos(lat1) * Math.cos(lat2);

  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var distance = R * c; // kilomter
  return distance;
}
function getDistanceInKilometers ( p1 , p2 ) { // Credit: adapts code for Haversine formula and degrees-to-radian conversion found at: // http://www.movable-type.co.uk/scripts/latlong.html var R = 6371 ; // earth's radius in kilometers var dLat = degreesToRadians ( p2 . lat - p1 . lat ) ; var dLon = degreesToRadians ( p2 . lng - p1 . lng ) ; var lat1 = degreesToRadians ( p1 . lat ) ; var lat2 = degreesToRadians ( p2 . lat ) ; var a = Math . sin ( dLat / 2 ) * Math . sin ( dLat / 2 ) + Math . sin ( dLon / 2 ) * Math . sin ( dLon / 2 ) * Math . cos ( lat1 ) * Math . cos ( lat2 ) ; var c = 2 * Math . atan2 ( Math . sqrt ( a ) , Math . sqrt ( 1 - a ) ) ; var distance = R * c ; // kilomter return distance ; }
Calculate great circle distance and return result in kilometers
@param p1 the origin with lat (latitude) and lng (longitude) properties
@param p2 the destination with lat (latitude) and lng (longitude) properties
Calculate great circle distance and return result in kilometers