经纬度坐标计算距离

2种方法都是在网上搜索来的,一种原来是java写的,一种是.net写的,java的那个我翻译成.net的了.


1:原JAVA


double distanceByLnglat(double _Longitude1,


   double _Latidute1,


   double _Longitude2,


   double _Latidute2)


  {


   


   double radLat1 = _Latidute1 * Math.PI / 180;


   double radLat2 = _Latidute2 * Math.PI / 180;


   double a = radLat1 - radLat2;


   double b = _Longitude1 * Math.PI / 180 - _Longitude2 * Math.PI / 180;


   double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));


   s = s * 6378137.0;// 取WGS84标准参考椭球中的地球长半径(单位:m)


   s = Math.Round(s * 10000) / 10000;


   return s;


  }


 


 


 2:原.Net


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


   


  }


 


测试代码:


DateTime   dt1=DateTime.Now;


   DateTime dt2 = DateTime.Now;


   TimeSpan ts1;


   


   dt1 = DateTime.Now;


   for(int i=0;i< P>


    ss = distanceByLnglat(jd1,wd1,jd2,wd2);


   dt2 = DateTime.Now;


   ts1 = dt2- dt1;


   this.label4.Text =  String.Format("Java:{0:N3}米 计算次数: {1:N:0} 用时: {2:N1} 毫妙",ss,sumCount,ts1.TotalMilliseconds);


   


   dt1=DateTime.Now;


   for(int i=0;i< P>


    ss = GetDistance(jd1,wd1,jd2,wd2);


   dt2 = DateTime.Now;


   ts1 = dt2- dt1;


   this.label3.Text =  String.Format("Net:{0:N3}米 计算次数: {1:N:0} 用时: {2:N1} 毫妙",ss,sumCount,ts1.TotalMilliseconds);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值