Skip to content

Both copy text to your clipboard — Build with AI copies a setup prompt to paste into Claude Code, Cursor, Codex or Copilot; Copy page as Markdown copies this page to paste into a chat. How it works

Marker ​

Defined in: src/ui/marker.ts:127

Creates a marker component

Examples ​

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

Set options

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

See ​

Events ​

Event dragstart of type Event will be fired when dragging starts.

Event drag of type Event will be fired while dragging.

Event dragend of type Event will be fired when the marker is finished being dragged.

Extends ​

Constructors ​

Constructor ​

new Marker(options?: MarkerOptions): Marker

Defined in: src/ui/marker.ts:157

Parameters ​

ParameterTypeDescription
options?MarkerOptionsthe options

Returns ​

Marker

Overrides ​

Evented.constructor

Methods ​

addClassName() ​

addClassName(className: string): void

Defined in: src/ui/marker.ts:668

Adds a CSS class to the marker element.

Parameters ​

ParameterTypeDescription
classNamestringon-empty string with CSS class name to add to marker element

Returns ​

void

Example ​

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

addTo() ​

addTo(map: Map): this

Defined in: src/ui/marker.ts:315

Attaches the Marker to a Map object.

Parameters ​

ParameterTypeDescription
mapMapThe mapmetrics GL JS map to add the marker to.

Returns ​

this

Example ​

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

getElement() ​

getElement(): HTMLElement

Defined in: src/ui/marker.ts:418

Returns the Marker's HTML element.

Returns ​

HTMLElement

element


getLngLat() ​

getLngLat(): LngLat

Defined in: src/ui/marker.ts:389

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 ​

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


getOffset() ​

getOffset(): Point

Defined in: src/ui/marker.ts:643

Get the marker's offset.

Returns ​

Point

The marker's screen coordinates in pixels.


getPitchAlignment() ​

getPitchAlignment(): Alignment

Defined in: src/ui/marker.ts:846

Returns the current pitchAlignment property of the marker.

Returns ​

Alignment

The current pitch alignment of the marker in degrees.


getPopup() ​

getPopup(): Popup

Defined in: src/ui/marker.ts:524

Returns the Popup instance that is bound to the Marker.

Returns ​

Popup

popup

Example ​

ts
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

getRotation() ​

getRotation(): number

Defined in: src/ui/marker.ts:810

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

Returns ​

number

The current rotation angle of the marker.


getRotationAlignment() ​

getRotationAlignment(): Alignment

Defined in: src/ui/marker.ts:828

Returns the current rotationAlignment property of the marker.

Returns ​

Alignment

The current rotational alignment of the marker.


isDraggable() ​

isDraggable(): boolean

Defined in: src/ui/marker.ts:792

Returns true if the marker can be dragged

Returns ​

boolean

True if the marker is draggable.


listens() ​

listens(type: string): boolean

Defined in: src/util/evented.ts:165

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

Parameters ​

ParameterTypeDescription
typestringThe event type

Returns ​

boolean

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

Inherited from ​

Evented.listens


off() ​

off(type: string, listener: Listener): Marker

Defined in: src/util/evented.ts:90

Removes a previously registered event listener.

Parameters ​

ParameterTypeDescription
typestringThe event type to remove listeners for.
listenerListenerThe listener function to remove.

Returns ​

Marker

Inherited from ​

Evented.off


on() ​

on(type: string, listener: Listener): Subscription

Defined in: src/util/evented.ts:73

Adds a listener to a specified event type.

Parameters ​

ParameterTypeDescription
typestringThe event type to add a listen for.
listenerListenerThe 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 ​

Subscription

Inherited from ​

Evented.on


once() ​

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

Defined in: src/util/evented.ts:106

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 ​

ParameterTypeDescription
typestringThe event type to listen for.
listener?ListenerThe 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


remove() ​

remove(): this

Defined in: src/ui/marker.ts:348

Removes the marker from a map

Returns ​

this

Example ​

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

removeClassName() ​

removeClassName(className: string): void

Defined in: src/ui/marker.ts:683

Removes a CSS class from the marker element.

Parameters ​

ParameterTypeDescription
classNamestringNon-empty string with CSS class name to remove from marker element

Returns ​

void

Example ​

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

setDraggable() ​

setDraggable(shouldBeDraggable?: boolean): this

Defined in: src/ui/marker.ts:770

Sets the draggable property and functionality of the marker

Parameters ​

ParameterTypeDescription
shouldBeDraggable?booleanTurns drag functionality on/off

Returns ​

this


setEventedParent() ​

setEventedParent(parent?: Evented, data?: any): Marker

Defined in: src/util/evented.ts:176

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

Parameters ​

ParameterType
parent?Evented
data?any

Returns ​

Marker

Inherited from ​

Evented.setEventedParent


setLngLat() ​

setLngLat(lnglat: LngLatLike): this

Defined in: src/ui/marker.ts:406

Set the marker's geographical position and move it.

Parameters ​

ParameterTypeDescription
lnglatLngLatLikeA LngLat describing where the marker should be located.

Returns ​

this

Example ​

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

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

See ​


setOffset() ​

setOffset(offset: PointLike): this

Defined in: src/ui/marker.ts:651

Sets the offset of the marker

Parameters ​

ParameterTypeDescription
offsetPointLikeThe offset in pixels as a PointLike object to apply relative to the element's center. Negatives indicate left and up.

Returns ​

this


setOpacity() ​

setOpacity(opacity?: string, opacityWhenCovered?: string): this

Defined in: src/ui/marker.ts:856

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

Parameters ​

ParameterTypeDescription
opacity?stringSets the opacity property of the marker.
opacityWhenCovered?stringSets the opacityWhenCovered property of the marker.

Returns ​

this


setPitchAlignment() ​

setPitchAlignment(alignment?: Alignment): this

Defined in: src/ui/marker.ts:836

Sets the pitchAlignment property of the marker.

Parameters ​

ParameterTypeDescription
alignment?AlignmentSets the pitchAlignment property of the marker. If alignment is 'auto', it will automatically match rotationAlignment.

Returns ​

this


setPopup() ​

setPopup(popup?: Popup): this

Defined in: src/ui/marker.ts:435

Binds a Popup to the Marker.

Parameters ​

ParameterTypeDescription
popup?PopupAn instance of the Popup class. If undefined or null, any popup set on this Marker instance is unset.

Returns ​

this

Example ​

ts
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


setRotation() ​

setRotation(rotation?: number): this

Defined in: src/ui/marker.ts:800

Sets the rotation property of the marker.

Parameters ​

ParameterTypeDescription
rotation?numberThe rotation angle of the marker (clockwise, in degrees), relative to its respective Marker#setRotationAlignment setting.

Returns ​

this


setRotationAlignment() ​

setRotationAlignment(alignment?: Alignment): this

Defined in: src/ui/marker.ts:818

Sets the rotationAlignment property of the marker.

Parameters ​

ParameterTypeDescription
alignment?AlignmentSets the rotationAlignment property of the marker. defaults to 'auto'

Returns ​

this


setSubpixelPositioning() ​

setSubpixelPositioning(value: boolean): Marker

Defined in: src/ui/marker.ts:485

Set the option to allow subpixel positioning of the marker by passing a boolean

Parameters ​

ParameterTypeDescription
valuebooleanwhen set to true, subpixel positioning is enabled for the marker.

Returns ​

Marker

Example ​

ts
let marker = new Marker()
marker.setSubpixelPositioning(true);

toggleClassName() ​

toggleClassName(className: string): boolean

Defined in: src/ui/marker.ts:700

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

Parameters ​

ParameterTypeDescription
classNamestringNon-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 ​

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

togglePopup() ​

togglePopup(): this

Defined in: src/ui/marker.ts:540

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

Returns ​

this

Example ​

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

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