Currently I can draw alternative routes by using Google Maps API. But I've a little problem after drawing first route. For example the first route below(Winters-New Mexico Center)
After drawing the first, I want to draw Midland - Albuquerque. But first route still appears on the map
I've tried to add a code block to renderDirections() to solve it but I think that code block works only for single routes:
if(directionsDisplay != null) {
directionsDisplay.setMap(null);
directionsDisplay = null;
}
How can I delete the first route before drawing the second ? Here are all of the JS code:
function renderDirections(result, rendererOptions, routeToDisplay)
{
if(routeToDisplay==0)
{
var _colour = '#008bff';
var _strokeWeight = 4;
var _strokeOpacity = 1.0;
var _suppressMarkers = false;
}
else
{
var _colour = '#444753';
var _strokeWeight = 4;
var _strokeOpacity = 0.7;
var _suppressMarkers = false;
}
directionsDisplay = new google.maps.DirectionsRenderer({
draggable: false,
suppressMarkers: _suppressMarkers,
polylineOptions: {
strokeColor: _colour,
strokeWeight: _strokeWeight,
strokeOpacity: _strokeOpacity
}
});
directionsDisplay.setMap(map);
directionsDisplay.setDirections(result);
directionsDisplay.setRouteIndex(routeToDisplay);
}
function requestDirections(start, end, travel_mode, directionsService,directionsDisplay, routeToDisplay, main_route) {
directionsService.route({
origin: {'placeId': start},
destination: {'placeId': end},
travelMode: travel_mode,
provideRouteAlternatives: main_route
}, function(result, status) {
if (status === google.maps.DirectionsStatus.OK)
{
if(main_route)
{
var rendererOptions = getRendererOptions(true);
for (var i = 0; i < result.routes.length; i++)
{
renderDirections(result, rendererOptions, i);
}
}
else
{
var rendererOptions = getRendererOptions(false);
renderDirections(result, rendererOptions, routeToDisplay);
}
}
else{
window.alert('Şu nedenden dolayı herhangi bir yol bulunamadı: ' + status);
}
});
}