最近在做一个需要持续定位功能的APP,目前只测试了安卓手机(魅族、小米、华为)
用定时器10s调用一起uni.getlocation循环,持续一段时间就卡死了,不同手机的性能不同,卡死时长不同,有的低端机1个小时左右就卡死了。
我先用屏保常亮使APP一直在前台运行,测试了定时器不调uni.getlocation,10几个小时后程序还是可以点击操作的,调取uni.getlocation 后一定卡死,APP中的按钮点击没有反应。
贴代码:
async startgetLocation() {
var that = this;
var delay = that.$common.delayTime;//定时器时间间隔10s
var delay_seek = that.$common.delayPeriod;//如果网络异常,增加定时器时间间隔30s
var isNetCon = uni.getStorageSync("networkConnected");//判断网络是否连接
var timerId_get = that.$common.timerId_get;//定时器句柄
if (timerId_get != "")
clearTimeout(timerId_get);//清除定时器
if (isNetCon) {
try {
await that.getLocation();//调取定位接口
// await that.updateLocation();
} catch (e) {
delay = delay + delay_seek;
}
} else {
delay = delay + delay_seek;
}
that.$common.timerId_get = setTimeout(function() {
that.startgetLocation();//调用自己
}, delay);
}
getLocation: () => {
return new Promise((resolve, reject) => {
uni.getLocation({
type: 'gcj02',
// geocode: true,
success: function(res) {
//正常接收定位数据
getApp().globalData.longitude = res.longitude;
getApp().globalData.latitude = res.latitude;
getApp().globalData.speed = res.speed;
resolve('suc');
},
fail: function(err) {
//定位失败
reject('err');
}
});
})
}
0 个回复