// 获取当前地地址
async getLocation() {
let self = this
let result = null
try {
const resultLocation = await wepy.getLocation()
result = await this.geocoder(resultLocation)
} catch (e) {
wx.getSetting({
success: function (res) {
console.log('getSetting...', res)
let statu = res.authSetting
if (!statu['scope.userLocation']) {
wx.showModal({
title: '是否授权当前位置',
content: '需要获取您的地理位置,请确认授权,否则将无法获得优惠券',
success: function (tip) {
if (tip.confirm) {
wx.openSetting({
success: function (data) {
console.log('openSetting...', data)
if (data.authSetting['scope.userLocation'] === true) {
wepy.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
// 授权成功之后,再调用chooseLocation选择地方
wepy.getLocation({
success(res) {
self.geocoder(res)
}
})
} else {
// 开关关闭,提示无法拿到位置权限
wx.showModal({
title: '提示',
content: '无法使用定位权限',
confirmText: '太遗憾了',
showCancel: false
})
}
},
fail: function(res) {
console.log('openSetting.failed' + res)
}
})
}
if (tip.cancel) {
wx.showToast({
title: '定位失败',
icon: 'none',
duration: 1000
})
}
}
})
} else {
wepy.showModal({
title: '定位服务已关闭',
content: '请在“设置”中打开定位服务,以便精准定位获取更多优惠消息!'
})
}
},
fail: function (res) {
console.log(res)
wepy.showToast({
title: '调用授权窗口失败',
icon: 'success',
duration: 1000
})
}
})
}
return result
}