Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

mapping - Direction between 2 Latitude/Longitude points in C#

I have 2 coordinates in Lat Long format.

How do I determine from Point A (eg New York 37.149472,-95.509544 ) the direction in degrees to point B (eg Toronto 40.714269,-74.005973)

I'm looking for a value like "340 Degrees"

In C#

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want a constant bearing to follow you don't want the shortest (great circle) path you want a Rhumb line

Conversion of the Movable Type Scripts for that

static double DegreeBearing(
    double lat1, double lon1, 
    double lat2, double lon2)
{   
    var dLon = ToRad(lon2-lon1);
    var dPhi = Math.Log(
        Math.Tan(ToRad(lat2)/2+Math.PI/4)/Math.Tan(ToRad(lat1)/2+Math.PI/4));
    if (Math.Abs(dLon) > Math.PI) 
        dLon = dLon > 0 ? -(2*Math.PI-dLon) : (2*Math.PI+dLon);
    return ToBearing(Math.Atan2(dLon, dPhi));
}

public static double ToRad(double degrees)
{
    return degrees * (Math.PI / 180);
}

public static double ToDegrees(double radians)
{
    return radians * 180 / Math.PI;
}

public static double ToBearing(double radians) 
{  
    // convert radians to degrees (as bearing: 0...360)
    return (ToDegrees(radians) +360) % 360;
}

// verify against the website example
DegreeBearing(50.36389,-4.15694,42.35111,-71.04083);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...