收藏
回答

多次调用wx.startLocationUpdateBackground()会出现重重复监听事件?

比如我单击按钮 开启后台位置监控调用,再单击关闭后台监控,现在的问题是 开了然后关,再开发现onLocationChange事件触发2次,再关再开就会出现3,4,5次.这是如何避免
//开启后台获取地理位置坐标
  async enableLocationUpdateBackground() {
    const that = this;


    wx.startLocationUpdateBackground({
       success:async res=> {
        //console.log('开启后台定位', res)
        Notify({
          type'success',
          message: '开启上报定位信息'
        });
        //开始记录
        const userId = wx.getStorageSync('userId');
        const openId = wx.getStorageSync('openid');
        const userInfo = this.data.userInfo;
        const startloc = await that.getLocation();
        const startaddress = await that.reverseGeocoder({
          latitude: startloc.latitude,
          longitude: startloc.longitude
        });
        that.addTrackPath({
          "UserId": userId,
          "TrackingDateTime": moment().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
          "BeginDateTime": moment().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
          "Location"JSON.stringify(startloc),
          "latitude": startloc.latitude,
          "longitude": startloc.longitude,
          "Address": startaddress.result.address,
          "OpenId": openId,
          "NickName": userInfo.nickName,
          "AvatarUrl": userInfo.avatarUrl,
        })
        wx.onLocationChange(
          async res => {
            //console.log('onLocationChange', res)


            const loc = await that.reverseGeocoder({
              latitude: res.latitude,
              longitude: res.longitude
            });
            //console.log(loc)
            that.addTrackPath({
              "UserId": userId,
              "TrackingDateTime": moment().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
              "Location"JSON.stringify(res),
              "latitude": res.latitude,
              "longitude": res.longitude,
              "Address": loc.result.address,
              "OpenId": openId,
              "NickName": userInfo.nickName,
              "AvatarUrl": userInfo.avatarUrl,
            })
          });
      },
      fail(res) {
        that.setData({
          enable: false
        })
        console.log('开启后台定位失败', res)
        Notify({
          type'danger',
          message: '开启定位失败'
        });
        Dialog.alert({
          message: '请转到设置位置信息[使用小程序期间和离开小程序后]',
        }).then(() => {
          wx.navigateTo({
            url: '../manage/index',
          })
        });
         
      },
      complete: function (res{
        // console.log('开启后台定位.complete', res)
        // wx.openSetting({
        //   success(res) {
        //     console.log(res.authSetting)
        //     res.authSetting = {
        //       "scope.userInfo": true,
        //       "scope.userLocation": true
        //     }
        //   }
        // })
      },
    });
  },
  //关闭后台获取地理坐标
  disableLocationUpdateBackground() {
    const that = this;
    wx.stopLocationUpdate({
      complete: async (res) => {
        wx.offLocationChange(res=>{
          
        })
        //结束记录
        const userId = wx.getStorageSync('userId');
        const openId = wx.getStorageSync('openid');
        const userInfo = this.data.userInfo;
        const endloc = await that.getLocation();
        const endaddress = await that.reverseGeocoder({
          latitude: endloc.latitude,
          longitude: endloc.longitude
        });
        that.addTrackPath({
          "UserId": userId,
          "TrackingDateTime": moment().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
          "EndDateTime": moment().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
          "Location"JSON.stringify(endloc),
          "latitude": endloc.latitude,
          "longitude": endloc.longitude,
          "Address": endaddress.result.address,
          "OpenId": openId,
          "NickName": userInfo.nickName,
          "AvatarUrl": userInfo.avatarUrl,
        })


        Notify({
          type'primary',
          message: '停止上报定位信息'
        });
      },
    })
  },

回答关注问题邀请回答
收藏

5 个回答

  • 洪烧狮子头
    洪烧狮子头
    2023-11-14

    // 关闭startLocationUpdateBackground

    uni.stopLocationUpdate({

    success: () => {

    console.log('后台定位更新已关闭');

    },

    fail: (err) => {

    uni.showToast({ title: '后台定位更新关闭失败:' + err, icon: 'none' });

    }

    })

    // 关闭onLocationChange

    uni.offLocationChange();

    2023-11-14
    有用
    回复
  • Tyler李春良
    Tyler李春良
    2021-06-01

    问题解决了吗,方便说说吗

    2021-06-01
    有用
    回复
  • 赵世新
    赵世新
    发表于移动端
    2020-07-30
    ds#ff4fzfrdrrr
    2020-07-30
    有用
    回复
  • 灿灿
    灿灿
    2020-07-23

    请问问题解决了么?怎么可以避免重复触发?或者怎么关闭然后再触发?

    2020-07-23
    有用
    回复 1
    • 灿灿
      灿灿
      2020-07-23
      onLocationChange这个方法只需要调用一次。stopLocationUpdate之后重新调用startLocationUpdateBackground的话,onLocationChange里面的数据就会重新走的
      2020-07-23
      回复
  • Cjiang
    Cjiang
    2020-07-08

    请提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2020-07-08
    有用
    回复
登录 后发表内容
问题标签