getUserLocation: function () {
let vm = this;
wx.getSetting({
success: (res) => {
console.log(JSON.stringify(res))
// res.authSetting[‘scope.userLocation‘] == undefined 表示 初始化进入该页面
// res.authSetting[‘scope.userLocation‘] == false 表示 非初始化进入该页面,且未授权
// res.authSetting[‘scope.userLocation‘] == true 表示 地理位置授权
if (res.authSetting[‘scope.userLocation‘] != undefined && res.authSetting[‘scope.userLocation‘] != true) {
wx.showModal({
title: ‘请求授权当前位置‘,
content: ‘需要获取您的地理位置,请确认授权‘,
success: function (res) {
if (res.cancel) {
wx.showToast({
title: ‘拒绝授权‘,
icon: ‘none‘,
duration: 1000
})
} else if (res.confirm) {
wx.openSetting({
success: function (dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
wx.showToast({
title: ‘授权成功‘,
icon: ‘success‘,
duration: 1000
})
//再次授权,调用wx.getLocation的API
vm.getLocation();
} else {
wx.showToast({
title: ‘授权失败‘,
icon: ‘none‘,
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting[‘scope.userLocation‘] == undefined) {
//调用wx.getLocation的API
vm.getLocation();
}
else {
//调用wx.getLocation的API
vm.getLocation();
}
}
})
},
// 获取用户的当前位置
getLocation: function () {
let that= this;
wx.getLocation({
type: ‘wgs84‘,
success: function (res) {
var la1 = res.latitude
var lo2 = res.longitude
that.setData({
la1: la1,
lo2: lo2,
scale:28
})
that.getShangjia(la1, lo2);
}
})
},
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;
console.log("计算结果", s)
this.setData({
s: s
})
},
//商家
getShangjia: function (la1, lo2) {
var that = this;
app.util.request({
‘url‘: ‘entry/wxapp/Shangjia‘,
success: function (res) {
console.log(res.data.data);
var shangjia = res.data.data
for (var i = 0; i < shangjia.length; i++) {
that.distance(la1, lo2, shangjia[i].s_latitude, shangjia[i].s_longitude)
shangjia[i].s_shopjuli = that.data.s;
}
that.setData({
shangjia: res.data.data
})
},
fail: function (err) {
console.log(err)
},
});
},