2013-10-27 53 views
1

我建立与PhoneGap的和jQuery的运动应用程序与this tutorial
所有工作得很好,但与从时间跳转到时间的GPS位置的问题,作为一个结果,我有跟踪问题因为它看起来像我已经走过了很长的路(正如你可以在附加的图片中看到的)PhoneGap的地理位置GPS门槛

我试图解决这个问题的方法是检查lat和lng的平均值,并确保下一个样本是没有高于平均值的lat或lng(取决于我正在检查的内容) 知道什么是平均值后,我正在检查样本中最后一个样本的变化率,如果它更大,我想设置position.coords.l ngitude/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); 

        }, 

回答

0

我发现了同样的当现实世界的测试我的应用程序 - 即Android和iOS将间歇性在使用GPS时提供极端不准确的位置。我通过使用位置精度(作为W3C Position规范的一部分传递)来解决此问题,以滤除不够准确的位置。尝试这样的:

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... 
); 
+0

非常感谢您! – Dori