<MapboxMarker>

Gitlab
A Vue component for adding markers to a Mapbox map with customizable rendering and behavior.
Added since: v0.1.0
Last changed:

Usage

MapboxMarker is a Vue component that provides a convenient way to add markers to a Mapbox map. It supports custom rendering through slots, dynamic element types, and automatic positioning based on GeoJSON features.

The component can be used to add markers to your map with custom content and behavior.

Basic example

MapComponent.vue
<script setup lang="ts">
import type { Feature, Point } from 'geojson';

const markerFeature: Feature<Point> = {
  type: 'Feature',
  geometry: {
    type: 'Point',
    coordinates: [-74.5, 40]
  },
  properties: {}
};
</script>

<template>
  <MapboxMarker
    id="my-marker"
    :feature="markerFeature"
    as="button"
    @click="handleMarkerClick"
  >
    <div class="custom-marker">
      <span>πŸ“</span>
    </div>
  </MapboxMarker>
</template>

Type Definition

type 
MapboxMarkerProps
= {
/** * Unique identifier for the marker */
id
: string;
/** * GeoJSON feature containing marker coordinates * @see {@link PointFeature|PointFeature} */
feature
:
PointFeature
<any>;
/** * Mapbox marker options * @defaultValue { anchor: "center" } * @see {@link mapboxgl.MarkerOptions|MarkerOptions} * @see {@link https://docs.mapbox.com/mapbox-gl-js/api/markers/#marker-parameters|Mapbox GL Docs - Marker parameters} */
options
?:
MarkerOptions
;
/** * The destination to link to * * When the `to` prop is provided, the component will render as a `NuxtLink` component. * * _(The `as` prop will override this behavior when provided.)_ */
to
?: string;
/** * The element or component this component should render as. * * Will prioritize components over elements when the name for both is the same. * @defaultValue "div" * @see {@link AsTag|AsTag} * @see {@link Component|Component} * @example * ```vue * <template> * <MapboxMarker as="span" /> * </template> * ``` */
as
?:
AsTag
|
Component
;
} &
Pick
<
PrimitiveProps
, "asChild">;
type
MapboxMarkerContext
= {
/** * The Marker instance * @see {@link mapboxgl.Marker|Marker} * @see {@link https://docs.mapbox.com/mapbox-gl-js/api/markers/#marker|Mapbox GL Docs - Marker} */
marker
:
Ref
<
Marker
| null | undefined>;
};

API Reference

Props

PropDefaultType
id*
string

Unique identifier for the marker.

options
{ anchor: 'center' }MarkerOptions

Mapbox marker options.

feature
Feature<Point, any>

GeoJSON feature containing marker coordinates.

as
divstring

The element or component this component should render as.
Will prioritize components over elements when the name for both is the same.

to
string

The destination to link to.
When the to prop is provided, the component will render as a NuxtLink component.

(The as prop will override this behavior when provided.)

Slots

SlotPayload
default

Content to render inside the marker element

Attributes

AttributeValueType
data-typemapbox-markerstring

Exposed

ExposedType
markerMarker
markerElementComponentPublicInstance

The marker element instance

Context

The component provides a context that can be injected by child components to access the marker instance.

ContextType
markerRef<Marker>

The marker instance

CustomMarker.vue
<script setup lang="ts">
const { marker } = injectMapboxMarkerContext();
</script>

<template>
  <div>{{ marker }}</div>
</template>

Changelog

v0.2.3

on

#ebea626c

-

improvement: added full typescript support

v0.2.2

on

#56fc08c0

-

fix: resolved marker position flash on rendering

#93b26e58

-

improvement: updated src structure to match latest internal conventions

v0.1.1

on

#d40a96ab

-

chore: working module/ release pipeline