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

    }
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: wx.startlocationupdatebackground是微信小程序中的一个API,用于开启后台定位服务。开启后,小程序可以在后台持续获取用户位置信息,以便提供更加精准的服务和体验。需要注意的是,使用API需要用户授权,并且会消耗一定的电量和流量。 ### 回答2: wx.startLocationUpdateBackground 是一个微信小程序的接口,允许小程序在后台获取用户的位置信息。在小程序退出或被切换到后台时,仍可通过该接口获取到用户的位置信息,从而实现后台定位的功能。 wx.startLocationUpdateBackground 接口的使用需要用户授权,当小程序使用该接口时,会弹出一个授权提示框,询问用户是否允许小程序获取其位置信息。需要注意的是,该接口只能在真机上测试,因为在模拟器上会提示出错。 该接口的调用方法如下: wx.startLocationUpdateBackground({ success: function(res) { console.log(res); }, fail: function(res) { console.log(res); } }); 其中,success 和 fail 都是回调函数。当调用接口成功时,会执行 success 回调函数;当调用失败时,则会执行 fail 回调函数。在 success 回调函数中,可以获取返回的信息,其中包括用户当前的经纬度、速度、高度等信息。在 fail 回调函数中,可以获取到错误信息,如用户拒绝授权等。 总之,wx.startLocationUpdateBackground 接口提供了小程序后台定位的功能,方便开发者开发出更加精准的定位类应用。开发者可以结合用户的需求,使用该接口实现一些便利的功能,如打车、导航、社交等。但是,出于用户隐私的考虑,开发者需要明确告知用户,获取其位置信息的目的,并尽可能保护用户的隐私。 ### 回答3: wx.startLocationUpdateBackground是一个微信小程序API接口,用于在后台定位用户的位置。该接口需要用户授权才能使用。 在使用wx.startLocationUpdateBackground时,开发者可以选择是否开启高精度定位,这将会使用GPS和移动基站进行定位,提高定位精度。使用该接口时,建议在需要定位的时候开启,在不需要定位的时候关闭。 在使用wx.startLocationUpdateBackground时,需要注意以下几点: 1. 在使用该接口之前,需要调用wx.authorize进行授权,否则会调用失败; 2. 为了避免用户的隐私泄露,建议在文案中明确说明使用该接口的目的; 3. 目前该接口仅在iOS和Android系统上支持,其他系统暂不支持。 对于使用wx.startLocationUpdateBackground需要注意的地方,建议开发者仔细阅读微信小程序官方文档,了解详细的使用说明。同时,也需要在使用该接口时严格遵守相关法律法规,保护用户的个人隐私。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值