c# - 两个坐标之间的地理中点

标签 c# geolocation latitude-longitude

我一直在使用 Moveable-Type 网站来帮助我进行一些地理坐标计算,它非常有用,但是,我在计算两个坐标之间的中点时遇到了一个错误。我的结果接近预期,但还不够接近:

posA = {47.64570362, -122.14073746}
posB = {47.64316917, -122.14032175}

预期结果(取自活字计算器)= 47°38′40″N, 122°08′26″W = {47.644444, -122.140556} 我的结果:{ 49.6054801645915, -122.14052959995759}

这是我的代码:

private Geocoordinate MidPoint(Geocoordinate posA, Geocoordinate posB)
{
   Geocoordinate midPoint = new Geocoordinate();

   double dLon = DegreesToRadians(posB.Longitude - posA.Longitude);
   double Bx = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Cos(dLon);
   double By = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Sin(dLon);

   midPoint.Latitude = RadiansToDegrees(Math.Atan2(Math.Sin(DegreesToRadians(posA.Latitude)) + Math.Sin(DegreesToRadians(posB.Latitude)), 
                Math.Sqrt((Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) * (Math.Cos(DegreesToRadians(posA.Latitude))) + Bx) + By * By));

   midPoint.Longitude = posA.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(posA.Latitude)) + Bx));

   return midPoint;
}

我有几个私有(private)方法可以在度数和弧度之间来回转换。 例如

private double DegreeToRadian(double angle)
{
   return Math.PI * angle / 180.0;
}

我不明白为什么我的结果在 Lat 值上有几个度数的偏差。有什么想法吗?

谢谢

最佳答案

你放错了一些括号。我把代码里的地方标出来了。

private Geocoordinate MidPoint(Geocoordinate posA, Geocoordinate posB)
{
   Geocoordinate midPoint = new Geocoordinate();

   double dLon = DegreesToRadians(posB.Longitude - posA.Longitude);
   double Bx = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Cos(dLon);
   double By = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Sin(dLon);

   midPoint.Latitude = RadiansToDegrees(Math.Atan2(
                Math.Sin(DegreesToRadians(posA.Latitude)) + Math.Sin(DegreesToRadians(posB.Latitude)),
                Math.Sqrt(
                    (Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) *
                    (Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) + By * By))); 
                 // (Math.Cos(DegreesToRadians(posA.Latitude))) + Bx) + By * By)); // Your Code

   midPoint.Longitude = posA.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(posA.Latitude)) + Bx));

   return midPoint;
}

关于c# - 两个坐标之间的地理中点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4164830/

相关文章:

C# 动态 getter 和 setter 取决于应用上下文

c# - 桌面 VB.NET <> C# 转换器

geolocation - 通过 API 从 Instagram 照片中提取纬度/经度

ios - 如何在 iOS 中获取没有当前位置(lat-long)的城市名称?

c# - 如何通过 SignalR 实现 'Who is typing' 功能?

c# - 通用类型属性的任何替代解决方案?

javascript - 计算地理位置距离返回 NaN

flutter - 当应用程序关闭/终止或屏幕关闭时,如何让 Flutter 应用程序在后台每 10 秒连续运行一次获取数据

php - 查找数据库中在一组纬度和经度点的特定距离内的所有记录

php - 更新数据库中的谷歌地图纬度/经度