Optional options: MarkerOptionsthe options
Attaches the Marker to a Map object.
The MapLibre GL JS map to add the marker to.
let marker = new Marker()
.setLngLat([30.5, 50.5])
.addTo(map); // add the marker to the map
Get the marker's geographical location.
The longitude of the result may differ by a multiple of 360 degrees from the longitude previously
set by setLngLat because Marker wraps the anchor longitude across copies of the world to keep
the marker on screen.
A LngLat describing the marker's location.
// Store the marker's longitude and latitude coordinates in a variable
let lngLat = marker.getLngLat();
// Print the marker's longitude and latitude values in the console
console.log('Longitude: ' + lngLat.lng + ', Latitude: ' + lngLat.lat )
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
Set the marker's geographical position and move it.
A LngLat describing where the marker should be located.
Create a new marker, set the longitude and latitude, and add it to the map
new Marker()
.setLngLat([-65.017, -16.457])
.addTo(map);
Sets the opacity and opacityWhenCovered properties of the marker.
When called without arguments, resets opacity and opacityWhenCovered to defaults
Optional opacity: stringSets the opacity property of the marker.
Optional opacityWhenCovered: stringSets the opacityWhenCovered property of the marker.
Sets the pitchAlignment property of the marker.
Optional alignment: AlignmentSets the pitchAlignment property of the marker. If alignment is 'auto', it will automatically match rotationAlignment.
Optional popup: Popuplet marker = new Marker()
.setLngLat([0, 0])
.setPopup(new Popup().setHTML("<h1>Hello World!</h1>")) // add popup
.addTo(map);
Sets the rotation property of the marker.
Optional rotation: numberThe rotation angle of the marker (clockwise, in degrees), relative to its respective Marker#setRotationAlignment setting.
Sets the rotationAlignment property of the marker.
Optional alignment: AlignmentSets the rotationAlignment property of the marker. defaults to 'auto'
Add or remove the given CSS class on the marker element, depending on whether the element 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
let marker = new Marker()
marker.toggleClassName('toggleClass')
Opens or closes the Popup instance that is bound to the Marker, depending on the current state of the Popup.
let marker = new Marker()
.setLngLat([0, 0])
.setPopup(new Popup().setHTML("<h1>Hello World!</h1>"))
.addTo(map);
marker.togglePopup(); // toggle popup open or closed
Creates a marker component
Example
Example
Set options
See
Events
Event
dragstartof type Event will be fired when dragging starts.Event
dragof type Event will be fired while dragging.Event
dragendof type Event will be fired when the marker is finished being dragged.