Skip to content

Class: Marker

Creates a marker component

Example

let marker = new Marker()
  .setLngLat([30.5, 50.5])
  .addTo(map);

Example

Set options

let marker = new Marker({
    color: "#FFFFFF",
    draggable: true
  }).setLngLat([30.5, 50.5])
  .addTo(map);

See

Events

dragstart Fired when dragging starts, marker object that is being dragged

drag Fired while dragging. marker object that is being dragged

dragend Fired when the marker is finished being dragged, marker object that was dragged

Hierarchy

Marker

Constructors

constructor

new Marker(options?): Marker

Parameters

Name Type Description
options? MarkerOptions the options

Returns

Marker

Overrides

Evented.constructor

Defined in

src/ui/marker.ts:150

Methods

addClassName

addClassName(className): void

Adds a CSS class to the marker element.

Parameters

Name Type Description
className string on-empty string with CSS class name to add to marker element

Returns

void

Example

let marker = new Marker()
marker.addClassName('some-class')

Defined in

src/ui/marker.ts:647


addTo

addTo(map): this

Attaches the Marker to a Map object.

Parameters

Name Type Description
map Map The MapLibre GL JS map to add the marker to.

Returns

this

this

Example

let marker = new Marker()
  .setLngLat([30.5, 50.5])
  .addTo(map); // add the marker to the map

Defined in

src/ui/marker.ts:310


getElement

getElement(): HTMLElement

Returns the Marker's HTML element.

Returns

HTMLElement

element

Defined in

src/ui/marker.ts:407


getLngLat

getLngLat(): LngLat

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.

Returns

LngLat

A LngLat describing the marker's location.

Example

// 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 )

See

Create a draggable Marker

Defined in

src/ui/marker.ts:377


getOffset

getOffset(): Point

Get the marker's offset.

Returns

Point

The marker's screen coordinates in pixels.

Defined in

src/ui/marker.ts:621


getPitchAlignment

getPitchAlignment(): Alignment

Returns the current pitchAlignment property of the marker.

Returns

Alignment

The current pitch alignment of the marker in degrees.

Defined in

src/ui/marker.ts:829


getPopup

getPopup(): Popup

Returns the Popup instance that is bound to the Marker.

Returns

Popup

popup

Example

let marker = new Marker()
 .setLngLat([0, 0])
 .setPopup(new Popup().setHTML("<h1>Hello World!</h1>"))
 .addTo(map);

console.log(marker.getPopup()); // return the popup instance

Defined in

src/ui/marker.ts:498


getRotation

getRotation(): number

Returns the current rotation angle of the marker (in degrees).

Returns

number

The current rotation angle of the marker.

Defined in

src/ui/marker.ts:791


getRotationAlignment

getRotationAlignment(): Alignment

Returns the current rotationAlignment property of the marker.

Returns

Alignment

The current rotational alignment of the marker.

Defined in

src/ui/marker.ts:810


isDraggable

isDraggable(): boolean

Returns true if the marker can be dragged

Returns

boolean

True if the marker is draggable.

Defined in

src/ui/marker.ts:772


listens

listens(type): boolean

Returns a true if this instance of Evented or any forwardeed instances of Evented have a listener for the specified type.

Parameters

Name Type Description
type string The event type

Returns

boolean

true if there is at least one registered listener for specified event type, false otherwise

Inherited from

Evented.listens

Defined in

src/util/evented.ts:163


off

off(type, listener): Marker

Removes a previously registered event listener.

Parameters

Name Type Description
type string The event type to remove listeners for.
listener Listener The listener function to remove.

Returns

Marker

this

Inherited from

Evented.off

Defined in

src/util/evented.ts:88


on

on(type, listener): this

Adds a listener to a specified event type.

Parameters

Name Type Description
type string The event type to add a listen for.
listener Listener 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.

Returns

this

this

Inherited from

Evented.on

Defined in

src/util/evented.ts:74


once

once(type, listener?): Promise<any> | Marker

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.

Parameters

Name Type Description
type string The event type to listen for.
listener? Listener The function to be called when the event is fired the first time.

Returns

Promise<any> | Marker

this or a promise if a listener is not provided

Inherited from

Evented.once

Defined in

src/util/evented.ts:104


remove

remove(): this

Removes the marker from a map

Returns

this

this

Example

let marker = new Marker().addTo(map);
marker.remove();

Defined in

src/ui/marker.ts:338


removeClassName

removeClassName(className): void

Removes a CSS class from the marker element.

Parameters

Name Type Description
className string Non-empty string with CSS class name to remove from marker element

Returns

void

Example

let marker = new Marker()
marker.removeClassName('some-class')

Defined in

src/ui/marker.ts:662


setDraggable

setDraggable(shouldBeDraggable?): this

Sets the draggable property and functionality of the marker

Parameters

Name Type Description
shouldBeDraggable? boolean Turns drag functionality on/off

Returns

this

this

Defined in

src/ui/marker.ts:750


setEventedParent

setEventedParent(parent?, data?): Marker

Bubble all events fired by this instance of Evented to this parent instance of Evented.

Parameters

Name Type
parent? Evented
data? any

Returns

Marker

this

Inherited from

Evented.setEventedParent

Defined in

src/util/evented.ts:175


setLngLat

setLngLat(lnglat): this

Set the marker's geographical position and move it.

Parameters

Name Type Description
lnglat LngLatLike A LngLat describing where the marker should be located.

Returns

this

this

Example

Create a new marker, set the longitude and latitude, and add it to the map

new Marker()
  .setLngLat([-65.017, -16.457])
  .addTo(map);

See

Defined in

src/ui/marker.ts:395


setOffset

setOffset(offset): this

Sets the offset of the marker

Parameters

Name Type Description
offset PointLike The offset in pixels as a PointLike object to apply relative to the element's center. Negatives indicate left and up.

Returns

this

this

Defined in

src/ui/marker.ts:630


setOpacity

setOpacity(opacity?, opacityWhenCovered?): this

Sets the opacity and opacityWhenCovered properties of the marker. When called without arguments, resets opacity and opacityWhenCovered to defaults

Parameters

Name Type Description
opacity? string Sets the opacity property of the marker.
opacityWhenCovered? string Sets the opacityWhenCovered property of the marker.

Returns

this

this

Defined in

src/ui/marker.ts:840


setPitchAlignment

setPitchAlignment(alignment?): this

Sets the pitchAlignment property of the marker.

Parameters

Name Type Description
alignment? Alignment Sets the pitchAlignment property of the marker. If alignment is 'auto', it will automatically match rotationAlignment.

Returns

this

this

Defined in

src/ui/marker.ts:819


setPopup

setPopup(popup?): this

Binds a Popup to the Marker.

Parameters

Name Type Description
popup? Popup An instance of the Popup class. If undefined or null, any popup set on this Marker instance is unset.

Returns

this

this

Example

let marker = new Marker()
 .setLngLat([0, 0])
 .setPopup(new Popup().setHTML("<h1>Hello World!</h1>")) // add popup
 .addTo(map);

See

Attach a popup to a marker instance

Defined in

src/ui/marker.ts:425


setRotation

setRotation(rotation?): this

Sets the rotation property of the marker.

Parameters

Name Type Description
rotation? number The rotation angle of the marker (clockwise, in degrees), relative to its respective Marker#setRotationAlignment setting.

Returns

this

this

Defined in

src/ui/marker.ts:781


setRotationAlignment

setRotationAlignment(alignment?): this

Sets the rotationAlignment property of the marker.

Parameters

Name Type Description
alignment? Alignment Sets the rotationAlignment property of the marker. defaults to 'auto'

Returns

this

this

Defined in

src/ui/marker.ts:800


toggleClassName

toggleClassName(className): boolean

Add or remove the given CSS class on the marker element, depending on whether the element currently has that class.

Parameters

Name Type Description
className string Non-empty string with CSS class name to add/remove

Returns

boolean

if the class was removed return false, if class was added, then return true

Example

let marker = new Marker()
marker.toggleClassName('toggleClass')

Defined in

src/ui/marker.ts:679


togglePopup

togglePopup(): this

Opens or closes the Popup instance that is bound to the Marker, depending on the current state of the Popup.

Returns

this

this

Example

let marker = new Marker()
 .setLngLat([0, 0])
 .setPopup(new Popup().setHTML("<h1>Hello World!</h1>"))
 .addTo(map);

marker.togglePopup(); // toggle popup open or closed

Defined in

src/ui/marker.ts:515