比如我单击按钮 开启后台位置监控调用,再单击关闭后台监控,现在的问题是 开了然后关,再开发现onLocationChange事件触发2次,再关再开就会出现3,4,5次.这是如何避免
async enableLocationUpdateBackground() {
const that = this;
wx.startLocationUpdateBackground({
success:async 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 => {
const loc = await that.reverseGeocoder({
latitude: res.latitude,
longitude: res.longitude
});
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) {
},
});
},
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: '停止上报定位信息'
});
},
})
},
// 关闭startLocationUpdateBackground
uni.stopLocationUpdate({
success: () => {
console.log('后台定位更新已关闭');
},
fail: (err) => {
uni.showToast({ title: '后台定位更新关闭失败:' + err, icon: 'none' });
}
})
// 关闭onLocationChange
uni.offLocationChange();
问题解决了吗,方便说说吗
请问问题解决了么?怎么可以避免重复触发?或者怎么关闭然后再触发?
请提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。