【问题标题】:GPS Pull - Pulls Older Data Several Times Before Updating To Current LocationGPS 拉取 - 在更新到当前位置之前多次拉取旧数据
【发布时间】:2012-05-22 05:06:02
【问题描述】:

我查看了一些类似的 GPS 问题。但似乎运气不太好。当我通过这种方法提取 GPS 时 - 它似乎提取了旧数据,即最后一次提取 GPS 的来源。有什么建议吗?

这是一个简单的 javascript GPS 拉取功能 - 在请求时填充表单。

 <script type="text/javascript"> 

function getLocationConstant()
{
    if(navigator.geolocation)
    {
        watchID = navigator.geolocation.watchPosition(onGeoSuccess,onGeoError); 
    } else {
        alert("Your browser or device doesn't support Geolocation");
    }
}

// Get a single location update
function getLocationConstant()
{
    if(navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError);
    } else {
        alert("Your browser or device doesn't support Geolocation");
    }
}

// If we have a successful location update
function onGeoSuccess(event)
{
    document.getElementById("Latitude").value =  event.coords.latitude; 
    document.getElementById("Longitude").value = event.coords.longitude;

}

// If something has gone wrong with the geolocation request
function onGeoError(event)
{
    alert("Error code " + event.code + ". " + event.message);
}
 </script>

 <div class=general align=center>
 <cfform action="gps_pull.cfm" method="post">
 <div align=center>
 <b>GPS Services Needs to be enabled on your device.</b>
 <br>
 <br>With a GPS Capable Device - Choose "Get Location". Once the GPS data is pulled in, choose "Add GPS".
 <br><span class=code8>You may need to allow GPS Device time to pull current location</span>
 </div>
 <br>
 <table align=center>
 <tr>
 <td>Latitude:</td>
 <td><cfinput type="text" id="Latitude" name="gpslat" value="" required="yes" message="Choose Get Location First"></td>
 </tr>
 <tr>
 <td>Longitude:</td>
 <td><cfinput type="text" id="Longitude" name="gpslong" value=""></td>
 </tr>
 <tr>
 <td colspan=2 align=center><br><input type="button" value="Get Location" onclick="getLocationConstant()"/> &nbsp; <input type="submit" value="Add GPS"></td>
 </tr>
 </table>
 <input type="hidden" name="src" value="gpsup">
 </cfform>

【问题讨论】:

  • 您的问题是“为什么watchPosition 不不断更新?”
  • 是的,我猜,还是我应该使用不同的东西?需要多次拉动才能获得正确的位置 - 有时大约 5 次或更多。
  • 我正在尝试这个:将 watchPosition 更改为 getCurrentPosition - 根据dev.w3.org/geo/api/spec-source.html - 这是一次 GPS 拉动 - 我会再测试一些

标签: javascript geolocation gps w3c-geolocation


【解决方案1】:

watchPosition 可以取选项:watchPosition(onGeoSuccess,onGeoError,options);,默认值为

options = { "enableHighAccuracy": false,
            "timeout"           : Infinity,
            "maximumAge"        : 0 }

您的 GPS 设备可能不会记录位置的微小变化,除非 enableHighAccuracytrue。当它注意到位置变化时,再次调用onGeoSuccess。并且您可以通过重新创建手表来强制重新找到位置(但由于enableHighAccuracy 设置,GPS 保持的位置可能没有改变)。

enableHighAccuracy 也适用于getCurrentPosition 选项,并且将以相同的方式工作。

【讨论】:

  • 现在正在尝试 - 添加了最大年龄 - 并删除了观看位置的东西 - 只是尝试使用 getCurrentPosition - 到目前为止,感谢您的洞察力
【解决方案2】:

这也是我的代码。仍然拉旧数据,通常需要 3+ 拉才能获得正确的数据。使用 Android Samsung Galaxy S2 - 已尝试仅使用 GPS 模式和 GPS + 小区三角测量。而且通常仍然需要 3 次以上的拉取才能获得正确的数据。

各位有什么想法吗?

 <script type="text/javascript"> 

// Get a single location update
function getLocationConstant()
{
    if(navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError,{enableHighAccuracy:true,maximumAge:0});
    } else {
        alert("Your browser or device doesn't support Geolocation");
    }
}

// If we have a successful location update
function onGeoSuccess(event)
{
    document.getElementById("Latitude").value =  event.coords.latitude; 
    document.getElementById("Longitude").value = event.coords.longitude;

}

// If something has gone wrong with the geolocation request
function onGeoError(event)
{
    alert("Error code " + event.code + ". " + event.message);
}
 </script>

【讨论】:

    猜你喜欢
    • 2014-01-16
    • 2011-02-11
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多