如何保存用户';s在数据库中的位置


How to save user's location in database

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

这里有一个例子,使用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数据库中。