Calculate distance of two geo points in km c#

2020-01-30 08:00发布

I`d like calculate the distance of two geo points. the points are given in longitude and latitude.

the coordinates are:

point 1: 36.578581, -118.291994

point 2: 36.23998, -116.83171

here a website to compare the results:

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

here the code I used from this link: Calculate distance between two points in google maps V3

    const double PIx = Math.PI;
    const double RADIO = 6378.16;

    /// <summary>
    /// Convert degrees to Radians
    /// </summary>
    /// <param name="x">Degrees</param>
    /// <returns>The equivalent in radians</returns>
    public static double Radians(double x)
    {
        return x * PIx / 180;
    }

    /// <summary>
    /// Calculate the distance between two places.
    /// </summary>
    /// <param name="lon1"></param>
    /// <param name="lat1"></param>
    /// <param name="lon2"></param>
    /// <param name="lat2"></param>
    /// <returns></returns>
    public static double DistanceBetweenPlaces(double lon1, double lat1, double lon2, double lat2)
    {
        double R = 6371; // km
        double dLat = Radians(lat2 - lat1);
        double dLon = Radians(lon2 - lon1);
        lat1 = Radians(lat1);
        lat2 = Radians(lat2);

        double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        double d = R * c;

        return d;
    }


Console.WriteLine(DistanceAlgorithm.DistanceBetweenPlaces(36.578581, -118.291994, 36.23998, -116.83171));

the issue is that I get two different results.

my result: 163,307 km

result of the website: 136 km

any suggestions???

torti

7条回答
相关推荐>>
2楼-- · 2020-01-30 08:39

try this... I have used this is apps before -- its pretty accurate. Forgive me for not giving due credit to the brilliant soul who originally published this, I transposed it from java to C#:

namespace Sample.Geography
{
    using System;

public class GeodesicDistance
{
    private static double DegsToRadians(double degrees)
    {
        return (0.017453292519943295 * degrees);
    }

    public static double? GetDistance(double lat1, double lon1, double lat2, double lon2)
    {
        long num = 0x615299L;
        double num2 = 6356752.3142;
        double num3 = 0.0033528106647474805;
        double num4 = DegsToRadians(lon2 - lon1);
        double a = Math.Atan((1 - num3) * Math.Tan(DegsToRadians(lat1)));
        double num6 = Math.Atan((1 - num3) * Math.Tan(DegsToRadians(lat2)));
        double num7 = Math.Sin(a);
        double num8 = Math.Sin(num6);
        double num9 = Math.Cos(a);
        double num10 = Math.Cos(num6);
        double num11 = num4;
        double num12 = 6.2831853071795862;
        int num13 = 20;
        double y = 0;
        double x = 0;
        double num18 = 0;
        double num20 = 0;
        double num22 = 0;
        while ((Math.Abs((double) (num11 - num12)) > 1E-12) && (--num13 > 0))
        {
            double num14 = Math.Sin(num11);
            double num15 = Math.Cos(num11);
            y = Math.Sqrt(((num10 * num14) * (num10 * num14)) + (((num9 * num8) - ((num7 * num10) * num15)) * ((num9 * num8) - ((num7 * num10) * num15))));
            if (y == 0)
            {
                return 0;
            }
            x = (num7 * num8) + ((num9 * num10) * num15);
            num18 = Math.Atan2(y, x);
            double num19 = ((num9 * num10) * num14) / y;
            num20 = 1 - (num19 * num19);
            if (num20 == 0)
            {
                num22 = 0;
            }
            else
            {
                num22 = x - (((2 * num7) * num8) / num20);
            }
            double num21 = ((num3 / 16) * num20) * (4 + (num3 * (4 - (3 * num20))));
            num12 = num11;
            num11 = num4 + ((((1 - num21) * num3) * num19) * (num18 + ((num21 * y) * (num22 + ((num21 * x) * (-1 + ((2 * num22) * num22)))))));
        }
        if (num13 == 0)
        {
            return null;
        }
        double num23 = (num20 * ((num * num) - (num2 * num2))) / (num2 * num2);
        double num24 = 1 + ((num23 / 16384) * (4096 + (num23 * (-768 + (num23 * (320 - (175 * num23)))))));
        double num25 = (num23 / 1024) * (256 + (num23 * (-128 + (num23 * (74 - (47 * num23))))));
        double num26 = (num25 * y) * (num22 + ((num25 / 4) * ((x * (-1 + ((2 * num22) * num22))) - ((((num25 / 6) * num22) * (-3 + ((4 * y) * y))) * (-3 + ((4 * num22) * num22))))));
        return new double?((num2 * num24) * (num18 - num26));
    }
}
}
查看更多
登录 后发表回答