Jquery Phonegap地理定位GPS阈值
我正在使用phonegap和jquery构建一个锻炼应用程序Jquery Phonegap地理定位GPS阈值,jquery,cordova,gps,Jquery,Cordova,Gps,我正在使用phonegap和jquery构建一个锻炼应用程序 一切都很好,但gps位置有问题,有时会跳转,因此我的轨迹有问题,因为我看起来已经走过了很长的一段路(如所附图片所示) 我试图解决这一问题的方法是检查lat和lng的平均值,并确保下一个样本不会高于lat或lng的平均值(取决于我检查的内容)。 在知道平均值后,我将检查前一个样本中最后一个样本的变化率,如果变化率更大,我希望将position.coords.lngitude/position.coords.latitude设置为我计算的
一切都很好,但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);
},
$(“#stepslink_start”).live('单击',函数()){
$(“步骤链接停止”).hide();
//开始跟踪用户
watch_id=navigator.geolocation.watchPosition(
//成功
职能(职位){
//获取当前lat和lng
纬度=位置坐标纬度;
lng=位置坐标经度;
//把每个人都推进他的阵型
最后2点横向推力(横向推力);
最后2个点推动液化天然气(lng);
//检查阵列长度是否大于2项,如果大于2项,则开始将lat和lng与平均值进行比较
如果(最后两个点的横向长度>2,最后两个点的横向长度>2){
//删除在最后2项之前创建的数组项
最后的横向拼接点(0,1);
最后的拼接点(0,1);
//重置变量
总和=0;
总和=0;
平均纬度=0;
平均液化天然气=0;
平均变化率=0;
平均液化天然气变化=0;
/*
*Lat函数
*/
//对数组的值求和
对于(i=0;i=2){
平均纬度=总纬度/最后两点纬度长度;
//检查lat是否大于平均lat
if(Math.abs(last_2_points_lat[last_2_points_lat.length-1])>Math.abs(avg_lat)){
//计算lat的平均变化
平均变化时间=(平均时间-最后2点时间[最后2点时间长度-1])/最后2点时间[最后2点时间长度-1];
}
}
}
if(Math.abs(lat-last_2_points_lat[last_2_points_lat.length-1])/Math.abs(lat)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]);
}否则{
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]);
}
}
/*
*液化天然气功能
*/
//对数组的值求和
对于(i=0;i=2){
平均液化天然气=总液化天然气/最后2点液化天然气长度;
//检查lng是否大于平均lng
if(Math.abs(last_2_points_lng[last_2_points_lng.length-1])>Math.abs(avg_lng)){
//计算液化天然气的平均变化
平均液化天然气变化=(平均液化天然气-最后2点液化天然气[最后2点液化天然气长度-1])/最后2点液化天然气[最后2点液化天然气长度-1];
}
}
if(数学abs(lng-最后2个点\U lng[最后2个点\U lng.length-1])/Math.abs(lng)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]);
}否则{
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]);
}
}
跟踪数据推送(位置);
},
我在实际测试我的应用程序时发现了同样的情况——Android和iOS在使用GPS时都会间歇性地提供非常不准确的位置。我通过使用作为W3C位置规范的一部分传递的位置精度来过滤出不够准确的位置,从而解决了这一问题。请尝试以下方法:
var MinimumAccuracy = 20; // metres
// Start tracking the User
watch_id = navigator.geolocation.watchPosition(
// Success
function(position) {
// Reject udpate if accuracy is not sufficient
if(position.coords.accuracy > MinimumAccuracy){
console.warn("Position update rejected because accuracy of"+position.coords.accuracy+"m is less than required "+MinimumAccuracy+"m");
return;
}
// Else, get the current lat and lng
lat = position.coords.latitude;
lng = position.coords.longitude;
}, etc...
);