The following returns 150 degrees using C# converted from JavaScript Bearing formulas, but Google earth returns 210 degrees for the same values. The broken code is GetBearing() but I've also included Calculate() to show hard coded test values and ParseCoordinate() to show that I am converting Degree-Minute-Second to radian decimal degrees for GetBearing(). Are the JavaScript Bearing formulas wrong?

double DEG_PER_RAD = (180.0/Math.PI);
// Return Bearing (degrees)
private double GetBearing(double lat1, double lon1, double lat2, double lon2)
    var dLon = lon2 - lon1;
    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);
    return DEG_PER_RAD*Math.Atan2(y, x);
private void Calculate()
    var lon1 = ParseCoordinate("38 51 30.5");
    var lat1 = ParseCoordinate("78 12 33.8");
    var lon2 = ParseCoordinate("38 45 46.2");
    var lat2 = ParseCoordinate("78 16 46.0");
    var bearing = GetBearing(lat1, lon1, lat2, lon2);
    // Show bearing
// Convert Degree-Minute-Second to Degree.ddd
private double ParseCoordinate(string coordinate)
    var dms = coordinate.Split(' ');
    var rVal = 0.0;
    var i = 0;
    foreach (var s in dms)
        double d;
        double.TryParse(s, out d);
        rVal += (d/(Math.Pow(60.0, i++)));
    rVal /= DEG_PER_RAD;
    return rVal;
  • +1 the javascript source you reference looks great ... assuming the bugs have been worked out. Sep 16, 2011 at 1:52

1 Answer 1


The two numbers are actually just mirrored bearings (360 - 150 = 210), so you either specified the longitude with the wrong sign (maybe it is 38 degrees west) or you're missing a sign in dLon or x calculation.

  • 1
    +1. This indicates that ParseCoordinate is broken, too (it will fail to convert negative values correctly). One also wonders whether lat, lon have been specified in the right order: these points are somewhere in the Arctic Ocean north of Russia.
    – whuber
    Sep 15, 2011 at 23:35
  • Yup. ParseCoordinate needs to be modified to properly handle negative numbers.
    – jacknad
    Sep 16, 2011 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.