中2个点之间的距离

中2个点之间的距离

我正在尝试找出一种方法来计算C#中2个点之间的距离。

这是我一直在尝试的代码,尽管我担心得到的答案不正确。

static void Main()
    {
        //postcode australia 2600 -> 3000
        float latA = -31.997976f;
        float longA = 115.762877f;
        float latB = -31.99212f;
        float longB = 115.763228f;
        decimal distance = (DistanceBetween(latA, latB, longA, longB));
        Console.WriteLine("Distance is" + distance);
        Console.ReadLine();
    }

    static decimal DistanceBetween(float latA, float longA, float latB, float longB)
    {
        var RadianLatA = Math.PI * latA / 180;
        var RadianLatb = Math.PI * latB / 180;
        var RadianLongA = Math.PI * longA / 180;
        var RadianLongB = Math.PI * longB / 180;

        double theDistance = (Math.Sin(RadianLatA)) *
                Math.Sin(RadianLatb) +
                Math.Cos(RadianLatA) *
                Math.Cos(RadianLatb) *
                Math.Cos(RadianLongA - RadianLongB);

        return Convert.ToDecimal(((Math.Acos(theDistance) * (180.0 / Math.PI)))) * 69.09M * 1.6093M;
    }

这是根据在此站点上找到的响应改编而成的
Distance between addresses

关于出了什么问题的任何想法/

谢谢
瑞安

最佳答案

我通常使用的类是GeoCoordinate

double latA = -31.997976f;
double longA = 115.762877f;
double latB = -31.99212f;
double longB = 115.763228f;

var locA = new GeoCoordinate(latA, longA);
var locB = new GeoCoordinate(latB, longB);
double distance = locA.GetDistanceTo(locB ); // metres

关于c# - 计算C#中2个点之间的距离,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22686161/

10-11 11:57