通过经度纬度得到距离

C#的写法

public struct EarthPoint
  {
   public const double Ea = 6378137; // 赤道半径 WGS84标准参考椭球中的地球长半径(单位:m) 
   public const double Eb = 6356725; // 极半径  
   public readonly double Longitude,Latidute;
   public readonly double Jd;
   public readonly double Wd;
   public readonly double Ec;
   public readonly double Ed;
   public EarthPoint(double _Longitude,double _Latidute)
   {
    Longitude = _Longitude;
    Latidute = _Latidute;
    Jd = Longitude * Math.PI / 180; //转换成角度
    Wd = Latidute * Math.PI /180; //转换成角度
    Ec = Eb + (Ea - Eb) * (90 - Latidute) / 90;
    Ed = Ec * Math.Cos(Wd);
   }
   public double Distance(EarthPoint _Point)
   {
    double dx = (_Point.Jd - Jd) * Ed;
    double dy = (_Point.Wd - Wd) * Ec;
    return Math.Sqrt(dx * dx + dy *dy);
   }
  }

public static Double GetDistance(double _Longitude1,
   double _Latidute1,
   double _Longitude2,
   double _Latidute2)
  {
   EarthPoint p1 = new EarthPoint(_Longitude1,_Latidute1);
   EarthPoint p2 = new EarthPoint(_Longitude2,_Latidute2);
   return p1.Distance(p2);
  }

C#的写法,可惜不会用,所以将上面的代码改造一下,形成C++的写法

C++的写法:

#include <math.h>
const double Ea = 6378137; // 赤道半径 WGS84标准参考椭球中的地球长半径(单位:m) 
const double Eb = 6356725; // 极半径
#define PI 3.1416926;
class EarthPoint
{
public: 
    double Longitude,Latidute;
    double Jd;
    double Wd;
    double Ec;
    double Ed;

    EarthPoint(double _Longitude,double _Latidute)
    {
        Longitude = _Longitude;
        Latidute = _Latidute;
        Jd = Longitude * 3.1415926 / 180; //转换成角度
        Wd = Latidute * 3.1415926 / 180; //转换成角度
        Ec = Eb + (Ea - Eb) * (90 - Latidute) / 90;
        Ed = Ec * cos(Wd);
    }

    double Distance(EarthPoint * _Point)
    {
        double dx = (_Point->Jd - Jd) * Ed;
        double dy = (_Point->Wd - Wd) * Ec;
        return sqrt(dx * dx + dy *dy);
    }
};

double GetDistance(double _Longitude1,
                   double _Latidute1,
                   double _Longitude2,
                   double _Latidute2)
{
    EarthPoint *p1 = new EarthPoint(_Longitude1,_Latidute1);
    EarthPoint *p2 = new EarthPoint(_Longitude2,_Latidute2);
    double d=p1->Distance(p2);
    return d;
}

posted on 2011-01-31 15:20  carekee  阅读(435)  评论(1)    收藏  举报