hide and show a polyline in leaflet?

Baptiste Metge picture Baptiste Metge · Oct 17, 2013 · Viewed 12.7k times · Source

I'm using leaflet for show raw itinerary to go to some markers. I'm showing my itinerary with a leaflet polyline. But I would like to be able to

How to hide and show a polyline in leaflet ?

I can do this :

$('.leaflet-overlay-pane').hide();

and

$('.leaflet-overlay-pane').show();

But this will show and hide all my polyline. I would like to be able to hide and show them separately.

Thanks.

Answer

Edu picture Edu · Oct 17, 2013

If you have a reference to the polyline

var polyline = L.polyline(...);

Then you can use

map.addLayer(polyline);//For show
map.removeLayer(polyline);// For hide

at the moment I think there is no native method to only hide/show, maybe in the 0.7 version

Other solution is to access to the object container, in a old commet from the maintainer

I don't think there's an easy solution, for tile layers at least. :( I'll try to handle this sooner.
For vectors, you can change path._container.style.display, and for markers - marker._image.style.display and marker._shadow.style.display.