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("获取两字之间的距离错误")
})
})