Marker
Markers are interactive map elements placed at precise geographic coordinates, commonly used to highlight points of interest, locations, or other key features.
Constructor
Creates a new Marker with the specified options.
Parameters:
| Name | Type | Description |
|---|---|---|
| options | MarkerOptions | Options for configuring the marker's appearance and behavior. |
Example:
// Create a red, draggable marker
const marker = new Marker({
color: '#FF0000',
draggable: true,
anchor: 'bottom',
rotation: 45
})
.setLngLat([12.55, 55.66])
.addTo(map);
Properties
The following properties are set via the MarkerOptions object during construction and
can be accessed or modified via the respective getter/setter methods.
The DOM element that represents the marker on the map. If not provided in options, a default element is created.
The color to use for the default marker if no custom element is
provided. Default: #3FB1CE (light blue).
If true, the marker can be dragged to a new position on
the map. Default: false.
A string indicating which part of the marker should be positioned
closest to the coordinate set via setLngLat. Valid values include:
'center', 'top', 'bottom', 'top-left', etc.
Default: 'center'.
The offset in pixels to apply relative to the element's center.
Useful for adjusting the position of custom icons. Default: [0, 0].
The rotation angle of the marker in degrees, relative to its
rotationAlignment setting. Default: 0.
The alignment of the marker's rotation. Can be 'map',
'viewport', or 'auto'. Default: 'auto' (viewport).
The alignment of the marker to the map's pitch. Can be
'map', 'viewport', or 'auto'. Default:
'auto' (matches rotationAlignment).
Events
The Marker class emits various events that developers can listen to.
const markerElement = marker.getElement();
// 2. Attach a standard DOM click event listener to the element
markerElement.addEventListener('click', (e) => {
console.log('Marker was clicked!');
});
Fired when the marker element is clicked.
console.log('Drag started!');
});
Fired when the user starts dragging the marker.
console.log('Drag!');
});
Fired repeatedly while the marker is being dragged.
console.log('Drag started!');
});
Fired when the user stops dragging the marker.
Methods
Adds the marker to the given map. This is required for the marker to become visible.
Parameters:
| Parameter | Description |
|---|---|
| map | The map object to add the marker to. |
this (The Marker instance)Removes the marker from the map.
this (The Marker instance)Sets the marker's geographical position.
Parameters:
| Parameter | Description |
|---|---|
| lngLat | The new longitude and latitude array, or LngLat object. |
this (The Marker instance)Gets the marker's current geographical location.
LngLat (A LngLat describing the marker's location)Sets the offset of the marker, in pixels, relative to the anchor.
Parameters:
| Parameter | Description |
|---|---|
| offset | The offset in pixels (e.g., [0, -10]). |
this (The Marker instance)Sets whether the marker can be dragged by the user.
Parameters:
| Parameter | Description |
|---|---|
| draggable | A boolean indicating whether the marker is draggable. |
this (The Marker instance)Returns true if the marker is draggable.
booleanBinds a Popup instance to the Marker. The popup will toggle on click.
Parameters:
| Parameter | Description |
|---|---|
| popup | The Popup instance to bind, or undefined to unbind. |
this (The Marker instance)Toggles the visibility of the Popup bound to this Marker.
this (The Marker instance)Returns the marker's underlying HTML element.
HTMLElementSets the rotation angle of the marker in degrees.
Parameters:
| Parameter | Description |
|---|---|
| rotation | The new rotation angle in degrees. |
this (The Marker instance)