Basically, I am trying to achieve this thing. 1 - Get Lat/long from MySQL using AJAX 2 - Draw Route on Map using direction service and waypoints technique. 3 - When a user clicks on map marker each marker has a clickable function when the marker is clicked location detail will be fetched in a div underneath the map, basically I need to handle click listener on each marker click so that I can perform my desired actions on those clicks.
What I achieve is that:
1 - Able to fetch lat/long using ajax request on page load from MySQL using PHP REST API.
2 - Pass Those Markers to map and draw a route using direction Service. Screenshot for idea reference https://imgur.com/a/ApkPjTN
var i = 0;
var ACoptions = {
componentRestrictions: {
country: "PK"
}
};
var map;
var directionsDisplay;
var directionsService;
function initialize(directionsService, directionsDisplay , waypointElmts , origin1 , designation1) {
directionsDisplay = new google.maps.DirectionsRenderer({
polylineOptions:{
strokeColor:"#00a54f",
strokeOpacity: 1,
strokeWeight:5
}
});
directionsService = new google.maps.DirectionsService();
document.getElementById( 'map' ).style.display = "block";
var melbourne = new google.maps.LatLng(30.3753,69.3451);
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: melbourne,
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
directionsDisplay.setMap(map);
calculateAndDisplayRoute(directionsService, directionsDisplay , waypointElmts , origin1 , designation1);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay , waypointElmts , origin1 , designation1) {
var waypts = [];
for (var i = 0; i < waypointElmts.length; i++) {
waypts.push({
location: waypointElmts[i],
stopover: true,
});
}
directionsService.route({
origin: origin1,
destination: designation1,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
renderDirectionsPolylines(response);
}
else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
wait = true;
setTimeout("wait = true", 2000);
//alert("OQL: " + status);x
} else {
toastr.error('Directions request failed due to '+status,'Error!',
{positionClass: 'toast-top-full-width', containerId: 'toast-top-full-width'});
}
});
}
$.ajax({
type: "POST",
url: "ajax-requests/ajaxm.php",
dataType: "json",
data: { what_need : 'detail_routesheet'
,_token: '<?php echo $_SESSION['_token'];?>',
route_id: <?php echo $_GET['routeid']?>
},
success: function(response){
var len = response.length;
for(var i=0; i<len; i++){
var start_location = response[i].start_location;
var end_location = response[i].end_location;
var waypoints = response[i].waypoints;
var datetime = response[i].datetime;
}
var array = $.map(waypoints, function(value, index) {
return [value];
});
function firstAndLast(array) {
var firstItem = array[0];
var lastItem = array[array.length-1];
var objOutput = {
start : firstItem,
end : lastItem
};
return objOutput;
}
var display = firstAndLast(array);
var start_locationlatlng = display.start;
var end_locationlatlng = display.end;
//calculateAndDisplayRoute(directionsService, directionsDisplay ,waypoints , originmap , designationmap);
array.shift();
array.pop();
initialize(directionsService, directionsDisplay , array , start_locationlatlng , end_locationlatlng)
}
});
1 - When User Click I can handle the click event on each marker generated by direction service waypoint