2014-10-31 96 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