I am trying to clear previous directions from a Google Map. But when you do another search, the Polyline and markers from the previous search still show. I have tried using directionsRenderer.setMap(null), but it doesn't make any difference. Any ideas what I'm doing wrong?
(function () {
Maps.Directions = function(googleMap, data, options) {
var directionsService = new google.maps.DirectionsService(),
directionsRenderer = new google.maps.DirectionsRenderer();
function getDirections() {
directionsService.route(data.request, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsRenderer.setDirections(response);
}
});
}
directionsRenderer.setMap(null);
directionsRenderer.setMap(googleMap);
getDirections();
};
})();