How to get the bearing (direction) from 2 geolocation points?
Let's say I have 2 points with latitude and longitude. How do I calculate the bearing(direction) from point A to point B in degree?
I found this code, but the toBrng() is failed to compile.
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) 
Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var brng = Math.atan2(y, x).toBrng();
2 Answers

Accepted Answer
var dLat = (lat2lat1).toRad(); var dLon = (lon2lon1).toRad(); var lat1 = lat1.toRad(); var lat2 = lat2.toRad(); var y = Math.sin(dLon) * Math.cos(lat2); var x = Math.cos(lat1)*Math.sin(lat2)  Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon); var brng = Math.atan2(y, x).toDeg();
Your code is very similar to this….you just need to make sure that all variables are in radians. (you will need to write and extend the number objects with the methods) which is just
var * Math.PI / 180
and then you need to convert it back to Deg which you will also need to write. The reason why it is not working is because you need to write the methodstoRad()
andtoDeg()
[which in your code you calltoBrng()
] 
i have been testing the code above, but when i made a comparision with this site http://www.movabletype.co.uk/scripts/latlong.html i get very different values for bearing, obvously with the same points… any help ?
with this points http://cl.ly/3y3u1P471A1M0Z0Q2q18 (see the image) in android i get a bearing o 185… i don't know what i'am doing wrong… :(