Direction based off of 2 Lat,Long points

2019-08-04 12:48发布

I am looking for a function that will allow me to send 2 Lat, Longs. 1 Lat, long is my base and the second is what I want to determine if it is N,S,E, or West. Or would I have to go NW,N,NE,EN,E,ES,SE,S,SW,WS,W,WN? Either way does anyone have something like this in C#?

1条回答
成全新的幸福
2楼-- · 2019-08-04 13:14

First you can calculate the Great Circle Bearing

θ = atan2( sin(Δλ).cos(φ2), cos(φ1).sin(φ2) − sin(φ1).cos(φ2).cos(Δλ) )

JavaScript (easily convertable to C#):

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();

http://www.movable-type.co.uk/scripts/latlong.html

Then segment the result into the desired cardinal directions, e.g. if bearing is between -45 (315 degrees) degrees and 45 degrees it is North and so on.

public string Cardinal(double degrees)
{
    if (degrees > 315.0 || degrees < 45.0)
    {
        return "N";
    }
    else if (degrees >= 45.0 && degrees < 90)
    {
        return "E";
    }
    // Etc for the whole 360 degrees.  Segment finer if you want NW, WNW, etc.
}
查看更多
登录 后发表回答