The goal is to have a map with movable marker which once clicked on 'confirm position' button, saves the latitude and longitude coordinates. I already seem to have got the map with a movable marker which gets long and lat of the pointer, but I do not know how to store these values and pass it to my database using SQL.
Please NOTE - there is supposed to be a google maps link in the header page which includes one's API key. Also - I am using this link for the location picker -
<!DOCTYPE html>
<html>
<head>
<title>Tujjari</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--Font Awesome CDN -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://use.fontawesome.com/265b5bff8c.js"></script>
<script src="https://kit.fontawesome.com/420b921a12.js" crossorigin="anonymous"></script>
<!--Animate.css link1-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.0.0/animate.min.css"/>
<!--Animate.css link2-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css">
<!--Linking style.css-->
<link rel="stylesheet" type="text/css" href="tujjaricss.css?v=<?php echo time(); ?>">
<!--Bootstrap CDN's-->
<script src="https://unpkg.com/scrollreveal"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://unpkg.com/location-picker/dist/location-picker.min.js"></script>
<script
src="https://code.jquery.com/jquery-3.5.1.js"
integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAMq_YJsJ75HOUWUAK3MyZp47vzdR7_Jv8"></script>
<script src="https://unpkg.com/location-picker/dist/location-picker.min.js"></script>
</head>
<body>
<div id="map" ></div>
<br>
<center>
<button id="confirmPosition" class="btn btn-secondary" formaction="congrats.php">Confirm Position</button></center>
<br><br>
</div>
<br><br>
<script>
// Get element references
var confirmBtn = document.getElementById('confirmPosition');
var longlat = document.getElementById('longlat');
var onIdlePositionView = document.getElementById('onIdlePositionView');
// Initialize locationPicker plugin
var lp = new locationPicker('map', {
setCurrentPosition: true, // You can omit this, defaults to true
}, {
zoom: 15 // You can set any google map options here, zoom defaults to 15
});
// Listen to button onclick event
confirmBtn.onclick = function () {
// Get current location and show it in HTML
var location = lp.getMarkerPosition();
console.log(location);
xmlhttp = new XMLHttpRequest();
xmlhttp.open("REQUEST", "ajax.php", "&location.lat=" + location.lat + "&location.lng=" + location.lng, true);
xmlhttp.send();
};
// Listen to map idle event, listening to idle event more accurate than listening to ondrag event
google.maps.event.addListener(lp.map, 'idle', function (event) {
// Get current location and show it in HTML
var location = lp.getMarkerPosition();
onIdlePositionView.innerHTML = 'The chosen location is ' + location.lat + ',' + location.lng;
console.log(location.lat)
});
</script>
<script>
src="https://code.jquery.com/jquery-3.5.1.js"
integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
crossorigin="anonymous">
</script>
</body>
<style type="text/css">
@media only screen and (max-width: 600px) {
.locu{
width: 40%;
}
.loca
{
margin-left: 75px;
}
.locus{
margin-left: 31px;
}
}
@media only screen and (min-width: 768px) {
.locu,.loca{
margin-left: 306px;
}
.locu{
width: 40%;
}
.locus{
margin-left: 262px !important;
width: 40%;
}
}
#map {
width: 100%;
height: 480px;
}
</style>
Ajax code
<?php include("db/db.php");
if($_GET['confirm']){
print_r($_REQUEST);
$latitude = $_REQUEST['location.lat'];
$longitude = $_REQUEST['location.lng'];
$query = mysql_query("insert into location(latitude, longitude) values('$latitude', '$longitude')") or die(mysql_error());
}
?>