多端应用不支持 wx.getSetting 接口,应该如何调用 wx.startLocationUpdateBackground 呢?
wx.getSetting({
success(res) {
// 如果没有拿到scope.userLocationBackground授权,提示
if (!res.authSetting['scope.userLocationBackground']) {
wx.authorize({
scope: 'scope.userLocationBackground',
success() {
// 用户已经同意,调用wx.startLocationUpdateBackground
wx.startLocationUpdateBackground({
success(res) {
// 成功开启后台定位, 调用实时位置变更
console.log('后台定位开启成功')
that.openLocaionListener()
},
fail(res) {
wx.showToast({
title: '开启后台定位失败',
})
}
})
}
})
} else {
// 已经拿到scope.userLocationBackground授权调用
wx.startLocationUpdateBackground({
success(res) {
// 成功开启后台定位,
that.openLocaionListener()
// that.addMarker()
},
fail(res) {
wx.showToast({
title: '开启后台定位失败',
})
}
})
}
},
fail(e){
console.log(e)
}
})
然后更改代码为:
const systemSetting = wx.getSystemSetting()
const appAuthorizeSetting = wx.getAppAuthorizeSetting()
console.log(appAuthorizeSetting.locationAuthorized)
if(appAuthorizeSetting.locationAuthorized=='authorized'){
if(systemSetting.locationEnabled){
wx.startLocationUpdateBackground({
success(res) {
// 成功开启后台定位,
that.openLocaionListener()
// that.addMarker()
},
fail(res) {
console.log(res)
wx.showToast({
title: '开启后台定位失败',
})
}
})
}else{
console.log('请打开手机定位开关')
wx.showToast({
title: '请打开手机定位开关',
})
}
}else{
console.log('未授权定位')
wx.showToast({
title: '未授权定位',
})
}
又提示:errMsg: "startLocationUpdateBackground:fail auth deny"
开启 location updates of background modes
开启project.miniapp.json 的iOS 对应的 隐私权限