I'm trying to implement within js the Haversine formula
which takes in two sets of longitudes and latitudes and roughly calculates the geographic distance between those two points.
Its currently returning nan and I don't have a clue why. There was a troublesome .toRad function that I've replaced with the equation itself.
Heres the Max side of things:
inlets = 1;
outlets = 1;
autowatch = 1;
var lat1 = val
var lon1 = val
var lat2 = val
var lon2 = val
var R = 6371; // km
//has a problem with the .toRad() method below.
var x1 = lat2-lat1;
var dLat = x1* Math.PI / 180;
var x2 = lon2-lon1;
var dLon = x2* Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1* Math.PI / 180) * Math.cos(lat2* Math.PI / 180) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;