小程序until工具函数 简单计算两者之间的距离

until / index.js 下的封装的函数

// 计算距离
/**
 * @param {Number} lat1  // 当前位置
 * @param {Number} lng1	 // 当前位置
 * @param {Number} lat2  // 要去的位置
 * @param {Number} lng2	 // 要去的位置
 */
export function GetDistance( lat1,  lng1,  lat2,  lng2){
	let radLat1 = lat1*Math.PI / 180.0;
	let radLat2 = lat2*Math.PI / 180.0;
	let a = radLat1 - radLat2;
	let  b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
	let 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 *6378.137 ;// EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000;
	return s.toFixed(2);
}

调用
需要的文件引入

import { GetDistance } from "@/utils/"

获取当前位置方法

    /**
      * 获取地址位置
      */
     getPosition() {
       return new Promise((resolve, reject) => {
         setTimeout(() => {
           wx.getLocation({
             type: "gcj02",
             success: res => {
               this.currentLat = res.latitude;
               this.currentLng = res.longitude;
               resolve();
             },
			fail: err =>{
			   reject(err)
			}
          });
       	}, 200);
       });
     },

引用

this.getPosition().then(resolve => {
	//请求获取要去的位置坐标
	request.post('').then(res => {
		this.distance = GetDistance(this.currentLat, this.currentLng, res.data.lat, res.data.lng)
	}).catch(err => {
		console.log("获取两字之间的距离错误")
	})
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值