6

I'm using the DirectionsRenderer to display a path, though after I'm done with that I'd like remove the polyline and carry on with things. I don't seem to have control over markers and polylines created by this function.

Does anyone know how to remove such polylines, if it's not possible, other suggestions?

I now you can use the suppress property, though since I'm using the polylines in the first phase, this doesn't really solve anything.

Severely frustrated.. Cheers!

user2314737
  • 27,088
  • 20
  • 102
  • 114
user594388
  • 61
  • 1
  • 3

3 Answers3

2

Here is a solution to your problem: either use the suppressPolylines option or remove the renderer from the map

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var time;

var pointA = new google.maps.LatLng(48.86, 2.35);
var pointB = new google.maps.LatLng(33.7167, 73.0667);

function initialize() {

  var rendererOptions = {
      map: map,
      draggable: true
    }
    // Instantiate a directions service.
  directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);

  // Create a map and center it on islamabad.
  var islamabad = new google.maps.LatLng(33.7167, 73.0667);
  var mapOptions = {
    zoom: 13,
    center: islamabad
  }
  map = new google.maps.Map(document.getElementById('map'), mapOptions);
  directionsDisplay.setMap(map);
  calcRoute();
}

function calcRoute() {
  var start = pointA;
  var end = pointB;
  var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
};

function removeRoute() {
  directionsDisplay.setOptions({
    suppressPolylines: true
  });
  // this "refreshes" the renderer
  directionsDisplay.setMap(map);
};

function removeRouteNull() {
  directionsDisplay.setMap(null);
};
google.maps.event.addDomListener(window, 'load', initialize);
#map {
  height: 280px;
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<button onclick="removeRoute()">Remove route (suppressPolylines)</button>
<button onclick="removeRouteNull()">Remove route (setMap(null))</button>
<button onclick="initialize()">Undo all</button>
<section id="map"></section>
user2314737
  • 27,088
  • 20
  • 102
  • 114
0

This is how a polyline is be removed from google maps v3. Hope it helps you.

var line = [];
 var flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates, //create an array of LatLng objects
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 2
    });
 line.push(flightPath);

Now you are pushing all the polyline objects into an array line. You can make it invisible or remove it from the map by looping it like this:

for (i=0; i<line.length; i++) 
{                           
  line[i].setMap(null); //or line[i].setVisible(false);
}
Aravind Asok
  • 514
  • 1
  • 7
  • 18
0

Use

[self.polyline setMap:nil];

Rajan Twanabashu
  • 4,586
  • 5
  • 43
  • 55