The distance calculator uses the Haversine formula:
a = sin²(Δφ/2) + cos φ1 * cos φ2 * sin²(Δλ/2)
c = 2 * atan2( √a, √(1−a) )
d = R * c
Where φ is the latitude in radians, λ is the longitude in radians and R is the earth’s radius in km.
The bearing calculator uses the formula:
θ = atan2( cos φ2 * sin Δλ , cos φ1 * sin φ2 − sin φ1 * cos φ2 * cos Δλ )
Where φ1,λ1 are the start coordinates in radians, φ2,λ2 are the end coordinates in radians,
Δλ is the difference in longitude and θ is the bearing in radians.
The code then converts radians back to degrees (multiply by pi/180).
Note that since atan2 returns values of -π..+π (-180°..+180°) we have to normalise the result and return 0°..360° for a compass heading by converting negative values to 180°..360°. We do this by adding 360 then taking the floating point modulus of 360, i.e. (θ + 360) % 360
We use cookies to ensure the site functions correctly. By continuing you agree to our Cookie Policy and Privacy Policy.