计算两个地理点的距离(以公里为单位)c#
我想计算两个地理点的距离。这些点以经度和纬度给出。
坐标为:
点 1:36.578581,-118.291994
点 2:36.23998,-116.83171
这里有一个比较结果的网站:
http://www.movable-type.co.uk/scripts/latlong.html
这里是我从此链接中使用的代码: 计算 Google 地图 V3 中两点之间的距离
const double PIx = Math.PI;
const double RADIO = 6378.16;
/// <summary>
/// Convert degrees to Radians
/// </summary>
/// <param name="x">Degrees</param>
/// <returns>The equivalent in radians</returns>
public static double Radians(double x)
{
return x * PIx / 180;
}
/// <summary>
/// Calculate the distance between two places.
/// </summary>
/// <param name="lon1"></param>
/// <param name="lat1"></param>
/// <param name="lon2"></param>
/// <param name="lat2"></param>
/// <returns></returns>
public static double DistanceBetweenPlaces(double lon1, double lat1, double lon2, double lat2)
{
double R = 6371; // km
double dLat = Radians(lat2 - lat1);
double dLon = Radians(lon2 - lon1);
lat1 = Radians(lat1);
lat2 = Radians(lat2);
double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double d = R * c;
return d;
}
Console.WriteLine(DistanceAlgorithm.DistanceBetweenPlaces(36.578581, -118.291994, 36.23998, -116.83171));
问题是我得到两个不同的结果。
我的结果:163,307 公里
网站结果:136 公里
有什么建议吗???
托蒂
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的公式几乎是正确的,但您必须交换经度和纬度的参数
我使用简化的公式:
测试:(
赤道距离):经度 0, 100;纬度 = 0,0; DistanceBetweenPlaces(0, 0, 100, 0) = 11119.5 km
(北极距离):经度 0, 100;纬度 = 90,90;地点之间的距离(0, 90, 100, 90) = 0 公里
经度: -118.291994, -116.83171;纬度:36.578581, 36.23998 = 135.6 公里
经度:36.578581, 36.23998;纬度:-118.291994, -116.83171 =
公里
163.2 在 网站 您用于结果比较,每个点第一个文本框是纬度,第二个是经度
Your formula is almost correct, but you have to swap parameters for longitude an latitude
I'm using simplified formula:
Testing:
(Distance at Equator): Longitudes 0, 100; Latitudes = 0,0; DistanceBetweenPlaces(0, 0, 100, 0) = 11119.5 km
(Distance at North Pole): Longitudes 0, 100; Latitudes = 90,90; DistanceBetweenPlaces(0, 90, 100, 90) = 0 km
Longitudes: -118.291994, -116.83171; Latitudes: 36.578581, 36.23998 = 135.6 km
Longitudes: 36.578581, 36.23998; Latitudes: -118.291994, -116.83171 = 163.2 km
Best regards
P.S. At web site you use for result comparison, for every point first text box is latitude, second - longitude
由于您使用的是框架 4.0,我建议使用
GeoCooperative
类。您必须添加对 System.Device.dll 的引用。
As you are using the framework 4.0, I would suggest the
GeoCoordinate
class.You have to add a reference to System.Device.dll.
在我几年前发表的文章中(链接:http ://www.codeproject.com/Articles/469500/Edumatter-School-Math-Calculators-and-Equation-Sol)我有描述了 3 个有用的
函数
来计算 2 个地理点之间的距离(换句话说,地球上 2 个地理点之间的大圆(正向)距离),其在精度/性能方面有所不同:函数返回以英里为单位的结果;要查找以公里为单位的距离,请将结果乘以 1.60934(请参阅
private const double _m2km = 1.60934
)。与示例相关:找到距离点1(36.578581,-118.291994)和点2(36.23998,-116.83171)上述三个函数产生以下结果(公里):
和计算器(链接:http://www.movable-type.co.uk/scripts/latlong.html)给出的结果:136.0
希望这可能有所帮助。此致,
In my article published several years ago (link: http://www.codeproject.com/Articles/469500/Edumatter-School-Math-Calculators-and-Equation-Sol) I have described 3 useful
Functions
to calculate the distance between 2 geo-points (in other words, great-circle (orthodromic) distance on Earth between 2 geo-points), which differs in terms of accuracy/performance:Functions return results in miles; to find the distance in km multiply the result by 1.60934 (see
private const double _m2km = 1.60934
).Pertinent to the sample: find the distance point1 (36.578581, -118.291994) and point2 (36.23998, -116.83171) the three aforementioned Function produced the following results (km):
and the calculator (link: http://www.movable-type.co.uk/scripts/latlong.html) gave the result: 136.0
Hope this may help. Best regards,
我使用了 Wikipedia 中的公式并将其放入 lambda 函数中:
I used the formula from Wikipedia and put it in a lambda function:
试试这个...我以前用过这个应用程序——它非常准确。请原谅我没有给予最初发表此文章的杰出灵魂应有的荣誉,我将其从 java 转换为 C#:
try this... I have used this is apps before -- its pretty accurate. Forgive me for not giving due credit to the brilliant soul who originally published this, I transposed it from java to C#:
我刚刚尝试在 GeoDataSource 上编码,它运行得很好:
http://www.geodatasource.com/developers/c-sharp
I just tried to code at GeoDataSource, and it worked perfectly well:
http://www.geodatasource.com/developers/c-sharp
我认为您正在交换纬度和经度值。尝试更正这些或更改参数顺序。
I think you are interchanging latitude and longitude values. Try correcting those or change sequence of parameters.