41

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
daveredfern
  • 1,235
  • 4
  • 14
  • 15
  • http://stackoverflow.com/questions/5232756/remove-route-with-google-map question already solved! Firstly search than ask. – lesyk Jun 01 '12 at 11:08
  • From here: http://stackoverflow.com/questions/5232756/remove-route-with-google-map try directionsDisplay.setDirections({routes: []}); – Pr Shadoko Jun 10 '13 at 14:50
  • Possible duplicate of [remove route with google map](https://stackoverflow.com/questions/5232756/remove-route-with-google-map) – Keith Aug 08 '18 at 15:27

6 Answers6

61

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

directionDisplay.setMap(null);
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Josnidhin
  • 12,469
  • 9
  • 42
  • 61
  • 4
    Similarly if you are using a panel to show directions, you can clear that by doing `directionDisplay.setPanel(null);` – totallyNotLizards Jul 10 '14 at 09:13
  • What if I want to use the same map to display another set of directions? I setMap on the renderer again? There has got to be a better way of doing this! – Aamir Khan Nov 28 '16 at 11:14
54

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

directionDisplay.set('directions', null);
Jimmy Collazos
  • 1,924
  • 2
  • 13
  • 7
  • 1
    This was a useful addition which helped me as I needed to maintain the reference. Thanks! – David Jan 10 '14 at 18:12
  • 1
    I feel like this should be marked as the answer. The Google supported API documentation binds the directionsDistplay object to the map and panel on initialization. So when manipulating the directions with on-page events, it seems to make more sense to clear the directions contents rather than unbind the connection with the map and panel. – Mark W Dickson Oct 22 '16 at 03:49
  • 2
    This one is the best answer here: it allows to show/hide directions without reinitializing map. – Vadim Nov 15 '16 at 04:44
9

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

bharat
  • 1,762
  • 1
  • 24
  • 32
1

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

// Clear past routes
    if (directionsDisplay != null) {
        directionsDisplay.setMap(null);
        directionsDisplay = null;
    }
Friendly Code
  • 1,585
  • 1
  • 25
  • 41
1

That should read as:

directionDisplay.setMap(null);
Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
webewitch
  • 27
  • 1
  • I found it http://stackoverflow.com/questions/3264072/how-to-remove-default-a-b-markers-on-google-maps-route-direction – B L Praveen Jan 07 '14 at 11:48
-2

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)

Community
  • 1
  • 1
user2314737
  • 27,088
  • 20
  • 102
  • 114
  • If you suppress poly lines then they will never be shown. Here the context of the question is different – Jassi Dec 04 '19 at 11:11