【问题标题】:How to save user's location in database如何在数据库中保存用户的位置
【发布时间】:2014-12-17 11:33:04
【问题描述】:

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

【问题讨论】:

  • 您必须对服务器上的脚本进行 ajax 调用才能将数据添加到数据库中。不过,在问这里之前,您真的应该尝试一下。当你的代码有问题时回来;)
  • 您不需要 Google Maps API 来获取用户的地理位置。检查 navigator.geolocation。

标签: php mysql google-maps google-maps-api-3


【解决方案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 数据库中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多