在 .NET 核心中查找 2 个坐标之间的距离 [英] Find distance between 2 coordinates in .NET core
问题描述
我需要在 .NET 核心中找到 2 个坐标之间的距离.我试过使用下面提到的代码,
I need to find the distance between 2 coordinates in .NET core. I've tried using the below mentioned code,
var sCoord = new GeoCoordinate(sLatitude, sLongitude);var eCoord = new GeoCoordinate(eLatitude, eLongitude);
var sCoord = new GeoCoordinate(sLatitude, sLongitude); var eCoord = new GeoCoordinate(eLatitude, eLongitude);
返回 sCoord.GetDistanceTo(eCoord);
return sCoord.GetDistanceTo(eCoord);
但是,似乎 .NET 核心不支持 GeoCoordinate 类.有没有其他精确的方法可以使用 .NET Core 中的纬度和经度来计算 2 个坐标之间的距离?
But, it seems like the GeoCoordinate class is not supported in .NET core. Is there any other precise way to calculate the distance between 2 coordinates using the latitude and longitude in .NET core?
推荐答案
GeoCoordinate 类是 .net 框架中 System.Device.dll 的一部分.但是,.Net Core 不支持它.我找到了找到 2 个坐标之间距离的替代方法.
GeoCoordinate class is part of System.Device.dll in .net framework. But, it is not supported in .Net Core. I have found alternative methods to find the distance between 2 coordinates.
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是一个类,如下图,
where point1 and point2 are 2 points with the coordinates and Location is a class as shown below,
public class Location
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
请检查链接以获取另一种计算距离的方法,其结果与上述代码相同 -替代方法
Please check the link for another method to calculate the distance, which gives the same result as the above code - Alternative method
这篇关于在 .NET 核心中查找 2 个坐标之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!