小工具知识网

当前位置: 首页 > 行业知识 > 小程序API wx.startLocationUpdateBackground 的使用

行业知识

小程序API wx.startLocationUpdateBackground 的使用

2026-11-08 21:43:26 来源:互联网转载

若index.php/tags-602.html" >使用该接口,需要在 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)      }    }

上一篇:手机文件怎么传到服务器?

下一篇:怎么看网站有没有收录?