Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Directions from Google Maps

I'm displaying a list of places on a map using the google places api. Users can get directions to each of those places individually. The directions appear on the map fine and everything works, but each time they get directions to a different place it adds it to the map along with the old one(s). I want to overwrite the existing directions on the map when a new place is selected. So only one set of directions appear at anyone time. Ideally, I only want one route marked on the map and the one list of directions displayed.

I've tried adding the the following to clear all directions before I set the new ones:

directionDisplay = new google.maps.DirectionsRenderer();
directionDisplay.suppressMarkers = true;
directionDisplay.setMap(null);

As suggested here: Google Maps Version 3 Remove directions markers but to no avail.

No matter how much googling and search through the documentation I just can't seem to get it.

Any help would be greatly appreciated.

JA

like image 499
Jim Avatar asked Apr 16 '12 20:04

Jim


1 Answers

There is no need to clear the directions before rendering new ones.

Use 1 global variable for directionDisplay, the current directions will be removed as soon as you call setDirections() to render new directions.

like image 142
Dr.Molle Avatar answered Sep 22 '22 08:09

Dr.Molle