PBrockmann
PBrockmann

Reputation: 429

How to close multiple popups with Leaflet?

I have found exactly the behavior I am looking for (possible multiple popups and close them when user clicks on the map) from a hack exposed here http://bl.ocks.org/mpmckenna8/9395643

Unfortunately, this hack does not work with last 1.6.0 release. Perhaps there is a mix of options that offers this behavior but I haven't found it.

I have prepared a jsfiddle to explore this: https://jsfiddle.net/PBrockmann/3j40ychf/

var popup1 = L.popup({
  minWidth: 100,
  closeOnClick: false,
  autoClose: false
}).setContent("marker1");

L.circleMarker([51.5072, 0.1275]).addTo(map).bindPopup(popup1);

And also from https://observablehq.com/@pbrockmann/untitled/2

Any help welcome on this.

Upvotes: 0

Views: 152

Answers (1)

peeebeee
peeebeee

Reputation: 2618

This code will close popups on all L.CircleMarkers - is that close enough?

map.on('click', function(e) {
   map.eachLayer(function(layer) {
    if (layer instanceof L.CircleMarker) {
        layer.closePopup()
     }
   })
});

Upvotes: 1

Related Questions