How to close all popups programmatically in mapbox gl?


So, I know that we have Marker.togglePopup() in Mapbox GL API.
But can we close all popups programmatically?


Here is an example:
Click the buttons at the top right to open/close the popup.

Given 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]})

You can close the popup by calling:


or you can open it by calling:


As you can see in the Marker source, togglePopup uses these two methods internally:

togglePopup() {
    var popup = this._popup;

    if (!popup) return;
    else if (popup.isOpen()) popup.remove();
    else popup.addTo(this._map);

Answered By – kmandov

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published