我正在使用本教程
构建一个带有phonegap和jquery的练习应用程序
一切正常,但gps位置存在问题,不时跳转,结果我遇到了跟踪问题,因为看起来我已经通过了很长的路(正如您在所附图片中看到的那样。
我试图解决这个问题的方法是检查 lat 和 lng 的平均值,并确保下一个样本不会高于 lat 或 lng 的平均值(取决于我正在检查的内容)。在知道平均值是多少之后,我正在检查之前样本中最后一个样本的变化率,如果它更大,我想将 position.coords.lngitude / position.coords.latitude 设置为我拥有的最大速率计算。
但它似乎对我不起作用......
这是代码:
$("#stepslink_start").live('click', function() {
$("#stepslink_stop").hide();
// Start tracking the User
watch_id = navigator.geolocation.watchPosition(
// Success
function(position) {
// Gets the current lat and lng
lat = position.coords.latitude;
lng = position.coords.longitude;
// Push that lat each one in to his array
last_2_points_lat.push(lat);
last_2_points_lng.push(lng);
// Checks if the array length is biger then 2 items and if so starts the process of compering the lat and lng to the average
if (last_2_points_lat.length > 2 | last_2_points_lng.length > 2) {
// Delete the items of the array that has created before the last 2 items
last_2_points_lat.splice(0, 1);
last_2_points_lng.splice(0, 1);
// resets the variables
sum_lat = 0;
sum_lng = 0;
avg_lat = 0;
avg_lng = 0;
avg_change_lat = 0;
avg_change_lng = 0;
/*
* Lat Function
*/
// Sum the values of the array
for (i = 0; i < last_2_points_lat.length; i++) {
if (i < 3) {
sum_lat += last_2_points_lat[i];
}
}
// Checks if the array has 2 items
if (last_2_points_lat.length >= 2) {
avg_lat = sum_lat / last_2_points_lat.length;
// Checks if the lat is larger then average lat
if (Math.abs(last_2_points_lat[last_2_points_lat.length - 1]) > Math.abs(avg_lat)) {
// Calculate the average change of the lat
avg_change_lat = (avg_lat - last_2_points_lat[last_2_points_lat.length - 1]) / last_2_points_lat[last_2_points_lat.length - 1];
}
}
}
if (Math.abs(lat - last_2_points_lat[last_2_points_lat.length - 1]) / Math.abs(lat) <= Math.abs(avg_change_lat)) {
current_change_rate = (lat - last_2_points_lat[last_2_points_lat.length - 1]) / (lat);
console.log(current_change_rate);
if (current_change_rate > 0) {
position.coords.latitude = lat * (1 + ((lat - (last_2_points_lat[last_2_points_lat.length - 1])) / ((last_2_points_lat[last_2_points_lat.length - 1]))));
} else {
position.coords.latitude = lat * ((lat - (last_2_points_lat[last_2_points_lat.length - 1])) / ((last_2_points_lat[last_2_points_lat.length - 1])));
}
}
/*
* Lng Function
*/
// Sum the values of the array
for (i = 0; i < last_2_points_lng.length; i++) {
if (i < 3) {
sum_lng += last_2_points_lng[i];
}
}
// Checks if the array has 2 items
if (last_2_points_lng.length >= 2) {
avg_lng = sum_lng / last_2_points_lng.length;
// Checks if the lng is larger then average lng
if (Math.abs(last_2_points_lng[last_2_points_lng.length - 1]) > Math.abs(avg_lng)) {
// Calculnge the average change of the lng
avg_change_lng = (avg_lng - last_2_points_lng[last_2_points_lng.length - 1]) / last_2_points_lng[last_2_points_lng.length - 1];
}
}
if (Math.abs(lng - last_2_points_lng[last_2_points_lng.length - 1]) / Math.abs(lng) <= Math.abs(avg_change_lng)) {
current_change_rate = (lng - last_2_points_lng[last_2_points_lng.length - 1]) / (lng);
console.log(current_change_rate);
if (current_change_rate > 0) {
position.coords.lngitude = lng * (1 + ((lng - (last_2_points_lng[last_2_points_lng.length - 1])) / ((last_2_points_lng[last_2_points_lng.length - 1]))));
} else {
position.coords.lngitude = lng * ((lng - (last_2_points_lng[last_2_points_lng.length - 1])) / ((last_2_points_lng[last_2_points_lng.length - 1])));
}
}
tracking_data.push(position);
},