1、判斷用戶是否授權(quán)定位
checkLocationAuth() {
let that = this
wx.getSetting({
success: (res) => {
let authSetting = res.authSetting
if (authSetting['scope.userLocation']) {
that.getCurrentLocation()
} else if (authSetting['scope.userLocation'] === false) {
wx.showModal({
title: '您未開啟地理位置授權(quán)',
content: '請在系統(tǒng)設(shè)置中打開位置授權(quán),以便我們?yōu)槟峁└玫姆?wù)',
success: (res) => {
if (res.confirm) {
wx.openSetting()
}
}
})
} else {
wx.authorize({
scope: 'scope.userLocation',
success: () => {
that.getCurrentLocation()
},
fail: () => {
wx.showModal({
title: '您未開啟地理位置授權(quán)',
content: '請在系統(tǒng)設(shè)置中打開位置授權(quán),以便我們?yōu)槟峁└玫姆?wù)',
success: (res) => {
if (res.confirm) {
wx.openSetting()
}
}
})
}
})
}
}
})
}
2、獲取當(dāng)前的地理位置
getCurrentLocation() {
let that = this
wx.getLocation({
type: 'gcj02',
success: function (res) {
console.log("當(dāng)前坐標(biāo)信息:", res)
that.setData({latitude: res.latitude,longitude: res.longitude})
},
fail: function(err) {
console.log(err);
if(err.errMsg === 'getLocation:fail:auth denied') {
wx.showToast({
title: '拒絕授權(quán)',
icon: 'none'
})
} else if(err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' ||
res.errMsg === 'getLocation:fail system permission denied' ||
res.errMsg === 'getLocation:fail:system permission denied') {
wx.showModal({
title: '您未開啟地理位置授權(quán)',
content: '請在系統(tǒng)設(shè)置中打開定位服務(wù),以便我們?yōu)槟峁└玫姆?wù)',
showCancel: false
})
}
}
})
}
在實現(xiàn)獲取用戶位置信息功能時,要先判斷用戶是否給小程序授權(quán)定位信息
如果未授權(quán),則打開授權(quán)位置信息
可以手動開啟,通過手機(jī)右上角的三個點打開設(shè)置
文章來源:http://www.zghlxwxcb.cn/news/detail-587383.html
如果已授權(quán),則獲取用戶當(dāng)前定理位置,如果獲取失敗,可能是手機(jī)系統(tǒng)沒有打開定位功能,需要提醒用戶手動開啟
安卓報錯信息:getLocation:fail:ERROR_NOCELL&WIFI_LOCAL
文章來源地址http://www.zghlxwxcb.cn/news/detail-587383.html
到了這里,關(guān)于微信小程序判斷用戶是否授權(quán)定位的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!