【发布时间】:2015-06-18 19:20:03
【问题描述】:
我一直在阅读 stackoverflow 和这个网站 (http://www.movable-type.co.uk/scripts/latlong.html) 关于如何做到这一点,但我无法让我的代码给出正确答案。它给出的坐标方向不正确。我整天都在做这件事,似乎碰壁了。这是我的功能:
public static void destination()
{
double heading = 335.9;
double startLatitude = 41.8369;
double startLongitude = 87.6847;
//Convert to Radians
startLatitude = startLatitude * Math.PI / 180;
startLongitude = startLongitude * Math.PI / 180;
heading = heading * Math.PI / 180;
int distanceKilometers = 100;
double angularDistance = distanceKilometers / 6371e3;
double endLat = Math.Asin((Math.Sin(startLatitude) * Math.Cos(angularDistance)) +
(Math.Cos(startLatitude) * Math.Sin(angularDistance) * Math.Cos(heading)));
double endLong = startLongitude + (Math.Atan2((Math.Sin(heading) * Math.Sin(angularDistance) * Math.Cos(startLatitude)),
Math.Cos((angularDistance) - (Math.Sin(startLatitude) * Math.Sin(endLat)))));
endLong = (endLong + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
Console.WriteLine("endLatitude: " + (endLat * 180 / Math.PI) + " endLongitude: " + (endLong * 180 / Math.PI));
}
【问题讨论】:
-
是的,所以如果你手动计算纬度/经度之间的距离很糟糕。有一个名为
DbGeography的类需要一个lat+long,并允许您使用这样的语法来计算两点之间的距离:firstDbGeography.Distance(otherDbGeography)。不是您问题的真正答案,但它可能有助于清理您的代码(这可能会使错误更加明显)
标签: c# latitude-longitude heading bearing