February 15, 2012

JavaScript Lat - Lon into Km

var R = 6371; //km
var dLat = (39.518660 - 39.617641).toRad();
var dLon = (-74.709163 - (-74.714657)).toRad();
var lat1 = lat1.toRad();
var lat2 = 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;
d;