How to get the bearing (direction) from 2 geolocation points?

You must Login before you can answer or comment on any questions.

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 = (lat2-lat1).toRad();
var dLon = (lon2-lon1).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 methods toRad() and toDeg() [which in your code you call toBrng()]

— answered 3 years ago by Abraham Vivas
answer permalink
3 Comments
  • Hi Abraham, thank you for your help. With your tips, I'm able to come up with a function like this. Time for testing this code.

    function getBearing(lat1,lon1,lat2,lon2){
        if (typeof(Number.prototype.toRad) === "undefined") {
          Number.prototype.toRad = function() {
            return this * Math.PI / 180;
          }
        }
     
        if (typeof(Number.prototype.toDeg) === "undefined") {
          Number.prototype.toDeg = function() {
            return this * 180 / Math.PI;
          }
        }
     
        var dLon = (lon2-lon1).toRad();
        lat1 = lat1.toRad();
        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();
     
        return brng;
     
    }

    — commented 3 years ago by Mel Maxwell

  • with the code above, I get some negative value while heading to south and west.

    Any idea how to fix this?

    — commented 3 years ago by Mel Maxwell

  • just figured it out how to get the degree in 0 to 360.

    if (brng<0){
            return (180+(180+brng));
        }
        else{
            return brng;
        }

    — commented 3 years ago by Mel Maxwell

i have been testing the code above, but when i made a comparision with this site http://www.movable-type.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... :(

Your Answer

Think you can help? Login to answer this question!