Javascript Number toRadians()

Description

Javascript Number toRadians()


Number.prototype.toRadians = function() {
    return (this / 360) * 2 * Math.PI;
}

Javascript Number toRadians()

function getHaversineDistance(point1, point2){ 
 var R = 6372.8;//from  w ww.  j a  va 2 s.  co m

 var dLat = (point2.lat - point1.lat).toRadians();
   var dLon = (point2.lon - point1.lon).toRadians();
   var lat1 = point1.lat.toRadians();
   var lat2 = point2.lat.toRadians();

 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))
 
 return = R * c;  
}

Number.prototype.toRadians = function() {
   return (this.valueOf() * Math.PI) / 180;
}

Javascript Number toRadians()

Number.prototype.toRadians = function () {
   return this * Math.PI / 180;
}

var haversine = function (pointA, pointB) {
 var lat1 = pointA.lat;
 var lat2 = pointB.lat;

 var lon1 = pointA.lng;
 var lon2 = pointB.lng;

 var R = 6371e3; // metres
 var f1 = lat1.toRadians();
 var f2 = lat2.toRadians();
 var f = (lat2-lat1).toRadians();
 var d = (lon2-lon1).toRadians();

 var a = Math.sin(f/2) * Math.sin(f/2) +
         Math.cos(f1) * Math.cos(f2) *
         Math.sin(d/2) * Math.sin(d/2);
 var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

 var d = R * c;//from w  ww . jav a 2  s.c om
 return d;
}



PreviousNext

Related