我在为我的旅程跟踪应用程序存储位置数据时遇到问题。我在我的应用程序中同时使用了加速度计和 GPS 位置。所有数据都存储在电话sqlite数据库中。加速度计的采样率为 25 毫秒,GPS 的采样率为 3000 毫秒。因此,对于我放入数据库的每 120 个加速度传感器读数,都会插入一个 GPS 读数。
这在大约 30 秒内完美运行,然后应用程序奇怪地停止添加 GPS 读数,但它继续添加加速度计读数。
下面是我的手表功能,每 3 秒运行一次,从 GPS 获取读数。我使用它的原因是因为我以前在插件的监视功能方面遇到了问题。
设置监视函数代码:
var activeWatch=null;
// sets up the interval at the specified frequency
function setupWatch(freq) {
activeWatch = setInterval(watchLocation, freq);
}
// stop watching
function logout() {
clearInterval(activeWatch);
}
手表功能代码:
function watchLocation() {
var gcp = navigator.geolocation.getCurrentPosition(
// Success
function(position){
newlongitude = position.coords.longitude;
//alert("New "+newlongitude)
newlatitude = position.coords.latitude;
newSpeed = position.coords.speed;
//alert(position.coords.speed);
var timestamp = new Date().toISOString().slice(0, 19).replace('T', ' ');
//alert(timestamp);
insertDB(track_id, newlongitude,newlatitude,newSpeed,timestamp,row);
row+=120;
},
// Error
function(error){
alert(error.message);
}, {
enableHighAccuracy: true
});
}
以下是我用来将GPS读数插入数据库的功能。我使用 sql update 语句将加速度计读数已创建的行与 GPS 读数和当前指南针方向一起更新
。插入数据库函数代码:
function insertDB(trackID, longi,lat,speed, time,arow){
//alert(Longi + " " + Lat);
watch_Comp = navigator.compass.getCurrentHeading(onSuccess, onError,compassOptions);
function onSuccess(heading) {
newDir = heading.magneticHeading;
//alert(newDir);
if(arow===0){
arow=1;
}
//alert("update row "+arow);
db.transaction(function(tx){
tx.executeSql('UPDATE DEMO SET longitude = '+'''+longi+'''+', latitude = '+'''+lat+'''+
', speed = '+'''+speed+'''+ ', time = '+'''+time+'''+', direction = '+'''+newDir+'''+
' WHERE uniqueID = ' + '''+arow+''');
}, errorCB);
};
function onError(error) {
alert('CompassError: ' + error.code);
};
var compassOptions = {
frequency: 3000
};
有人知道是什么原因导致这种情况发生吗?
我想通了。它停止更新 GPS 数据的原因是因为它尝试更新的行尚未创建。当我开始跟踪我的旅程时,我首先启动加速度计手表,它开始插入加速度计读数。然后,我启动 GPS 手表,它更新使用 GPS 读数创建的加速度计行。
问题是加速度计手表必须对数据库进行如此多的插入,它在 3 秒内有点慢,因此最终 GPS 手表会赶上它并尝试更新尚未创建的行。一旦 GPS 手表通过加速度计手表,我就无法存储 GPS 读数。
解决此问题的方法是将GPS手表的采样率减慢至3.5秒,或将其从"更新"加速度器行更改为"插入"。尽管如果将其更改为插入,则数据库中每三秒标记处就会有双行。这就是为什么我首先将其更改为更新的原因。