您使用了错误的公式。这就是你没有得到正确结果的原因。 您使用的公式是我们用于计算同一平面上两个点之间的距离 的公式,可以使用毕达哥拉定理来证明。然而,当我们想要计算球体表面上两点之间的距离(我们假设地球是一个完美的球体)时,我们不使用这种类型。
Here是一个带有正确公式和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;
}
如果你不想实现它如上,你可以使用GeoCoordinate
类,它
表示由纬度和经度坐标确定的地理位置。还可能包括高度,准确度,速度和 课程信息。
如果这样做,那么:
var point1 = new GeoCoordinate(latitude1, longitude1);
var point2 = new GeoCoordinate(latitude2, latitude2);
,然后你会得到像下面point1
和point2
之间的距离:
point1.GetDistanceTo(point2);
该公式给出了两点之间的位移而不是距离。 – bit 2014-10-02 08:43:32
@bit这个公式并没有给你任何有用的东西。 – DavidG 2014-10-02 08:44:46
有没有给我距离的公式? – ahmad 2014-10-02 08:44:48