eun0404
eun0404

Reputation: 23

How to save user's location in database

How to save Google maps data into MySQL DB ? using php . I am currently working on a code where when users enter my website, system will get their latitude and longitude automatically. I use Google maps API, but I don't know how to save latitude and longitude in my database. Please help in transferring these values for server side in php and adding them in database Thanx very much :^)

Upvotes: 2

Views: 8982

Answers (1)

MrUpsidown
MrUpsidown

Reputation: 22490

Here is an example, using navigator.geolocation and jQuery to pass the information to your backend (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);
    });
}

In your PHP page, you will receive the data as $_POST['lat'] and $_POST['lng'] and you can use them to insert the data in your MySQL database.

Upvotes: 4

Related Questions