daveredfern
daveredfern

Reputation: 1255

Remove directions from google map api v3

I have a google map using API v3 which gets directions from one location to another. The app works great but the window which gets the directions is an overlay on the map. I'd like it so when this window is closed directions are removed from the map but other markers remain.

I have tried the following:

$('#content .close').live('click', function() {
$('#content').hide();
directionDisplay = new google.maps.DirectionsRenderer();
directionDisplay.suppressMarkers = true;
directionDisplay.setMap(map);
return false;
});

This seems to hide the window as expected but doesn't do anything regards with removing directions from the map.

Any help is much appreciated.

Dave.

Upvotes: 42

Views: 47742

Answers (6)

bharat
bharat

Reputation: 1776

You can also use : directionsDisplay.setDirections({routes: []});

Upvotes: 9

Friendly Code
Friendly Code

Reputation: 1645

None of the above worked for me, this is what I needed:

// Clear past routes
    if (directionsDisplay != null) {
        directionsDisplay.setMap(null);
        directionsDisplay = null;
    }

Upvotes: 1

user2314737
user2314737

Reputation: 29317

Using directionDisplay.setMap(null); will remove the whole directions renderer overlay, including markers. If you just want to remove the routes keeping the markers you can use setOptions to change the options settings of DirectionsRenderer for suppressPolylines after initialization

directionsDisplay.setOptions({
    suppressPolylines: true
  });

(see also my other similar answer)

Upvotes: -2

Jimmy Collazos
Jimmy Collazos

Reputation: 1924

You can try this, and not lose reference to the map

directionDisplay.set('directions', null);

Upvotes: 54

Josnidhin
Josnidhin

Reputation: 12504

You can change the map binding for the DirectionsRenderer to "null" to remove the direction overlay

directionDisplay.setMap(null);

Upvotes: 62

webewitch
webewitch

Reputation: 27

That should read as:

directionDisplay.setMap(null);

Upvotes: 1

Related Questions