Popup
Pop-up component.
new Popup(options: Object?)
Parameters
| Name | Type | Description | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | Object | Requiredparameter
|
Case
var markerHeight = 50, markerRadius = 10, linearOffset = 25;
var popupOffsets = {
'top': [0, 0],
'top-left': [0,0],
'top-right': [0,0],
'bottom': [0, -markerHeight],
'bottom-left': [linearOffset, (markerHeight - markerRadius + linearOffset) * -1],
'bottom-right': [-linearOffset, (markerHeight - markerRadius + linearOffset) * -1],
'left': [markerRadius, (markerHeight - markerRadius) * -1],
'right': [-markerRadius, (markerHeight - markerRadius) * -1]
};
var popup = new mapmost.Popup({offset: popupOffsets, className: 'my-class'})
.setLngLat(e.lngLat)
.setHTML("<h1>Hello World!</h1>")
.setMaxWidth("300px")
.addTo(map);
##Instance members
addTo
Add a popup to the map.
Parameters
| Name | Type | Description |
|---|---|---|
| map | Map | RequiredMapmost GL JS map to add Popup. |
Case
var marker = new mapmost.Popup()
.setLngLat([30.5, 50.5])
.addTo(map); // Add a pop-up window on the map
getElement
Returns the Popup’s HTML element.
Case
// Modify the font size of the `Popup` element
const popup = new mapmost.Popup()
.setLngLat([-96, 37.8])
.setHTML("<p>Hello World!</p>")
.addTo(map);
const popupElem = popup.getElement();
popupElem.style.fontSize = "25px";
getLngLat
Returns the geographical location of the popup’s anchor.
The returned longitude will vary in multiples of 360 degrees based on the longitude previously set by setLngLat. This is because the Popup needs to completely surround the longitude of the anchor to ensure that the popup window can be displayed on the screen.
Case
const lngLat = popup.getLngLat();
getMaxWidth
Returns the maximum width of the popup window.
Case
const maxWidth = popup.getMaxWidth();
isOpen
If the pop-up window is in the pop-up state, it is true, if it is in the closed state, it is false.
Case
const isPopupOpen = popup.isOpen();
remove
Removed popup from adding map.
Case
var popup = new mapmost.Popup().addTo(map);
popup.remove();
setDOMContent
Sets the popup content to an element provided as a DOM node.
Parameters
| Name | Type | Description |
|---|---|---|
| htmlNode | Node | RequiredDOM node used as pop-up window content. |
Case
// Generate an element with pop-up window content
var div = window.document.createElement('div');
div.innerHTML = 'Hello, world!';
var popup = new mapmost.Popup()
.setLngLat(e.lngLat)
.setDOMContent(div)
.addTo(map);
setHTML
Sets the popup content to HTML provided as a string.
This method does not filter or sanitize HTML, so it should only be used with trusted content.
Parameters
| Name | Type | Description |
|---|---|---|
| html | String | RequiredHTML string used to represent the content of the pop-up window. |
Case
const popup = new mapmost.Popup()
.setLngLat(e.lngLat)
.setHTML("<h1>Hello World!</h1>")
.addTo(map);
setLngLat
After setting the geographical location of the anchor of the pop-up window, move the pop-up window there. Replaces the effect of trackPointer().
Parameters
| Name | Type | Description |
|---|---|---|
| lnglat | LngLatLike | RequiredSet the geographical location of the pop-up window. |
Case
popup.setLngLat([-122.4194, 37.7749]);
setMaxWidth
Set the maximum width of the pop-up window. Essentially, you are setting the CSS max-width property.
Parameters
| Name | Type | Description |
|---|---|---|
| maxWidth | String | RequiredA string representing the maximum width value. |
Case
popup.setMaxWidth('50');
setText
Set the content of the pop-up window to a string of text.
This function will create a Text node within the DOM, making it impossible for the original HTML to be inserted. Use this method as a security measure against XSS when the popup content is provided by the user.
Parameters
| Name | Type | Description |
|---|---|---|
| text | String | RequiredThe text content of the pop-up window. |
Case
var popup = new mapmost.Popup()
.setLngLat(e.lngLat)
.setText('Hello, world!')
.addTo(map);
trackPointer
Use the anchor of the popup to track the position of the cursor on the screen with the help of a pointing device (hidden in touch mode). Replaces the effect of setLngLat. In most cases, closeOnClick and closeButton should be set to false here.
Case
const popup = new mapmost.Popup({closeOnClick: false, closeButton: false})
.setHTML("<h1>Hello World!</h1>")
.trackPointer()
.addTo(map);
event
open
Triggered when the pop-up window is opened manually or programmatically. Parameters
| Name | Type | Description |
|---|---|---|
| popup | Popup | RequiredThe opened object. |
close
Triggered when the pop-up window is closed manually or programmatically. Parameters
| Name | Type | Description |
|---|---|---|
| popup | Popup | RequiredThe object that was closed. |