收藏
回答

多端应用中调用startLocationUpdateBackground报错?

多端应用不支持 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"

在多端应用中如何正确使用 wx.startLocationUpdateBackground 呢?


最后一次编辑于  06-19
回答关注问题邀请回答
收藏

1 个回答

  • 张晨  🔆
    张晨 🔆
    06-21

    开启 location updates of background modes

    开启project.miniapp.json 的iOS 对应的 隐私权限

    06-21
    有用
    回复 1
    • 科技狂人
      科技狂人
      06-21
      这怎么办?别的应用就有,多端应用就没有,应该怎么调起用户的这个授权呢?目前App仅针对安卓用户
      06-21
      回复
登录 后发表内容