Returns the great circle distance between (lat1, lon1) and (lat2, lon2). - Node.js Geometry

Node.js examples for Geometry:Distance

Description

Returns the great circle distance between (lat1, lon1) and (lat2, lon2).

Demo Code

function dist(lat1, lon1, lat2, lon2) {
  var R = 6371; // km
  var dLat = (lat2 - lat1).toRad();
  var dLon = (lon2 - lon1).toRad();
  var lat1 = parseFloat(lat1).toRad();
  var lat2 = parseFloat(lat2).toRad();

  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 d = R * c;/*from  w  w w  .j  a  v  a  2  s.c  o m*/
  return d;
}

Related Tutorials