2014-03-12 63 views
0

我正在開發Phone Gap應用程序。在那個應用程序中,我想顯示當前用戶的位置。爲此,我使用了HTML5地理定位。當我點擊獲取位置按鈕時,它不顯示位置。它也不會顯示錯誤。按下獲取位置按鈕後,它只顯示處於選定狀態的按鈕。在HTML5中使用地理位置時不顯示位置

<!DOCTYPE html> 
<html> 
<body> 
    <button onclick="getLocation()">Get Location</button> 
    <script> 
     var x = document.getElementById("demo"); 
     function getLocation() { 
      if (navigator.geolocation) { 
       navigator.geolocation.getCurrentPosition(showPosition, showError); 
      } 
      else { x.innerHTML = "Geolocation is not supported by this browser."; } 
     } 
     function showPosition(position) { 
      x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; 
     } 
     function showError(error) { 
      switch (error.code) { 
       case error.PERMISSION_DENIED: 
        x.innerHTML = "User denied the request for Geolocation." 
        break; 
       case error.POSITION_UNAVAILABLE: 
        x.innerHTML = "Location information is unavailable." 
        break; 
       case error.TIMEOUT: 
        x.innerHTML = "The request to get user location timed out." 
        break; 
       case error.UNKNOWN_ERROR: 
        x.innerHTML = "An unknown error occurred." 
        break; 
      } 
     } 
    </script> 
</body> 
</html> 

回答

1

您正在引用一個不存在的元素。 一個div添加到HTML的身體使用id = 「演示」,就像這樣:

<div id="demo"></div> 

見這裏上的jsfiddle:http://jsfiddle.net/wU6AE/