Is there any way of programatically close a popup?
I couldn't find any way of doing this on the documentation.
Yes, you can use Popup#remove
But that would actually remove completely the popup of the map isn't it?
If I want to open it again I should call addTo(map) again?
@diegomura you're correct, we don't have a method for hiding popups with css (ex visibility:none) . the pattern of using Popup#addTo(map) and Popup#remove() is what I would suggest!
Great @mollymerp. Thanks for your help!
What if you don't have access to the popup created, say it was created through an evented click using map.fire('click'), is there an alternative way of closing the popup?
remove works to totally remove the popup, just like add works to add the popups, but to trigger a popup, you have to setup something to "open" the popup, and the equivalent call would be a "close" method. You can "close" the popup without removing the functionality entirely by clicking the popup "x" button, seems like we should be able to do this programmatically. And it looks like we can, except the method is marked as private:
Even so, it's not technically hidden, so you can do:
let popup = new mapboxgl.Popup();
....
popup._onClickClose()
@chriszrc you can also just call the public method popup.remove() directly. we can't really think of a compelling case to essentially duplicate the remove functionality with a close method so this is the way that we recommend "closing" popups. if anyone has a good use case for a separate close method (say remove is somehow not performant enough), let us know.