小程序API wx.startLocationUpdateBackground 的使用

若使用该接口,需要在 app.json 中进行声明

json 复制代码
    requiredPrivateInfos: [
      'getLocation',
      'onLocationChange',
      'startLocationUpdate',
      'startLocationUpdateBackground'
    ],
    requiredBackgroundModes: [
      'audio',
      'location'
    ],

代码:

1、使用wx.startLocationUpdateBackground需要授权

2、搭配wx.onLocationChange api使用

javascript 复制代码
    /**
     * 使用wx.startLocationUpdateBackground需要授权
     */
    getAuth() {
      const that = this
      wx.getSetting({
        success(res) {
          console.log(1, res.authSetting['scope.userLocationBackground']);
          // 如果没有拿到scope.userLocationBackground授权,提示
          if (!res.authSetting['scope.userLocationBackground']) {
            console.log('-------------');
            wx.authorize({
              scope: 'scope.userLocationBackground',
              success() {
                console.log(2);
                // 用户已经同意,调用wx.startLocationUpdateBackground
                wx.startLocationUpdateBackground({
                  success(res) {
                    console.log('startLocationUpdateBackgroundsuccess');
                    // 成功开启后台定位, 调用实时位置变更
                    that.openLocaionListener()
                  },
                  fail(res) {
                    console.log('startLocationUpdateBackgroundfail', res);
                    wx.showToast({
                      icon: 'error',
                      title: '开启后台定位失败'
                    })
                  }
                })
              },
              fail(res) {
                wx.showToast({
                  title: '授权失败,点击右上角设置位置为使用时和离开后!',
                  icon: 'none',
                  duration: 5000,
                  mask: true,
                  success: () => {
                    wx.navigateBack()
                  }
                })
              }
            })
          } else {
            console.log(4);
            // 已经拿到scope.userLocationBackground授权调用
            wx.startLocationUpdateBackground({
              success(res) {
                console.log(5, res);
                // 成功开启后台定位,
                that.openLocaionListener()
              },
              fail(res) {
                console.log(6, res);
                wx.showToast({
                  icon: 'error',
                  title: '开启后台定位失败'
                })
              }
            })
          }
        }
      })
    }

    openLocaionListener() {
      const that = this
      console.log('openLocaionListener')
      wx.onLocationChange((_res) => {
        if (that.isUpdateLocation) {
          that.isUpdateLocation = false
          console.log('定位发生改变', _res, new Date())
          const date = new Date()
          const res = {
            'result': _res,
            'time': date
          }
          // const jsonStr = JSON.stringify(res)
          // console.log(jsonStr);
          wx.reportEvent('update_location', {
            "data": res
          })
        }
      })
      let i = 0
      console.log('updateLocationInterval', this.updateLocationInterval);
      
      if (!this.updateLocationInterval) {
        this.updateLocationInterval = setInterval(() => {
          console.log(i++, new Date());
          this.isUpdateLocation = true
        }, 5000)
      }

    }
相关推荐
互联科技报16 小时前
2026小程序店铺装修模板怎么选?小程序店铺装修教程是什么?
小程序
凡科网小帆16 小时前
右以云小程序:零基础企业的高性价比数字化落地实录
小程序
棋宣16 小时前
微信小程序onShareAppMessage 分享-生命周期函数 在vue3中 组合式函数 hooks中不生效
微信小程序·小程序
2601_9579665317 小时前
抽卡机盲盒小程序怎么做?完整功能搭建与开发要点
小程序
wuyoula2 天前
全新多平台电商代付商城源码
开发语言·c++·ui·小程序·php源码
低代码布道师2 天前
微搭低代码MBA 培训管理系统实战 36——小程序端课程预约功能实现
低代码·小程序
万岳科技系统开发2 天前
小程序直播架构调整指南:H5嵌套模式的优化与替代方案
小程序·架构
Greg_Zhong2 天前
学习AI 工程师第 3 天:小程序中调用豆包模型,实现ai助手(打字机效果与流式输出)
小程序·ai工程师·小程序调用豆包实现ai助手
于先生吖2 天前
家政派单小程序定制厂家
大数据·小程序
00后程序员张2 天前
完整指南 iOS App上架到App Store的步骤详解
macos·ios·小程序·uni-app·objective-c·cocoa·iphone