通过经度纬度得到距离

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值