2012-05-22 18 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>