小程序API wx.startLocationUpdateBackground 的使用

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

    requiredPrivateInfos: [
      'getLocation',
      'onLocationChange',
      'startLocationUpdate',
      'startLocationUpdateBackground'
    ],
    requiredBackgroundModes: [
      'audio',
      'location'
    ],

代码:
1、使用wx.startLocationUpdateBackground需要授权
2、搭配wx.onLocationChange api使用

    /**
     * 使用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. 【uniapp程序-wesocket使用

    2024-04-29 12:48:05       38 阅读
  2. 程序API wx.startLocationUpdateBackground 使用

    2024-04-29 12:48:05       11 阅读
  3. 微信程序vant使用

    2024-04-29 12:48:05       8 阅读
  4. 程序使用瀑布流组件记录

    2024-04-29 12:48:05       40 阅读
  5. C++ easyX程序(介绍几个函数使用)

    2024-04-29 12:48:05       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-29 12:48:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-29 12:48:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-29 12:48:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-29 12:48:05       18 阅读

热门阅读

  1. QT5之lambda

    2024-04-29 12:48:05       12 阅读
  2. C++ day5

    C++ day5

    2024-04-29 12:48:05      11 阅读
  3. C++中的时间相关处理

    2024-04-29 12:48:05       14 阅读
  4. python基础知识

    2024-04-29 12:48:05       12 阅读
  5. Unity坐标相关——坐标系,单位

    2024-04-29 12:48:05       13 阅读
  6. Node.js 的 fs 模块分析及其应用

    2024-04-29 12:48:05       16 阅读
  7. 使用动态ip上网稳定吗?

    2024-04-29 12:48:05       13 阅读
  8. 安装k8s

    2024-04-29 12:48:05       12 阅读
  9. web server apache tomcat11-20-connectors 连接器

    2024-04-29 12:48:05       14 阅读
  10. Odoo用浏览器与企业版客户端的区别

    2024-04-29 12:48:05       12 阅读
  11. ssh连接自动断开的几种可能

    2024-04-29 12:48:05       17 阅读
  12. linux 提权总结_linux提权

    2024-04-29 12:48:05       14 阅读
  13. 阿赵Json工具AzhaoJson的Lua版本

    2024-04-29 12:48:05       11 阅读
  14. 【Spark】读取本地文件

    2024-04-29 12:48:05       9 阅读
  15. rust语言tokio库spawn, blocking_spawn等的使用

    2024-04-29 12:48:05       20 阅读