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

- 22,633
- 10
- 99
- 115

- 345
- 1
- 4
- 12
-
1Same as https://stackoverflow.com/questions/40849927/how-to-hide-all-opened-marker-popups-in-mapbox-gl, this assumes the popups are tied to some markers. I use popups on the map which are not attached to any marker (like in the example https://www.mapbox.com/mapbox-gl-js/example/popup/). Is there some way to get all open popups from the map? – Suma May 03 '18 at 08:45
6 Answers
Here is an example: https://jsfiddle.net/kmandov/eozdazdr/
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]})
.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 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);
}

- 3,130
- 16
- 15
-
5This is useful for closing a particular popup, but doesn't address the issue of "all popups" on the map, as it assumes you have a reference to `popup` in order to call `popup.remove()`. Is there a way to get a handle on all popups on the map? – Stephen Lead Apr 17 '20 at 02:00
-
1@StephenLead see [my answer](https://stackoverflow.com/a/63006609/947370) for how to close all popups, no matter how they were created. – squarecandy Jul 21 '20 at 02:52
The accepted answer didn't apply to my use case (I wasn't using a Marker). I was able to come up with a different solution by utilizing the mapbox's built-in event workflow. Hopefully this helps someone else.
Mapbox allows you to listen to events on the map (and manually trigger them). The documentation doesn't mention it, but you can use custom events.
Given you have a popup:
// Create popup and add it to the map
const popup = new mapboxgl.Popup({ offset: 37, anchor: 'bottom' }).setDOMContent('<h5>Hello</h5>').setLngLat(feature.geometry.coordinates).addTo(map);
// Add a custom event listener to the map
map.on('closeAllPopups', () => {
popup.remove();
});
When you want to close all popups, fire the event:
map.fire('closeAllPopups');

- 1,160
- 11
- 12
-
You are essentially storing popup in a variable and accessing it via that. Wish there was a way to access it from the map itself. Like, `map.getMarker(id)` – Pankaj Jan 28 '21 at 21:11
-
I really like this method, wasn't aware you could create and call custom functions (e.g. "closeAllPopups") until reading this answer. I'll be using this a lot more in the future when working with Mapbox – Harry Ronchetti Oct 27 '22 at 08:32
Mapbox automatically uses the class .mapboxgl-popup
for the popup. You can also add additional classes with options.className
.
So, if you have jQuery available, just do:
$('.mapboxgl-popup').remove();
Or plain javascript:
const popup = document.getElementsByClassName('mapboxgl-popup');
if ( popup.length ) {
popup[0].remove();
}
I'm pretty sure you can assume there's only ever one popup open. The default behavior seems to be that if one is open and a second item is clicked, the first popup is removed from the DOM completely when the second one opens. If your application allows multiple open popups somehow, you will need to loop through and remove each with plain js, instead of using the first item only.

- 4,894
- 3
- 34
- 45
-
1It is not safe to assume that there is only one pop up. You can set `{closeOnClick: false}` and have as many pop ups as fit on the screen. – blindguy May 14 '21 at 23:25
-
Ok, cool, good call, but the basic premise should still work. The jQuery above should "just work" and the plain js you would have to loop through `popup` to `remove()` each one. – squarecandy May 15 '21 at 04:56
I know it's an old question but I recently worked on Mapbox. As of mapbox-gl v2.3, mapboxgl.Popup has a closeOnClick property where if set to true, the popup disappears when you click away from the popup.
let popup = new mapboxgl.Popup({
anchor: "top",
closeOnClick: true,
});
map.on(
"click",
"location",
(e) => {
map.getCanvas().style.cursor = "pointer";
let coordinates = e.features[0].geometry.coordinates.slice();
popup
.setLngLat(coordinates)
.setHTML("what I want to display")
.addTo(map);
}
);
Alternatively, you can show the popup on "mouseenter" instead of on "click" and add a "mouseleave" event to remove the popup:
map.on(
"mouseleave",
"location",
() => {
map.getCanvas().style.cursor = "";
popup.remove();
}
);

- 1,807
- 1
- 10
- 6
Iterate over active popups and remove them
var popups = []; // all your popups
popups.forEach(function (item) {
item.popup.remove();
});

- 11
- 3
wrap your popup with React.StrictMode
and closeOnClick={false}
.
const [popup, setPopup] = useState<boolean>(false);
...
{popup && (
<React.StrictMode>
<Popup longitude={52.001126260374704} latitude={34.906269171550214}
anchor="bottom"
onClose={() => {
setPopup(false)
}}
closeOnClick={false}
>
You are here
</Popup>
</React.StrictMode>
)}

- 11
- 3