This uses the ‘haversine’ formula to calculate great-circle distances between the two points – that is, the shortest distance over the earth’s surface – giving an ‘as-the-crow-flies’ distance between the points (ignoring any hills!).
Haversine formula:
R
= earth’s radius
(mean radius
= 6,371km
)
Δlat
= lat2− lat1
Δlong
= long2− long1
a
= sin²
(Δlat
/2) + cos(lat1
).cos(lat2
).sin²
(Δlong
/2)
c
= 2.atan2(√a
, √
(1−a
))
d
= R
.c
(Note that angles need to be in radians to pass to trig functions).
JavaScript:
var R
= 6371; // km
var dLat
= (lat2
-lat1
).toRad
();
var dLon
= (lon2
-lon1
).toRad
();
var a
= Math
.sin(dLat
/2) * Math
.sin(dLat
/2) +
Math
.cos(lat1
.toRad
()) * Math
.cos(lat2
.toRad
()) *
Math
.sin(dLon
/2) * Math
.sin(dLon
/2);
var c
= 2 * Math
.atan2(Math
.sqrt(a
), Math
.sqrt(1-a
));
var d
= R
* c
;
Source & reference : http://www.movable-type.co.uk/scripts/latlong.html
Post new comment