2014-10-22 69 views
1

如何将Google地图数据保存到MySQL DB中?使用PHP。我目前正在编写一个代码,当用户进入我的网站时,系统会自动获取他们的经纬度。我使用谷歌地图API,但我不知道如何在我的数据库中保存经度和纬度。请帮助在php中将这些值传输到服务器端,并将它们添加到数据库Thanx中:^)如何保存用户在数据库中的位置

+2

您将不得不对服务器上的脚本进行ajax调用,以将数据添加到数据库。但在你问这里之前你应该尝试一些东西。当您的代码出现问题时再回来;) – 2014-10-22 04:34:19

+0

您不需要Google Maps API来获取用户地理位置。检查navigator.geolocation。 – MrUpsidown 2014-10-22 07:32:58

回答

1

下面是一个示例,使用navigator.geolocation和jQuery将信息传递给您的后端(AJAX)。

if (navigator.geolocation) { 

    navigator.geolocation.getCurrentPosition(function(position) { 

     $.ajax({ 
      url: 'your_backend_page.php', 
      data: { 
       'lat': position.coords.latitude, 
       'lng': position.coords.longitude 
      }, 
      type: 'POST', 
      success: function (result) { 
       // If your backend page sends something back 
       alert(result); 
      } 
     }); 

     // Use the 2 lines below to center your map on user location (you need a map instance at this point) 
     userLoc = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     map.panTo(userLoc); 
    }); 
} 

在你的PHP页面,您将收到的数据$_POST['lat']$_POST['lng'],你可以用它们在你的MySQL数据库中插入数据。

相关问题