2

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 :^)

2
  • 2
    You're going to have to make an ajax call to a script on your server to add the data to your database. You should really try something before asking here though. Come back when you have an issue with your code ;) Commented Oct 22, 2014 at 4:34
  • You don't need Google Maps API to get users geolocation. Check navigator.geolocation. Commented Oct 22, 2014 at 7:32

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.