在 .NET 核心中查找 2 个坐标之间的距离

Find distance between 2 coordinates in .NET core

我需要找到 .NET Core 中 2 个坐标之间的距离。我试过使用下面提到的代码,

var sCoord = new GeoCoordinate(sLatitude, sLongitude); var eCoord = new GeoCoordinate(eLatitude, eLongitude);

return sCoord.GetDistanceTo(电子坐标);

但是,.NET 核心似乎不支持 GeoCoordinate class。在.NET core中有没有其他精确的方法来计算2个坐标之间的经纬度距离?

GeoCoordinate class 是 .net 框架中 System.Device.dll 的一部分。但是,.Net Core 不支持它。我找到了其他方法来查找 2 个坐标之间的距离。

    public double CalculateDistance(Location point1, Location point2)
    {
        var d1 = point1.Latitude * (Math.PI / 180.0);
        var num1 = point1.Longitude * (Math.PI / 180.0);
        var d2 = point2.Latitude * (Math.PI / 180.0);
        var num2 = point2.Longitude * (Math.PI / 180.0) - num1;
        var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) +
                 Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0);
        return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)));
    }

where point1 and point2 are 2 points with the coordinates and Location is a class 如下所示,

public class Location
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

请检查 link 另一种计算距离的方法,其结果与上述代码相同 - Alternative method