2012-05-22 32 views
0

我回顾了几个类似的GPS问题。但似乎没有太多的运气。当我通过这种方法拉动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> 
+0

所以你的问题“为什么'watchPosition'不会不断更新? –

+0

是的,我想,还是应该使用不同的东西?需要几次拉才能获得正确的位置 - 有时大约5个或更多。 –

+0

我想这:改变watchPosition getCurrentPosition - 根据http://dev.w3.org/geo/api/spec-source.html - 这是一次GPS拉 - 我会测试更多 –

回答

0

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

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

这可能是因为您的GPS设备中未记录位置的微小变化,除非enableHighAccuracytrue。当它注意到位置变化时,再次调用onGeoSuccess。您可以通过重新创建手表来强制再次找到位置(但由于设置为enableHighAccuracy,GPS的位置可能未发生变化)。

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

+0

尝试现在 - 添加了最大年龄 - 并删除了手表位置 - 只是试着用getCurrentPosition - thx了解到目前为止的情况 –

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>