Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#

like image 210
Ian Vink Avatar asked Jan 11 '10 15:01

Ian Vink


People also ask

How do you find the distance between two latitude and longitude in C?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.

How do you find the direction between two coordinates?

To find the directional vector, subtract the coordinates of the initial point from the coordinates of the terminal point.

How do you find the direction using latitude and longitude?

Latitude is the distance north or south of the Equator and is expressed as a number between 0 and 90 degrees north or south. Longitude is the distance east or west of the Prime Meridian and is expressed as a number between 0 and 180 degrees east or west.


2 Answers

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);
like image 86
ShuggyCoUk Avatar answered Sep 24 '22 08:09

ShuggyCoUk


if you have a look at http://www.movable-type.co.uk/scripts/latlong.html it has javascript you can rewrite to c#.

like image 31
John Boker Avatar answered Sep 25 '22 08:09

John Boker