[GIS] How to close all tooltips defined against the map object

leaflettooltip

In Leaflet it's possible to bind one or more tooltips to an individual marker, or a layer, or the base map object.

enter image description here

How can I close all open tooltips that have been bound to the map object?

var map = L.map("map");
L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png").addTo(map);
map.setView([48.85, 2.35], 12);

// Define some tooltips against the map object
var tooltip1 = map.openTooltip("tooltip 1",[48.84, 2.34]);
map.openTooltip("tooltip 2",[48.83, 2.33]);
map.openTooltip("tooltip 3",[48.82, 2.32]);

// Attempt to close tooltips, individually or en masse - neither works
map.closeTooltip(tooltip1);
map.closeTooltip();

See also the JS Fiddle version of the above code.

Best Answer

You can get all layers from your map, and remove them from the map. To avoid removing the tile layer (background), filter with if(layer.options.pane === "tooltipPane").

map.eachLayer(function(layer) {
    if(layer.options.pane === "tooltipPane") layer.removeFrom(map);
});

Fiddle: https://jsfiddle.net/3v7hd2vx/392/