How to close all popups programmatically in mapbox gl?

So, I know what we have Marker.togglePopup()in Mapbox GL Api. But is it possible to programmatically close all pop-ups?

+4
source share
1 answer

Here is an example: https://jsfiddle.net/kmandov/eozdazdr/
Click the buttons in the upper right corner to open / close the pop-up window.

Given that you have a popup and a marker:

var popup = new mapboxgl.Popup({offset:[0, -30]})
    .setText('Construction on the Washington Monument began in 1848.');

new mapboxgl.Marker(el, {offset:[-25, -25]})
    .setLngLat(monument)
    .setPopup(popup)
    .addTo(map);

You can close the popup by calling:

popup.remove();

or you can open it by calling:

popup.addTo(map);

As you can see in Token Source , it togglePopupuses these two methods internally:

togglePopup() {
    var popup = this._popup;

    if (!popup) return;
    else if (popup.isOpen()) popup.remove();
    else popup.addTo(this._map);
}
+4
source

Source: https://habr.com/ru/post/1662225/


All Articles