Popup
A Popup is an information window that can be attached to a specific geographical coordinate on the map. It can contain custom HTML content and includes a close button by default.
Constructor
Creates a new Popup with the specified options.
Parameters:
| Name | Type | Description |
|---|---|---|
| options | PopupOptions | Options for configuring the popup's appearance and behavior. |
Example:
// 1. Create a popup instance
const popup = new Popup({
closeButton: false,
closeOnClick: false,
offset: 25
})
.setLngLat([12.55, 55.66]) // Set its position
.setHTML("<h3>Hello!</h3><p>This is a custom popup.</p>") // Set its content
.addTo(map); // Add it to the map
Properties
The following properties are set via the PopupOptions object during construction.
If true, a close button will appear in the top right
corner of the popup. **Default:** true.
If true, the popup will close when the map is clicked.
**Default:** true.
The offset of the popup's anchor point from the coordinates. This
can be a single number (for uniform offset), a PointLike array [x, y], or an object
specifying offsets for different anchor positions. **Default:** 15.
The preferred position of the popup relative to the location
specified by setLngLat. Valid values are the same as for Marker:
'top', 'bottom', 'left', 'right', etc.
**Default:** 'bottom'.
A space-separated string of class names to add to the popup container element.
Events
The Popup class emits events related to its open/close state.
Fired when the popup is opened.
Fired when the popup is closed.
Methods
Adds the popup to the given map. This is required for the popup to become visible.
Parameters:
| Parameter | Description |
|---|---|
| map | The map object to add the popup to. |
this (The Popup instance)Removes the popup from the map and destroys the component.
this (The Popup instance)Checks if the popup is currently open on the map.
booleanSets the HTML content of the popup.
Parameters:
| Parameter | Description |
|---|---|
| html | The raw HTML string to set as the popup content. |
this (The Popup instance)Sets the plain text content of the popup. Note that any HTML tags will be escaped.
Parameters:
| Parameter | Description |
|---|---|
| text | The plain text string to set as the popup content. |
this (The Popup instance)Sets the content of the popup to a DOM element.
Parameters:
| Parameter | Description |
|---|---|
| htmlNode | A DOM Node to use as the popup content (e.g., a div). |
this (The Popup instance)Sets the popup's geographical position. This is required if the popup is not bound to a Marker.
Parameters:
| Parameter | Description |
|---|---|
| lngLat | The new longitude and latitude array, or LngLat object. |
this (The Popup instance)Gets the popup's current geographical location.
LngLat | undefined