1.先在data里面定义好圆周率以及地球半径
data: {
PI: 3.14159,//圆周率
EARTH_RADIUS: 6378137.0,//地球半径
},
2.开始定义计算
getRad(d) {
return d * this.data.PI / 180.0;
},
/**
*
* @param lat1 第一个纬度
* @param lng1第一个经度
* @param lat2第二个纬度
* @param lng2第二个经度
* @return 两个经纬度的距离
*/
getGreatCircleDistance(lat1, lng1, lat2, lng2) {
var radLat1 = this.getRad(lat1);
var radLat2 = this.getRad(lat2);
var a = radLat1 - radLat2;
var b = this.getRad(lng1) - this.getRad(lng2);
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 * this.data.EARTH_RADIUS;
s = Math.round(s * 10000) / 10000.0;
return s;
},
3开始调用
var that =this;
var jl = that.getGreatCircleDistance(26.85125, 106.737547, that.data.latitude, that.data.longitude);
if(jl > 2000){
wx.showToast({
title: '距离不在范围之内,无法签到',
icon: 'none',
duration: 10000
});
}else{
that.ksqd(e);
}