In reply to The Crow:
Ok well to do it from lon / lat in javascript its:
/*
* Calculate distance (in km) between two points specified by latitude/longitude with Haversine formula
*
*/
LatLong.distHaversine = function(p1, p2) {
var R = 6371; // earth's mean radius in km
var dLat = p2.lat - p1.lat;
var dLong = p2.lon - p1.lon;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(p1.lat) * Math.cos(p2.lat) * Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
/*
* ditto using law of cosines.
*/
LatLong.distCosineLaw = function(p1, p2) {
var R = 6371; // earth's mean radius in km
var d = Math.acos(Math.sin(p1.lat)*Math.sin(p2.lat) +
Math.cos(p1.lat)*Math.cos(p2.lat)*Math.cos(p2.lon-p1.lon)) * R;
return d;
}
/*
* LatLong constructor:
*/
function LatLong(degLat, degLong) {
this.lat = LatLong.llToRad(degLat);
this.lon = LatLong.llToRad(degLong);
}
/*
* convert lat/long in degrees to radians, for handling input values
*
* this is very flexible on formats, allowing signed decimal degrees (numeric or text), or
* deg-min-sec suffixed by compass direction (NSEW). A variety of separators are accepted
* (eg 3º 37' 09"W) or fixed-width format without separators (eg 0033709W). Seconds and minutes
* may be omitted. Minimal validation is done.
*/
LatLong.llToRad = function(llDeg) {
if (!isNaN(llDeg)) return llDeg * Math.PI / 180; // signed decimal degrees without NSEW
llDeg = llDeg.replace(/[s]*$/,''); // strip trailing whitespace
var dir = llDeg.slice(-1).toUpperCase(); // compass dir'n
if (!/[NSEW]/.test(dir)) return NaN; // check for correct compass direction
llDeg = llDeg.slice(0,-1); // and lose it off the end
var dms = llDeg.split(/[s:,°º′'″"]/); // check for separators indicating d/m/s
if (dms[dms.length-1] == '') dms.length--; // trailing separator? (see note below)
switch (dms.length) { // convert to decimal degrees...
case 3: // interpret 3-part result as d/m/s
var deg = dms[0]/1 + dms[1]/60 + dms[2]/3600; break;
case 2: // interpret 2-part result as d/m
var deg = dms[0]/1 + dms[1]/60; break;
case 1: // non-separated format dddmmss
if (/[NS]/.test(dir)) llDeg = '0' + llDeg; // - normalise N/S to 3-digit degrees
var deg = llDeg.slice(0,3)/1 + llDeg.slice(3,5)/60 + llDeg.slice(5)/3600; break;
default: return NaN;
}
if (/[WS]/.test(dir)) deg = -deg; // take west and south as -ve
return deg * Math.PI / 180; // then convert to radians
}
// note: 'x-'.split(/-/) should give ['x',''] but in IE just gives ['x']
<p>Lat 1: <input name="lat1" value="53 09 02N"> Long 1: <input name="long1" value="001 50 40W"></p>
<p>Lat 2: <input name="lat2" value="52 12 17N"> Long 2: <input name="long2" value="000 08 26E"></p>
<input type="button" value="calculate distance"
onClick="result.value = LatLong.distHaversine(new LatLong(lat1.value, long1.value),
new LatLong(lat2.value, long2.value)) + ' km'">
<input name="result" value="">
and.... (split coz its longer than the max message size)