需求:小程序退出或者微信在后時(shí),實(shí)時(shí)獲取當(dāng)前位置
代碼部分:
wx.startLocationUpdateBackground({
success(res1){
console.log("開啟前后臺(tái)位置更新",res1)
wx.onLocationChange((res2)=>{
console.log("監(jiān)聽位置變化",res2)
res2中有當(dāng)前經(jīng)緯度
})
}
})
manifest.json文件中需配置
"permission":{
"scope.userLocationBackground":{
"desc" : "獲取您的后臺(tái)位置"
},
"scope.userLocation" : {
"desc" : "獲取用戶位置"
},
},
"requiredPrivateInfos" : [ "getLocation", "onLocationChange", "startLocationUpdate","startLocationUpdateBackground" ],
"requiredBackgroundModes" : ["location"],
requiredBackgroundModes需設(shè)置:location
判斷用戶有沒有開啟授權(quán):
文章來源:http://www.zghlxwxcb.cn/news/detail-753205.html
使用uni.getSetting來獲取用戶的授權(quán)
沒有就直接使用uni.authorize文章來源地址http://www.zghlxwxcb.cn/news/detail-753205.html
uni.authorize({
scope: 'scope.userLocationBackground',
success() {
//成功回調(diào)
}
})
到了這里,關(guān)于uni編寫微信小程序使用wx.startLocationUpdateBackground監(jiān)聽位置的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!