微信小程序 功能函数 地图定位相对直线距离

 
GetDistance:function(lat1, lng1, lat2, lng2){
// console.log(lat1)
var radLat1 = lat1 * Math.PI / 180.0;
var radLat2 = lat2 * Math.PI / 180.0;
 
var a = radLat1 - radLat2;
 
var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
 
var 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;
 
s = Math.round(s * 10000) / 10000;
console.log(s)
return s;
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let that = this;
let a1,a2,b1,b2;
wx.chooseLocation({
success: function (res) {
a1 = res.latitude;
a2=res.longitude;
wx.chooseLocation({
success: function (res) {
b1 = res.latitude;
b2 = res.longitude;
console.log(a1)
console.log(a2)
console.log(b1)
console.log(b2)
that.GetDistance(a1, a2, b1, b2)
}
})
}
})
// wx.getLocation({
// type: 'wgs84',
// success: function (res) {
// console.log(res.latitude)
// console.log(res.longitude)
// }
// })
 
 
},
原文地址:https://www.cnblogs.com/dianzan/p/8319940.html