views:

547

answers:

2

Howdy.

I need to get the distance from a lat/lng point to a line. Of course needs to follow the Great Circle.

I found a great article on this at http://www.movable-type.co.uk/scripts/latlong.html

but the code is not working right. Either I am doing something wrong or there is something missing. Here is the function in question. See the link for the other functions if needed.

    var R = 3961.3
    LatLon.crossTrack = function(lat1, lon1, lat2, lon2, lat3, lon3) {
     var d13 = LatLon.distHaversine(lat1, lon1, lat3, lon3);
     var brng12 = LatLon.bearing(lat1, lon1, lat2, lon2);
     var brng13 = LatLon.bearing(lat1, lon1, lat3, lon3);
     var dXt = Math.asin(Math.sin(d13/R)*Math.sin(brng13-brng12)) * R;
     return dXt;
    }

lat/lon1 = -94.127592, 41.81762

lat/lon2 = -94.087257, 41.848202

lat/lon3 = -94.046875, 41.791057

This reports 0.865 miles. The actual distance is 4.29905 miles.

Any clues as to how to fix this? I am not a mathematician, just a long in the tooth programmer.

  • Brad
+1  A: 
duffymo
That was indeed it! I just went blank that the return value of the bearing method was degrees. A head slapper.Thanks!
Brad Mathews
A: 

hello, is your function returning same value for these coordinates: crossTrack(0,0,0,1,0.1,0.5); crossTrack(0,0,0,1,0.1,0.6); crossTrack(0,0,0,1,0.1,0.4);

i think it should but mine does not. the 3rd point is always 0.1 to the north from equator. only the longitude changes which should not affect the result. as it seems it does...