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

    }
相关推荐
说私域7 小时前
公域流量向私域流量转化策略研究——基于开源AI智能客服、AI智能名片与S2B2C商城小程序的融合应用
人工智能·小程序
半生过往7 小时前
微信小程序文件下载与预览功能实现详解
微信小程序·小程序·notepad++·压缩包下载解压
源码_V_saaskw7 小时前
JAVA图文短视频交友+自营商城系统源码支持小程序+Android+IOS+H5
java·微信小程序·小程序·uni-app·音视频·交友
weixin_lynhgworld10 小时前
淘宝扭蛋机小程序系统开发:重塑电商互动模式
大数据·小程序
ᥬ 小月亮13 小时前
Uniapp编写微信小程序,绘制动态圆环进度条
微信小程序·小程序·uni-app
说私域1 天前
技术赋能与营销创新:开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化路径研究
人工智能·小程序·开源
游戏开发爱好者82 天前
没有 Mac,如何上架 iOS App?多项目复用与流程标准化实战分享
android·ios·小程序·https·uni-app·iphone·webview
weixin_lynhgworld2 天前
代驾小程序系统开发:引领出行行业数字化转型
小程序
Python大数据分析2 天前
uniapp之微信小程序标题对其右上角按钮胶囊
微信小程序·小程序·uni-app
yzx9910132 天前
JS与Go:编程语言双星的碰撞与共生
java·数据结构·游戏·小程序·ffmpeg