This is how you calculate the distance between two points on on the earth.
Haversine formula: (^ = Change in)
R = earth?s radius (mean radius = 6,371km)
^lat = lat2- lat1
^long = long2- long1
a = sin^2(^lat/2) + cos(lat1).cos(lat2).sin^2(^long/2)
c = 2.atan2(Sqrt(a), Sqrt((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;
More info (including bearing)
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5