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

    }
相关推荐
2501_915909061 天前
iOS 发布 App 全流程指南,从签名打包到开心上架(Appuploader)跨平台免 Mac 上传实战
android·macos·ios·小程序·uni-app·cocoa·iphone
zy happy2 天前
RuoyiApp 在vuex,state存储nickname vue2
前端·javascript·小程序·uni-app·vue·ruoyi
云起SAAS2 天前
黄历万年历择吉日抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·黄历万年历择吉日
2501_915918412 天前
HTTP抓包工具推荐,Fiddler使用教程、代理设置与调试技巧详解(含HTTPS配置与实战案例)
http·ios·小程序·https·fiddler·uni-app·webview
一 乐2 天前
个人理财系统|基于java+小程序+APP的个人理财系统设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·后端·小程序
一只小白菜~2 天前
记录一下微信小程序里使用SSE
微信小程序·小程序·sse·小程序sse·小程序eventsource
小小王app小程序开发2 天前
家政小程序拓展分析:从工具型产品到全链路服务生态的技术落地与商业破局
小程序
上海云盾安全满满2 天前
APP小程序被攻击了该如何应对
网络·小程序
2501_915921432 天前
iOS 虚拟位置设置实战,多工具协同打造精准调试与场景模拟环境
android·ios·小程序·https·uni-app·iphone·webview
2501_916008892 天前
App 上架需要什么?从开发者账号到开心上架(Appuploader)免 Mac 上传的完整流程指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone