2017-08-29 79 views
0

我必須做點什麼,我已經有一部分工作。 我想在MySQL數據庫中插入經度(長)和緯度(緯度)。 發現這個代碼誰給我LON和LAT獲取緯度和經度html js

<!DOCTYPE html> 
<html> 
<body> 

<p>Click the button to get your coordinates.</p> 

<button onclick="getLocation()">Try It</button> 

<p id="demo"></p> 

<script> 
    var x = document.getElementById("demo"); 

    function getLocation() { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(showPosition); 
     } else { 
      x.innerHTML = "Geolocation is not supported by this browser."; 
     } 
    } 

    function showPosition(position) { 
     x.innerHTML = "Latitude: " + position.coords.latitude + 
      "<br>Longitude: " + position.coords.longitude; 
    } 
</script> 

</body> 

此代碼打印長,緯度,我想是讓長,緯度在PHP和後面運行的SQL查詢。

+0

使用'ajax'讓那些COORDS到後端 –

+0

我不知道如何,如果你有一個例子,我可以嘗試做它用ajax.but。 – shmyself

+0

我們不這樣做,但Google會爲您提供無數關於如何將數據發送到服務器的示例。 –

回答

1

使用ajax可以將經緯度保存到數據庫中。爲此更新您的腳本,如下所示。

function showPosition(position) { 
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 

    saveData(position.coords.latitude,position.coords.longitude); 

} 

function saveData(latitude,longitude){ 
     $.ajax({ 
     url: 'save_data.php', // Script to save latitude and longitude 
     type: 'GET', 
     data: { latitude : latitude, longitude : longitude }, 
     dataType: "json",  
     success: function(result) { 
      //Return result 
     } 
    }); 
} 
+0

是我的save_data.php好嗎? <?php /*簡單示例*/ $ lat = $ _ GET ['latitude']; $ long = $ _ GET ['longitude']; echo「$ lat」; ?> – shmyself

+0

是的。請確保您獲取GET變量值。 –

+0

使用GET保存數據?那麼用POST來代替呢? – kovogel