1// Converts from degrees to radians.
2function toRadians(degrees) {
3 return degrees * Math.PI / 180;
4};
5
6// Converts from radians to degrees.
7function toDegrees(radians) {
8 return radians * 180 / Math.PI;
9}
10
11
12function bearing(startLat, startLng, destLat, destLng){
13 startLat = toRadians(startLat);
14 startLng = toRadians(startLng);
15 destLat = toRadians(destLat);
16 destLng = toRadians(destLng);
17
18 y = Math.sin(destLng - startLng) * Math.cos(destLat);
19 x = Math.cos(startLat) * Math.sin(destLat) -
20 Math.sin(startLat) * Math.cos(destLat) * Math.cos(destLng - startLng);
21 brng = Math.atan2(y, x);
22 brng = toDegrees(brng);
23 return (brng + 360) % 360;
24}