1

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)

enter image description here

After drawing the first, I want to draw Midland - Albuquerque. But first route still appears on the map

enter image description here

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);
        }

    });
}
Burak Özdemir
  • 510
  • 8
  • 18
  • Please provide a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) that demonstrates the issue **in the question itself** – geocodezip Nov 29 '15 at 20:37

3 Answers3

1

Keep references to all the DirectionsRenderer objects and set all of their map properties to null.

function requestDirections(start, end, routeToDisplay, main_route) {
    // remove any old renderers from the map
    for (var i = 0; i < directionsRenderers.length; i++) {
        directionsRenderers[i].setMap(null);
    }
    // clear out the directionsRenderers array
    directionsRenderers = [];

    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
        provideRouteAlternatives: main_route
    };

    directionsService.route(request, 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);
            }
        }
    });
}

function renderDirections(result, rendererOptions, routeToDisplay) {

    if (routeToDisplay == 0) {
        var _colour = '#00458E';
        var _strokeWeight = 4;
        var _strokeOpacity = 1.0;
        var _suppressMarkers = false;
    } else {
        var _colour = '#ED1C24';
        var _strokeWeight = 4;
        var _strokeOpacity = 0.7;
        var _suppressMarkers = false;
    }

    // create new renderer object
    var directionsRenderer = new google.maps.DirectionsRenderer({
        draggable: false,
        suppressMarkers: _suppressMarkers,
        polylineOptions: {
            strokeColor: _colour,
            strokeWeight: _strokeWeight,
            strokeOpacity: _strokeOpacity
        }
    })
    directionsRenderer.setMap(map);
    directionsRenderer.setDirections(result);
    directionsRenderer.setRouteIndex(routeToDisplay);

    // push new renderer onto directionsRenderers array;
    directionsRenderers.push(directionsRenderer);
}

proof of concept fiddle

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • You only posted one function that uses a `DirectionsRenderer`. – geocodezip Nov 29 '15 at 20:46
  • renderDirections() uses DirectionsRenderer. But How Can it know that new route wanted ? (I'm using two search box and autocomplete for that boxes) – Burak Özdemir Nov 29 '15 at 20:54
  • See my comment on your question if you need more context on the answer (provide a minimal, compete, tested example that demonstrates the issue). – geocodezip Nov 29 '15 at 21:46
0

Try this :

directionsDisplay.setMap(null);

You can see the example code here http://code.google.com/apis/maps/documentation/javascript/examples/directions-simple.html

ske57
  • 577
  • 4
  • 21
  • I've tried that but it doesnt work on alternative routes. You can see the source code for alternative routing on the geocodezip site. – Burak Özdemir Nov 29 '15 at 20:31
0

I had the same problem and I solved this by reinitialize the google map before every draw.

       map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            center: { lat: 24.549798, lng: 46.5473599 }
        });
Dongolo Jeno
  • 414
  • 6
  • 8