/**判断是否授权定位 */
getSetting() {
let that = this;
// 获取当前城市
wx.getSetting({
success: (res) => {
console.log(res)
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { //非初始化进入该页面,且未授权
that.setData({
showMain: true,
})
wx.showModal({
content: `${app.config.referer}获取你的位置信息`,
success: function (res) {
if (res.cancel) {
wx.showToast({
title: '授权失败',
icon: 'success',
duration: 1000
})
wx.reLaunch({
url: `/pages_A/vending_20220701/apply/apply?type=4`
})
} else if (res.confirm) {
wx.openSetting({
success: function (dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
//再次授权,调用getLocationt的API
that.getLocation();
} else {
wx.showToast({
title: '授权失败',
icon: 'success',
duration: 1000
})
wx.reLaunch({
url: `/pages_A/vending_20220701/apply/apply?type=4`
})
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) { //初始化进入
that.getLocation();
that.setData({
showMain: true,
})
} else { //授权后默认加载
that.getLocation();
}
}
})
},
/**获取定位数据 */
getLocation() {
let that = this;
wx.getLocation({
type: 'wgs84',
success(res) {
const latitude = res.latitude
const longitude = res.longitude
console.log(res);
that.setData({
latitude,
longitude,
})
console.log(111)
that.getDistance(latitude, longitude);
},
fail() {
console.log(7777)
wx.reLaunch({
url: `/pages_A/vending_20220701/apply/apply?type=4`
})
}
})
},
进入页面,首先判断小程序的授权情况,根据不同情况做不同的操作,之后走getLocation()方法。wx.getLocation可直接调起按钮让用户同意授权,可直接获取经纬度,也可将经纬度放入接口调用,查询该定位所在省市区