2012-07-10 77 views
0

我已經在這裏多次閱讀了同樣的問題,並修復了最重複的解決方案,並添加了像w3c規範中提到的錯誤處理程序,但它沒有給我例外或錯誤,也沒有結果。我不知道該怎麼辦?W3C的地理定位API沒有錯誤沒有期待,也沒有結果

這是我寫的完整代碼!

<!DOCTYPE HTML> 
<html> 
<head> 
    <title> HTML5 Egypt User Group - Demos</title> 
<script src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 
<script type="text/javascript"> 

var lastLat; 
    var lastLong; 
function updateStatus(message) { 
     document.getElementById("status").innerHTML = message; 
    } 

function loadDemo() { 
    if(navigator.geolocation) { 
     updateStatus("W3C Geolocation is supported in your browser."); 
     navigator.geolocation.watchPosition(updateLocation, 
              handleLocationError, 
              {maximumAge:20000}); 
    } 
} 

window.addEventListener("load", loadDemo, true); 



function handleLocationError(error) { 
    switch (error.code) { 
case 0: 
updateStatus("There was an error while retrieving your location: " + 
       error.message); 
break; 

case 1: 
updateStatus("The user prevented this page from retrieving a location."); 
break; 

case 2: 
updateStatus("The browser was unable to determine your location: " + 
       error.message); 
break; 

case 3: 
updateStatus("The browser timed out before retrieving the location."); 
break; 
    } 
} 
function updateLocation(position) { 
    var latitude = position.coords.latitude; 
    var longitude = position.coords.longitude; 
    var accuracy = position.coords.accuracy; 
    var timestamp = position.timestamp; 

    document.getElementById("latitude").innerHTML = latitude; 
    document.getElementById("longitude").innerHTML = longitude; 
    document.getElementById("accuracy").innerHTML = accuracy; 
    document.getElementById("timestamp").innerHTML = timestamp; 
     // sanity test... don't calculate distance if accuracy 
    // value too large 
    if (accuracy >= 500) { 
     updateStatus("Need more accurate values to calculate distance."); 
     return; 
} 
} 

    </script> 
</head> 
    <body> 
<button onclick="loadDemo()"> loadDemo()</button> 
<input type="text" value="latitude here" id="latitude" /> 
<input type="text" value="latitude here" id="latitude" /> 
<input type="text" value="accuracy here" id="accuracy" /> 
<input type="text" value="timestamp here" id="timestamp" /> 
<input type="text" value="status here" id="status" /> 

<span> By Mustafa Adel<span> 
</body> 
</html> 

回答

0

您正在使用輸入控件。嘗試設置值而不是innerHTML。

function updateLocation(position) { 
    var latitude = position.coords.latitude; 
    var longitude = position.coords.longitude; 
    var accuracy = position.coords.accuracy; 
    var timestamp = position.timestamp; 

    document.getElementById("latitude").value = latitude; 
    document.getElementById("longitude").value = longitude; 
    document.getElementById("accuracy").value = accuracy; 
    document.getElementById("timestamp").value = timestamp; 
     // sanity test... don't calculate distance if accuracy 
    // value too large 
    if (accuracy >= 500) { 
     updateStatus("Need more accurate values to calculate distance."); 
     return; 
    } 
} 
+0

非常感謝@Maurice,我重寫了它,對我也沒有意義。瀏覽器不處理'position.coords.latitude'的問題我重新測試了最大年齡和時間,並且仍然存在!我不知道該怎麼辦! – 2012-07-10 09:17:16

+0

首先check.your瀏覽器如果請求返回一個值,用這個演示測試它,html5demos/geo – 2012-08-31 16:22:29

+0

你可以使用watchposition()來代替updateposition,它使請求座標的循環,我通常使用它,祝你好運:-) – 2012-08-31 16:26:24

相關問題