Optional
options: PopupOptionsthe options
Adds the popup to a map.
The MapLibre GL JS map to add the popup to.
new Popup()
.setLngLat([0, 0])
.setHTML("<h1>Null Island</h1>")
.addTo(map);
Returns the geographical location of the popup's anchor.
The longitude of the result may differ by a multiple of 360 degrees from the longitude previously
set by setLngLat
because Popup
wraps the anchor longitude across copies of the world to keep
the popup on screen.
The geographical location of the popup's anchor.
Adds a listener to a specified event type.
The event type to add a listen for.
The function to be called when the event is fired.
The listener function is called with the data object passed to fire
,
extended with target
and type
properties.
Adds a listener that will be called only once to a specified event type.
The listener will be called first time the event fires after the listener is registered.
The event type to listen for.
Optional
listener: ListenerThe function to be called when the event is fired the first time.
this
or a promise if a listener is not provided
Sets the popup's content to the element provided as a DOM node.
A DOM node to be used as content for the popup.
Create an element with the popup content
let div = document.createElement('div');
div.innerHTML = 'Hello, world!';
let popup = new Popup()
.setLngLat(e.lngLat)
.setDOMContent(div)
.addTo(map);
Sets the popup's content to the HTML provided as a string.
This method does not perform HTML filtering or sanitization, and must be used only with trusted content. Consider Popup#setText if the content is an untrusted text string.
A string representing HTML content for the popup.
let popup = new Popup()
.setLngLat(e.lngLat)
.setHTML("<h1>Hello World!</h1>")
.addTo(map);
Sets the geographical location of the popup's anchor, and moves the popup to it. Replaces trackPointer() behavior.
The geographical location to set as the popup's anchor.
Sets the popup's maximum width. This is setting the CSS property max-width
.
Available values can be found here: https://developer.mozilla.org/en-US/docs/Web/CSS/max-width
A string representing the value for the maximum width.
Sets the popup's offset.
Optional
offset: OffsetSets the popup's offset.
Sets the popup's content to a string of text.
This function creates a Text node in the DOM, so it cannot insert raw HTML. Use this method for security against XSS if the popup content is user-provided.
Textual content for the popup.
let popup = new Popup()
.setLngLat(e.lngLat)
.setText('Hello, world!')
.addTo(map);
Add or remove the given CSS class on the popup container, depending on whether the container currently has that class.
Non-empty string with CSS class name to add/remove
if the class was removed return false, if class was added, then return true, undefined if there is no container
let popup = new Popup()
popup.toggleClassName('toggleClass')
Tracks the popup anchor to the cursor position on screens with a pointer device (it will be hidden on touchscreens). Replaces the setLngLat
behavior.
For most use cases, set closeOnClick
and closeButton
to false
.
let popup = new Popup({ closeOnClick: false, closeButton: false })
.setHTML("<h1>Hello World!</h1>")
.trackPointer()
.addTo(map);
A popup component.
Example
Create a popup
Example
Create a popup
Example
See
Events
Event
open
of type Event will be fired when the popup is opened manually or programmatically.Event
close
of type Event will be fired when the popup is closed manually or programmatically.