Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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;
}
share|improve this question
    
+1 the javascript source you reference looks great ... assuming the bugs have been worked out. –  Kirk Kuykendall Sep 16 '11 at 1:52
add comment

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.

share|improve this answer
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 '11 at 23:35
    
Yup. ParseCoordinate needs to be modified to properly handle negative numbers. –  jacknad Sep 16 '11 at 2:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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