小程序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)
      }

    }
相关推荐
全职计算机毕业设计1 天前
基于微信小程序的城市特色旅游推荐应用的设计与实现
微信小程序·小程序
说私域1 天前
线下消费经济“举步维艰”,开源AI智能名片链动2+1+S2B2C小程序线上“狂飙突进”!
人工智能·小程序·开源·零售
JAVA叶知秋2 天前
uniapp自定义底部导航栏h5有效果小程序无效的解决方案
小程序·uni-app
沙尘暴炒饭2 天前
用uniapp在微信小程序实现画板(电子签名)功能,使用canvas实现功能
微信小程序·小程序·uni-app
PyAIGCMaster2 天前
Taro 编译不平不同平台小程序
小程序
fakaifa3 天前
【开源版】likeshop上门家政系统PHP版全开源+uniapp前端
小程序·uni-app·php·家政小程序源码·家政服务小程序·源码下载·上门家政
说私域3 天前
基于定制开发开源AI智能名片S2B2C商城小程序的公私域流量融合运营策略研究
人工智能·小程序·开源·零售
阿諪諪3 天前
uniapp小程序中实现无缝衔接滚动效果
小程序·uni-app
我是小伍同学4 天前
基于卷积神经网络和Pyqt5的猫狗识别小程序
人工智能·python·神经网络·qt·小程序·cnn
爱分享的程序员4 天前
小程序消息订阅的整个实现流程
小程序