On Map if there already is setDirection of user(A) path and then if I set new user(B) direction path then I want to clear old user(A) route path.
I am facing an issue, when I click on user(B) then direction path set of user(B) but user(A) path did not clear.
I have also set blank object, array in directionsDisplay.setDirections({ routes: [] }); but it did't work for me.
Can you please see if I missed anything - do guide me
Below is my code:
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var route = [];
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(23.023292, 72.571144);
var mapOptions = {
zoom: 11,
center: chicago
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
//console.log(map);
directionsDisplay.setMap(map);
}
$(document).ready(function(){
google.maps.event.addDomListener(window, 'load', initialize);
});
function calcRoute(data) {
if(data.length)
{
for (var key in data) {
var track = data[key];
var start = new google.maps.LatLng(track[0].latitude,track[0].longitude);
var end = new google.maps.LatLng(track[track.length - 1].latitude,track[track.length - 1].longitude);
var waypoints = track;
waypoints.shift();
waypoints.pop();
var waypointsgoogle = [];
for(var waykey in waypoints)
{
waypointsgoogle.push({
location: new google.maps.LatLng(waypoints[waykey].latitude,waypoints[waykey].longitude),
stopover: false
});
}
directionsService.route({
origin: start,
destination: end,
optimizeWaypoints:true,
travelMode: google.maps.TravelMode.DRIVING,
waypoints:waypointsgoogle
}, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var directionsDisplay = new google.maps.DirectionsRenderer({
preserveViewport: true
});
directionsDisplay.setOptions({
polylineOptions: {
strokeColor: '#0089d0'
}
});
directionsDisplay.setMap(map);
directionsDisplay.setDirections(response);
} else {
alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status);
}
});
}
}
else`enter code here`
{
directionsDisplay = new google.maps.DirectionsRenderer();
}
}