For AI agents: a documentation index is available at /llms.txt. Markdown versions of all pages are available by appending .md to any URL.

Interface MLMarker

Creates a marker component

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

Set options

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

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

Event drag of type MarkerDragEvent will be fired while dragging.

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

Event click of type MarkerClickEvent will be fired when the marker is clicked.

CSS class maplibregl-marker-covered is toggled on the marker element when the marker is hidden behind 3D terrain or on the back of a globe. Use this class to apply custom styles to covered markers.

.maplibregl-marker-covered {
    pointer-events: none;
    cursor: default;
}
interface MLMarker {
    _addDragHandler: (e: MapMouseEventMLGL | MapTouchEventMLGL) => void;
    _anchor: PositionAnchor;
    _clickTolerance: number;
    _color: string;
    _defaultMarker: boolean;
    _draggable: boolean;
    _element: HTMLElement;
    _eventedParent?: Evented<EventTypeMap>;
    _eventedParentData?: EventedParentData | (() => EventedParentData);
    _flatPos: Point;
    _isDragging: boolean;
    _keyboardDragActive: boolean;
    _listeners?: Listeners<MarkerEventType>;
    _lngLat: LngLat;
    _map: MapMLGL;
    _offset: Point;
    _onBlur: () => void;
    _onClick: (e: MouseEvent) => void;
    _oneTimeListeners?: Listeners<MarkerEventType>;
    _onKeyDown: (e: KeyboardEvent) => void;
    _onKeyPress: (e: KeyboardEvent) => void;
    _onKeyUp: (e: KeyboardEvent) => void;
    _onMapClick: (e: MapMouseEventMLGL) => void;
    _onMove: (e: MapMouseEventMLGL | MapTouchEventMLGL) => void;
    _onUp: () => void;
    _opacity: string;
    _opacityWhenCovered: string;
    _pitchAlignment: Alignment;
    _pointerdownPos: Point;
    _popup: PopupMLGL;
    _pos: Point;
    _positionDelta: Point;
    _roleManaged: boolean;
    _rotation: number;
    _rotationAlignment: Alignment;
    _scale: number;
    _state: "active" | "inactive" | "pending";
    _subpixelPositioning: boolean;
    _tabIndexManaged: boolean;
    _update: (
        e?: {
            type: "idle" | "move" | "moveend" | "terrain" | "projectiontransition";
        },
    ) => void;
    _updateCovered: () => Timeout;
    _updateCoveredUnthrottled: () => void;
    _endKeyboardDrag(): void;
    _isCovered(terrain: Terrain): boolean;
    _isInViewport(): boolean;
    _updateAccessibilityRole(): void;
    _updateOpacity(): void;
    _updateTabIndex(): void;
    addClassName(className: string): void;
    addTo(map: MapMLGL): this;
    fire(event: MarkerDragEvent | MarkerClickEvent): this;
    fire(type: keyof MarkerEventType, properties?: object): this;
    getElement(): HTMLElement;
    getLngLat(): LngLat;
    getOffset(): Point;
    getPitchAlignment(): Alignment;
    getPopup(): PopupMLGL;
    getRotation(): number;
    getRotationAlignment(): Alignment;
    isDraggable(): boolean;
    listens(type: keyof MarkerEventType): boolean;
    off<T extends keyof MarkerEventType>(
        type: T,
        listener: (event: MarkerEventType[T]) => void,
    ): this;
    on<T extends keyof MarkerEventType>(
        type: T,
        listener: (event: MarkerEventType[T]) => void,
    ): Subscription;
    once<T extends keyof MarkerEventType>(type: T): Promise<MarkerEventType[T]>;
    once<T extends keyof MarkerEventType>(
        type: T,
        listener: (event: MarkerEventType[T]) => void,
    ): this;
    remove(): this;
    removeClassName(className: string): void;
    setDraggable(shouldBeDraggable?: boolean): this;
    setEventedParent(
        parent?: Evented<EventTypeMap> | null,
        data?: EventedParentData | (() => EventedParentData),
    ): this;
    setLngLat(lnglat: LngLatLike): this;
    setOffset(offset: PointLike): this;
    setOpacity(
        opacity?: string | number,
        opacityWhenCovered?: string | number,
    ): this;
    setPitchAlignment(alignment?: Alignment): this;
    setPopup(popup?: PopupMLGL | null): this;
    setRotation(rotation?: number): this;
    setRotationAlignment(alignment?: Alignment): this;
    setSubpixelPositioning(value: boolean): this;
    toggleClassName(className: string): boolean;
    togglePopup(): this;
}

Hierarchy (View Summary)

Index

Properties

_addDragHandler: (e: MapMouseEventMLGL | MapTouchEventMLGL) => void
_clickTolerance: number
_color: string
_defaultMarker: boolean
_draggable: boolean
_element: HTMLElement
_eventedParent?: Evented<EventTypeMap>
_eventedParentData?: EventedParentData | (() => EventedParentData)
_flatPos: Point
_isDragging: boolean
_keyboardDragActive: boolean
_lngLat: LngLat
_map: MapMLGL
_offset: Point
_onBlur: () => void
_onClick: (e: MouseEvent) => void
_oneTimeListeners?: Listeners<MarkerEventType>
_onKeyDown: (e: KeyboardEvent) => void

Move a focused draggable default marker with the arrow keys (1 screen pixel per keydown; 10 with Shift). Mirrors the pointer drag gesture: the position updates before dragstart fires on the first movement, every movement fires drag, and releasing the arrow key (or losing focus) fires dragend. Holding a key down produces repeated drag events within a single gesture.

_onKeyPress: (e: KeyboardEvent) => void
_onKeyUp: (e: KeyboardEvent) => void
_onMapClick: (e: MapMouseEventMLGL) => void
_onMove: (e: MapMouseEventMLGL | MapTouchEventMLGL) => void
_onUp: () => void
_opacity: string
_opacityWhenCovered: string
_pitchAlignment: Alignment
_pointerdownPos: Point
_popup: PopupMLGL
_pos: Point
_positionDelta: Point
_roleManaged: boolean
_rotation: number
_rotationAlignment: Alignment
_scale: number
_state: "active" | "inactive" | "pending"
_subpixelPositioning: boolean
_tabIndexManaged: boolean
_update: (
    e?: {
        type: "idle" | "move" | "moveend" | "terrain" | "projectiontransition";
    },
) => void

Positions the marker and updates its opacity. As terrain tiles load, the map can adjust to the ground beneath its center without firing a move event. The idle event triggers a final marker update once the map has finished loading and rendering.

_updateCovered: () => Timeout

The terrain check, at most once per 100 ms while the map moves; the last call in a window runs when the window closes.

_updateCoveredUnthrottled: () => void

Applies opacityWhenCovered and the covered class while the terrain covers the marker, closing its popup; _updateCovered runs it at most once per 100 ms. Nothing to do once the marker, the terrain or the viewport's view of the marker is gone.

Methods

  • Returns void

  • Parameters

    Returns boolean

    Whether the terrain covers the marker's center: the location raised by the height offset lifts the element above it on screen, or the location itself when the offset lowers the element.

  • Returns boolean

    Whether the viewport shows the marker's position. There is nothing to compute for one it does not.

  • Keep the default marker role aligned with interactivity. Default markers need a role because aria-label is set in Marker.addTo. Non-interactive markers use role=img; interactive ones (draggable or with a popup) use role=button. Click listeners are application-owned and do not automatically change the role. Custom marker elements are left alone so applications own their a11y tree. Explicit roles set by the application are preserved.

    Returns void

  • Returns void

  • Keep the marker element focusable while it has built-in keyboard behavior. A popup makes any marker element interactive; dragging only manages focusability for the default marker so custom elements stay application-owned (#7790). A tabindex supplied by the application is never added, changed, or removed here.

    Returns void

  • Adds a CSS class to the marker element.

    Parameters

    • className: string

      on-empty string with CSS class name to add to marker element

    Returns void

    let marker = new Marker()
    marker.addClassName('some-class')
    
  • Attaches the Marker to a Map object.

    Parameters

    • map: MapMLGL

      The MapLibre GL JS map to add the marker to.

    Returns this

    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.

    Returns LngLat

    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 )
    
  • Get the marker's offset.

    Returns Point

    The marker's screen coordinates in pixels.

  • Returns the current pitchAlignment property of the marker.

    Returns Alignment

    The current pitch alignment of the marker in degrees.

  • Returns the Popup instance that is bound to the Marker.

    Returns PopupMLGL

    popup

    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
    
  • Returns the current rotation angle of the marker (in degrees).

    Returns number

    The current rotation angle of the marker.

  • Returns the current rotationAlignment property of the marker.

    Returns Alignment

    The current rotational alignment of the marker.

  • Returns true if the marker can be dragged

    Returns boolean

    True if the marker is draggable.

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

    Parameters

    Returns boolean

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

  • Removes a previously registered event listener.

    Type Parameters

    Parameters

    • type: T

      The event type to remove listeners for.

    • listener: (event: MarkerEventType[T]) => void

      The listener function to remove.

    Returns this

  • Adds a listener to a specified event type.

    Type Parameters

    Parameters

    • type: T

      The event type to add a listen for.

    • listener: (event: MarkerEventType[T]) => void

      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 Subscription

  • 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.

    Type Parameters

    Parameters

    • type: T

      The event type to listen for.

    Returns Promise<MarkerEventType[T]>

    a promise that resolves with the event

  • 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.

    Type Parameters

    Parameters

    • type: T

      The event type to listen for.

    • listener: (event: MarkerEventType[T]) => void

      The function to be called when the event is fired the first time.

    Returns this

    this when a listener is provided

  • Removes the marker from a map

    Returns this

    let marker = new Marker().addTo(map);
    marker.remove();
    
  • Removes a CSS class from the marker element.

    Parameters

    • className: string

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

    Returns void

    let marker = new Marker()
    marker.removeClassName('some-class')
    
  • Sets the draggable property and functionality of the marker. A draggable default marker is also keyboard focusable and movable with the arrow keys (see MarkerOptions.draggable); custom marker elements keep their focusability and keyboard behavior application-owned.

    Parameters

    • OptionalshouldBeDraggable: boolean

      Turns drag functionality on/off

    Returns this

  • Sets the offset of the marker

    Parameters

    • 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

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

    Parameters

    • Optionalopacity: string | number

      Sets the opacity property of the marker.

    • OptionalopacityWhenCovered: string | number

      Sets the opacityWhenCovered property of the marker.

    Returns this

  • Sets the pitchAlignment property of the marker.

    Parameters

    • Optionalalignment: Alignment

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

    Returns this

  • Binds a Popup to the Marker.

    Parameters

    • Optionalpopup: PopupMLGL | null

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

    Returns this

    let 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.

    Parameters

    • Optionalrotation: number

      The rotation angle of the marker (clockwise, in degrees), relative to its respective Marker.setRotationAlignment setting.

    Returns this

  • Sets the rotationAlignment property of the marker.

    Parameters

    • Optionalalignment: Alignment

      Sets the rotationAlignment property of the marker. defaults to 'auto'

    Returns this

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

    Parameters

    • value: boolean

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

    Returns this

    let marker = new Marker()
    marker.setSubpixelPositioning(true);
    
  • Add or remove the given CSS class on the marker element, depending on whether the element currently has that class.

    Parameters

    • 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

    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.

    Returns this

    let marker = new Marker()
     .setLngLat([0, 0])
     .setPopup(new Popup().setHTML("<h1>Hello World!</h1>"))
     .addTo(map);
    
    marker.togglePopup(); // toggle popup open or closed
    
Was this helpful?