0
点赞
收藏
分享

微信扫一扫

微信小程序 计算两地之间的距离

心如止水_c736 2022-02-16 阅读 80

 // 封装函数 计算距离

  distance: function (la1, lo1, la2, lo2) {
      var La1 = la1 * Math.PI / 180.0;
      var La2 = la2 * Math.PI / 180.0;
      var La3 = La1 - La2;
      var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
      var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
      s = s * 6378.137;
      s = Math.round(s * 10000) / 10000;
      s = s.toFixed(2);
      return s;
    },

// 发送请求查询目的地经度纬度   获取当前位置  使用方法

 onLoad: function (options) {
      let _this = this
        let id = options.id;
        wx.request({
          url: 'http://www.week.com/index.php/msg',
          data:{id:id},
          success(e){
                let data = e.data.data;
                let latitude= e.data.data.latitude;
                let longitude=e.data.data.longitude;
                wx.getLocation({
                  success(e){                                                                                     
                    let la = e.latitude
                    let lo = e.longitude
                    let distance = _this.distance(la,lo,latitude,longitude)
                    _this.setData({distance})
                  }
                })
                _this.setData({data,latitude,longitude})
            }  
        })
}
举报

相关推荐

0 条评论