c# haversine实现给出了与googlemaps/movable-type.co.uk不同的答案
本文关键字:uk co movable-type 答案 googlemaps 实现 haversine | 更新日期: 2023-09-27 18:15:12
我需要计算两点之间的距离(给定纬度&经度)。我在c#中实现了标准的haversine公式
private double toRadian(double val)
{
return (Math.PI / 180) *
}
public double Distance(Position pos1, Position pos2,DistanceType type)
{
double R = (type == DistanceType.Miles) ? 3960 : 6378137; // 6318137 in meters
double dLat = toRadian(pos2.Latitude - pos1.Latitude);
double dLon = toRadian(pos2.Longitude - pos2.Longitude);
double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Cos(this.toRadian(pos1.Latitude)) * Math.Cos(this.toRadian(pos2.Latitude)) *
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double d = R * c;
return d;
}
但我从代码和网站http://www.movable-type.co.uk/scripts/latlong.html得到不同的答案。例如输入:pos1.Latitude = 12.916822
、Pos1.Longitude = 77.599816
和pos2。纬度= 12.917135和Pos2。经度= 77.585783我的程序返回34.843(当我在答案上做一个Math.Round((dist), 4)
时),因为网站给出1.521公里有人能指出什么地方不对吗?PS:我的程序以米计算距离
当我快速查看它时,这应该是一个容易的修复。修改以下内容如何:
double dLon = toRadian(pos2.Longitude - pos2.Longitude);
…成:
double dLon = toRadian(pos2.Longitude - pos1.Longitude);
注意到pos1/pos2的错字了吗?:)
我建议更改您的实现,使用GeoCoordinate
上提供的内置dotnet