GPS拉 - 在更新到当前位置之前多次拉出旧数据

时间:2012-05-22 05:06:03

标签: javascript geolocation gps w3c-geolocation

我回顾了一些类似的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>

2 个答案:

答案 0 :(得分:0)

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

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

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

enableHighAccuracy也适用于getCurrentPosition个选项,并且会以相同的方式运作。

答案 1 :(得分:0)

这也是我的代码。仍然拉取旧数据,通常需要3+次拉取才能获得正确的数据。使用Android三星Galaxy S2 - 尝试过仅使用GPS模式和GPS + Cell Triangulation。并且通常还需要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>