使用初始坐标,航向和距离计算未来纬度经度

时间:2015-06-18 19:20:04

标签: c# latitude-longitude heading bearing

我一直在阅读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));
  }

1 个答案:

答案 0 :(得分:0)

我使用以下功能。 浮子为您提供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;
    }

当我从不同的类中提取代码时,我希望没有遗漏任何内容。

从公里得到输入参数DistInRad:

  float raddist = distanceKilometers * 1000f * SxMath.RadPerMeter ;