概要
當(dāng)用戶來(lái)到頁(yè)面之后就會(huì)彈出是否授權(quán)彈框,但是如果第一次關(guān)閉之后,下一次再過(guò)來(lái)的話頁(yè)面的授權(quán)彈框就不出現(xiàn)了,針對(duì)于這種情況寫了一個(gè)方法文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-764699.html
微信小程序的,使用的是高德地圖
getLocationPodel() {
let that = this
wx.getSetting({
success: (res) => {
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化進(jìn)入該頁(yè)面,且未授權(quán)
wx.showModal({
title: '是否授權(quán)當(dāng)前位置',
content: '需要獲取您的地理位置,請(qǐng)確認(rèn)授權(quán),否則無(wú)法獲取您所需數(shù)據(jù)',
success: function (res) {
if (res.cancel) {
} else if (res.confirm) {
wx.openSetting({
success: function (dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
wx.showToast({
title: '授權(quán)成功',
icon: 'success',
duration: 1000
})
//再次授權(quán),調(diào)用getLocationt的API
that.getLocation1();
} else {
wx.showToast({
title: '授權(quán)失敗',
icon: 'success',
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) {//初始化進(jìn)入
that.getLocation1();
}
else { //授權(quán)后默認(rèn)加載
that.getLocation1();
}
}
})
},
getLocation1() {
let that = this
uni.getLocation({
type: 'wgs84',
success: function (res) {
that.getLocation(res.latitude,res.longitude)
},
fail:function(res) {
console.log('dfvdfvdfv')
}
});
},
getLocation(latitude,longitude) {
let that = this
//根據(jù)經(jīng)緯度獲取城市信息
wx.request({
url: 'https://apis.map.qq.com/ws/geocoder/v1',
data: {
key:'自己的key值,
location:`${latitude},${longitude}`
},
success: (res) => {
let address = res.data.result.address_component
that.city = address.city
that.province = address.province
},
fail: () => {},
complete: () => {}
});
},
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-764699.html
到了這里,關(guān)于小程序判斷是否授權(quán)位置信息和手動(dòng)授權(quán)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!