Nodejs Distance Calculate haversineDistance(lat1, lon1, lat2, lon2)

Here you can find the source of haversineDistance(lat1, lon1, lat2, lon2)

Method Source Code

function haversineDistance(lat1, lon1, lat2, lon2){

   var R = 6371; // km 
   var x1 = lat2-lat1;
   var dLat = x1.toRad();  
   var x2 = lon2-lon1;
   var dLon = x2.toRad();  
   var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
   Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
   Math.sin(dLon/2) * Math.sin(dLon/2);  
   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
   var d = R * c; 

    return d;/*from  www . ja v  a  2s  .  c o  m*/
}

Related

  1. distanceBetweenTwoPoints(p1, p2)
    Util.distanceBetweenTwoPoints = function(p1, p2) {
      return Math.sqrt(Math.pow(p2.y - p1.y, 2) + Math.pow(p2.x - p1.x, 2)).toFixed(2);
    };
    
  2. distanceOfTimeInWords(from, to)
    window.distanceOfTimeInWords = function (from, to) {
      var distance_in_milliseconds = to - from;
      var distance_in_minutes = Math.round(Math.abs(distance_in_milliseconds / 60000));
      var words = "";
      if (distance_in_minutes == 0) {
        words = "less than a minute";
      } else if (distance_in_minutes == 1) {
        words = "1 minute";
      } else if (distance_in_minutes < 45) {
    ...
    
  3. getEditDistance(a, b)
    function getEditDistance(a, b){
      if(a.length == 0) return b.length;
      if(b.length == 0) return a.length;
      var matrix = [];
      var i;
      for(i = 0; i <= b.length; i++){
        matrix[i] = [i];
      var j;
    ...
    
  4. get_distance (lat1, lon1, lat2, lon2)
    function get_distance (lat1, lon1, lat2, lon2) {
      var R = 6371;
      var dLat = deg2rad(lat2-lat1);
      var dLon = deg2rad(lon2-lon1);
      var lat1 = deg2rad(lat1);
      var lat2 = deg2rad(lat2);
      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)); 
    ...
    
  5. haversineDistance(lat1, lng1, lat2, lng2)
    var haversineDistance(lat1, lng1, lat2, lng2) {
        var R = 6371; 
        var deltaLat = (lat2-lat1).inRadians();
        var deltaLng = (lng2-lng1).inRadians();
        var a = (Math.sin(deltaLat/2) * Math.sin(deltaLat/2)) + (Math.cos(lat1.inRadians()) * Math.cos(lat2.inRadians()) *
                       Math.sin(deltaLng/2) * Math.sin(deltaLng/2));
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        return R * c;