C# 如何计算两个坐标之间的距离
我需要在使用c#的windows 8.1应用程序中查找两点之间的距离(Km),因此我使用了以下函数,但返回值不正确。请提供任何帮助:C# 如何计算两个坐标之间的距离,c#,C#,我需要在使用c#的windows 8.1应用程序中查找两点之间的距离(Km),因此我使用了以下函数,但返回值不正确。请提供任何帮助: public static double DistanceTo(Double latitude1, Double longitude1, Double latitude2, Double longitude2) { var a = latitude1 - latitude2; var b = longitude1 - lon
public static double DistanceTo(Double latitude1, Double longitude1, Double latitude2, Double longitude2)
{
var a = latitude1 - latitude2;
var b = longitude1 - longitude2;
return Math.Sqrt(a * a + b * b);
}
你用错了公式。这就是你没有得到正确结果的原因。 您使用的公式是我们用于计算两点之间距离的公式 在同一平面上,可以用毕达哥拉定理证明。然而,当我们想要计算球体表面两点之间的距离时(我们假设地球是一个完美的球体),我们不使用这种类型 是一个具有正确公式的链接,是一个JavaScript实现 下面,我有一个C语言的实现# 首先,我们必须定义一个方法,该方法将角度作为参数,并以弧度为单位返回其值
public double ConvertToRadians(double angle)
{
return (Math.PI / 180) * angle;
}
然后我们可以定义计算距离的方法:
public static double DistanceTo(double latitude1,
double longitude1,
double latitude2,
double longitude2)
{
// The radius of the earth in Km.
// You could also use a better estimation of the radius of the earth
// using decimals digits, but you have to change then the int to double.
int R = 6371;
double f1 = ConvertToRadians(latitude1);
double f2 = ConvertToRadians(latitude2);
double df = ConvertToRadians(latitude1-latitude2);
double dl = ConvertToRadians(longitude1-longitude2);
double a = Math.Sin(dφ/2) * Math.Sin(dφ/2) +
Math.Cos(f1) * Math.Cos(f2) *
Math.Sin(dλ/2) * Math.Sin(dλ/2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a));
// Calculate the distance.
double d = R * c;
return d;
}
如果不想像上面那样实现它,可以使用geocordination
类
表示由纬度和纬度确定的地理位置
经度坐标。还可能包括高度、精度、速度和
课程信息
如果您这样做,那么:
var point1 = new GeoCoordinate(latitude1, longitude1);
var point2 = new GeoCoordinate(latitude2, latitude2);
然后得到点1
和点2
之间的距离,如下所示:
point1.GetDistanceTo(point2);
试着这样做:
var coord1 = new GeoCoordinate(lat1, long1);
var coord2 = new GeoCoordinate(lat2, long2);
var distance = coord1.GetDistanceTo(coord2);
看。看起来像是重复的这个公式给出的是两点之间的位移,而不是距离。@bit这个公式实际上没有给你任何有用的东西。有没有公式可以给出距离?@ahmad检查与之相关的重复问题毕达哥拉斯定理只适用于二维欧几里德空间。您需要使用一种处理球体的方法。只要搜索你想做的事情,就会有很多点击。这是一个很好的解释。你也应该把它加到原来的问题上:@PhilipPittle非常感谢你,伙计。