Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用初始坐标、航向和距离计算未来经纬度_C#_Latitude Longitude_Heading_Bearing - Fatal编程技术网

C# 用初始坐标、航向和距离计算未来经纬度

C# 用初始坐标、航向和距离计算未来经纬度,c#,latitude-longitude,heading,bearing,C#,Latitude Longitude,Heading,Bearing,我一直在阅读stackoverflow和这个网站()关于如何做到这一点,但我无法让我的代码给出正确的答案。它给出的坐标方向不正确。我一整天都在做这件事,似乎遇到了麻烦。这是我的职责: public static void destination() { double heading = 335.9; double startLatitude = 41.8369; double startLongitude = 87.6847; //Convert to

我一直在阅读stackoverflow和这个网站()关于如何做到这一点,但我无法让我的代码给出正确的答案。它给出的坐标方向不正确。我一整天都在做这件事,似乎遇到了麻烦。这是我的职责:

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));
  }
我使用下面的函数。 浮子为您提供3米的精度。如果您需要更多,请使用double

  internal class SxMath
  {
    internal const  float      PI                 = (float)Math.PI;
    internal const  float      x2PI               = PI * 2;
    internal const  float      PIDiv2             = PI/2;
    internal const  float      RadPerSec          = (float)(PI / 648000F);
    internal const  float      SecPerRad          = (float)(648000F / PI);
    internal const  float      RadPerDeg          = PI / 180;
    internal const  float      RadPerMin          = PI / 10800;
    internal const  float      DegPerRad          = 180 / PI;
    internal const  float      MinParRad          = (float)(10800.0/PI);
    internal const  float      RadPerMeter        = RadPerMin * (1F/1852F) /* Meter_To_NMs */ ;

    internal static float RealMod(float val,float modval)
    { // Example : RealMod(3,2*PI)=3 , RealMod(2*PI+3,2*PI)=3 , RealMod(-3,2*PI)=2*PI-3 
      float result = (float)Math.IEEERemainder(val,modval);
      if (result<0) result = result + modval;
      return result;
    }
  } // SxMath

  internal struct SxGeoPt
  {
    internal float lat ; // in radians, N positive
    internal float lon ; // in radians, W positive
  } // SxGeoPt

  internal static SxGeoPt GEO_CoorPointInAzim(SxGeoPt p1,float az,float raddist)
    // This procedure provides coordinates of the point p2 located
    // - at a distance raddist of a point p1
    // - in the direction of azimuth az
    // input   p1        <SxGeoPt> coordinates of reference point
    //         raddist   <float>   distance in radian between p1 and p2
    //         az        <float>   azimut of p2 from p1, 
    //                             (az=0, if p1 and p2 on same longitude and P2 north of P1)
    //                             (az=90, if p1 is on equator and p2 on equtor at East of P1)
    // output  p2        <SxGeoPt> coordinates of resulting point
    {
      SxGeoPt result;
      if (p1.lat>SxMath.PIDiv2-SxMath.RadPerMin)
      { if (az<=SxMath.PI) result.lon=az; else result.lon=az-SxMath.PI; result.lat=SxMath.PIDiv2-raddist; }
      else if (p1.lat<-SxMath.PIDiv2+SxMath.RadPerMin)
      { if (az<=SxMath.PI) result.lon=-az; else result.lon=-az+SxMath.PI; result.lat=-SxMath.PIDiv2+raddist; }
      else
      {
        result.lat = (float)Math.Asin((Math.Sin(p1.lat)*Math.Cos(raddist)) + 
                                  (Math.Cos(p1.lat)*Math.Sin(raddist)*Math.Cos(az)));
        float dlon = (float)Math.Atan2( Math.Sin(az)*Math.Sin(raddist)*Math.Cos(p1.lat), 
                                        Math.Cos(raddist)-Math.Sin(p1.lat)*Math.Sin(result.lat));
        result.lon = SxMath.RealMod(p1.lon-dlon+SxMath.PI,SxMath.x2PI)-SxMath.PI;
      }
      return result;
    }

是的,所以如果你手工计算横向/纵向的距离,那就太糟糕了。有一个名为
DbGeography
的类,它需要一个lat+long,并允许您使用这样的语法来计算两点之间的距离:
firstDbGeography.distance(otherDbGeography)
。这并不是你问题的答案,但它可能有助于清理你的代码(这可能会让错误变得更加明显)。非常感谢你,我觉得我已经差不多明白了,只是它仍然给我指出了错误的方向。如果我用E作为经度的正数,那会弄糟什么吗?比较这两个代码并将我的经度更改为东正数(通常的惯例是什么),区别在于最后一个术语,即endLong=…-在您的计算中使用Math.PI。当我将我的方法转换为East正数时,我发现+Math.PI。
  float raddist = distanceKilometers * 1000f * SxMath.RadPerMeter ;