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

标签 .net asp.net-core .net-core geolocation geospatial

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

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

返回 sCoord.GetDistanceTo(eCoord);

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

最佳答案

GeoCoordinate 类是 .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)));
    }
其中 point1 和 point2 是 2 个坐标点,Location 是一个类,如下所示,
public class Location
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}
请检查链接以获取另一种计算距离的方法,其结果与上述代码相同 -
Alternative method

关于.net - 在 .NET 核心中查找 2 个坐标之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60700865/

相关文章:

macos - Visual Studio Mac Preview Entity Framework SQLite 添加迁移

linux - 如何在 Linux Azure Function 启动命令上安装 libgdiplus apt 包?

c# - 通过 C# 中的反射访问 ICollection<SomeInnerClass> 的内部类型

.net - F# - 将简单的 for 循环转化为更多功能的结构

c# - 在一个命名空间下的多个子文件夹中重新组合 Razor 组件 - Blazor

c# - SwashBuckle 定义错误时的响应主体

c# - .NET Core - 全局化和本地化 - 类库

c# - 正则表达式 'or' 运算符避免重复

c# - MSTest:CollectionAssert.AreEquivalent 失败。预期的集合包含 1 次出现

angular - 即使在使用 .NET Core 和 Angular 客户端启用它之后,CORS 错误仍然会发生