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
-
2You'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 ;)Patrick James McDougle– Patrick James McDougle2014-10-22 04:34:19 +00:00Commented Oct 22, 2014 at 4:34
-
You don't need Google Maps API to get users geolocation. Check navigator.geolocation.MrUpsidown– MrUpsidown2014-10-22 07:32:58 +00:00Commented Oct 22, 2014 at 7:32
Add a comment
|
1 Answer
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.