jergason/Tolmey — Issue #8: pull haversine into own space
Author: coolaj86
Created: 2011-12-12 19:34:10+00:00
State: open

No need for this to be part of an object, really. Plus, it's easier to copy/paste out and in to other code this way.

function degreesToRadians(degrees) {
return (degrees * Math.PI) / 180;
};

function (lat_start, long_start, lat_end, long_end) {
var dLat = degreesToRadians(lat_end - lat_start),
dLon = degreesToRadians(long_end - long_start),
lat1 = degreesToRadians(lat_start),
lat2 = degreesToRadians(lat_end);

var a = Math.pow(Math.sin(dLat/2), 2) + Math.cos(lat1)*Math.cos(lat2)*Math.pow(Math.sin(dLon/2),2);
var centralAngle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return centralAngle;
};