2014-10-12 164 views
0

我使用html5地理位置獲取我的Web應用程序的緯度和經度。 我的問題是,如果可以做到這一點,我需要將lat,long轉換爲實際地址?然後使用php將地址保存到mysql數據庫。 到目前爲止的代碼:html5地理位置和地址

<p id="demo">Click the button to get your coordinates:</p> 
<button onclick="getLocation()">Try It</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> 

回答