ds#ff4fzfrdrrr
多次调用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: '停止上报定位信息' }); }, }) },
2020-07-30