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

    }
相关推荐
技术闲聊DD10 小时前
小程序原生-利用setData()对不同类型的数据进行增删改
小程序
康康爹10 小时前
uniapp 小程序,登录上传头像昵称页面处理步骤
小程序·uni-app
小雨cc5566ru10 小时前
小程序 uniapp+Android+hbuilderx体育场地预约管理系统的设计与实现
android·小程序·uni-app
DK七七14 小时前
【PHP陪玩系统源码】游戏陪玩系统app,陪玩小程序优势
前端·vue.js·游戏·小程序·php·uniapp
正小安1 天前
MobX-Miniprogram:微信小程序的状态管理利器
微信小程序·小程序
GDAL1 天前
软考鸭微信小程序:助力软考备考的便捷工具
微信小程序·小程序
技术闲聊DD1 天前
小程序原生-列表渲染
小程序
2401_844137951 天前
JAVA智慧社区系统跑腿家政本地生活商城系统小程序源码
微信·微信小程序·小程序·生活·微信公众平台·微信开放平台
程序员入门进阶1 天前
4S店4S店客户管理系统小程序(lw+演示+源码+运行)
小程序
光影少年1 天前
Vue Mini基于 Vue 3 的小程序框架
前端·vue.js·小程序