美文网首页
根据两点经纬度计算距离

根据两点经纬度计算距离

作者: 溪桥星雨 | 来源:发表于2021-06-07 13:29 被阅读0次

    因为地球是个球形的,所以根据两地的经纬度是可以计算出距离的。原理就不多说了。直接上代码吧。

    swift版本:

    static func calculateLineDistance(var0:LonLat, var1:LonLat) -> Double {
            let DEG_TO_RAD:Double = 0.0174532925199432958
            var var2 = Double(var0.lon!);
            var var4 = Double(var0.lat!);
            var var6 = Double(var1.lon!);
            var var8 = Double(var1.lat!);
            var2 = DEG_TO_RAD * var2!;
            var4 = DEG_TO_RAD * var4!;
            var6 = DEG_TO_RAD * var6!;
            var8 = DEG_TO_RAD * var8!;
            let var10 = sin(var2!);
            let var12 = sin(var4!);
            let var14 = cos(var2!);
            let var16 = cos(var4!);
            let var18 = sin(var6!);
            let var20 = sin(var8!);
            let var22 = cos(var6!);
            let var24 = cos(var8!);
            var var28 = Array<Double>()
            var var29 = Array<Double>()
            var28.append(var16 * var14)
            var28.append(var16 * var10)
            var28.append(var12)
            var29.append(var24 * var22)
            var29.append(var24 * var18)
            var29.append(var20)
            return (asin(sqrt((var28[0] - var29[0]) * (var28[0] - var29[0]) + (var28[1] - var29[1]) * (var28[1] - var29[1]) + (var28[2] - var29[2]) * (var28[2] - var29[2])) / 2.0) * 1.27420015798544E7)
        }
    

    Dart版本:

      static const double DEG_TO_RAD = 0.0174532925199432958;
    
      static double calculateLineDistance(LatLng var0, LatLng var1) {
        if(var0 != null && var1 != null){
          double var2 = var0.longitude;
          double var4 = var0.latitude;
          double var6 = var1.longitude;
          double var8 = var1.latitude;
          var2 *= DEG_TO_RAD;
          var4 *= DEG_TO_RAD;
          var6 *= DEG_TO_RAD;
          var8 *= DEG_TO_RAD;
          double var10 = sin(var2);
          double var12 = sin(var4);
          double var14 = cos(var2);
          double var16 =cos(var4);
          double var18 = sin(var6);
          double var20 = sin(var8);
          double var22 = cos(var6);
          double var24 = cos(var8);
          List<double> var28 = List<double>(3);
          List<double> var29 = List<double>(3);
          var28[0] = var16 * var14;
          var28[1] = var16 * var10;
          var28[2] = var12;
          var29[0] = var24 * var22;
          var29[1] = var24 * var18;
          var29[2] = var20;
          return (asin(sqrt((var28[0] - var29[0]) * (var28[0] - var29[0]) + (var28[1] - var29[1]) * (var28[1] - var29[1]) + (var28[2] - var29[2]) * (var28[2] - var29[2])) / 2.0) * 1.27420015798544E7);
        }else{
          return 0.0;
        }
      }
    

    点击下方赞赏,给作者一点鼓励!

    相关文章

      网友评论

          本文标题:根据两点经纬度计算距离

          本文链接:https://www.haomeiwen.com/subject/qjvosltx.html