2014-10-31 98 views
0

我需要將緯度和經度保存到我的數據庫中。我可以獲取座標值,但是我不能將它們分配給PHP變量,以便我可以將它們插入到數據庫中。我觀看過很多YouTube視頻並閱讀了很多博客,但他們的尼姑工作過。我不知道哪種方式更好地將座標插入數據庫,請指導我,謝謝。如何將HTML5,經緯度保存到數據庫中?

以下是我的代碼:

<body> 
    <p>HTML5 GEOLOCATION</p> 

    <a href="#" id="get_location">Get Location</a> 
    <div id="map"> 
    <iframe id="google_map" width="425" height="350" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/view?zoom=9&center=24.8615,67.0099&key=AIzaSyAgiV9XkktBAHcfc1XIkLcUngBuSnxmk1o"></iframe> 
    </div> 

    <script> 

     var c = function(pos){ 
      var lat = pos.coords.latitude, 
       long = pos.coords.longitude, 
       coords = lat +', '+long; 

      document.getElementById('google_map').setAttribute('src','https://maps.google.co.uk/?q='+ coords +'&z=15&output=embed'); 
      alert("latitude= "+lat+"\nlongitude= "+long); 

     }//end of function c 
     document.getElementById('get_location').onclick = function(){ 

      navigator.geolocation.getCurrentPosition(c); 
      return false; 
     }//end of document.getElementById() 
    </script> 

</body> 
+0

改爲使用Ajax。 – Amy 2014-10-31 06:45:41

+0

好的。我如何使用Ajax?有任何教程鏈接? – 2014-10-31 09:03:53

+0

我只是發佈答案提到這個 – Amy 2014-10-31 10:05:45

回答

-1

只是要使用jQuery的Ajax調用服務器端語言PHP,即和LAT &郎參數傳遞給你的PHP頁面。

$.ajax({ 
    type: 'POST', 
    url: 'your_server_url.php', 
    async:true, 
    data: { 
     'lat': lat, 
     'lang': lang 
    }, 
    success: function(response){ 
     // alert('wow' + msg); 
    } 
}); 

在你的php頁面讀取參數像這樣。

$lat = $_POST['lat']; 
$lang = $_POST['lang']; 
//your database logic 
+0

請提供評論,如果downvoted。 – Amy 2014-10-31 10:40:48

+0

謝謝,但沒有工作兄弟。這個網站有限制,我不能問在這裏的每一個類型的問題,我們可以通過電子郵件聊天? [email protected] – 2014-10-31 11:55:58

+0

給我你的問題在[email protected] – Amy 2014-10-31 12:14:32