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.
Key features:
The component can be used to add markers to your map with custom content and behavior.
<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>
| Prop | Default | Type |
|---|---|---|
id* | stringUnique identifier for the marker | |
options | {anchor: 'center'} | MarkerOptionsMapbox marker options |
feature | Feature<Point, any>GeoJSON feature containing marker coordinates | |
as | stringHTML element type to render (div, button, etc.) | |
to | stringIf provided, renders as NuxtLink with this destination |
| Slot | Payload |
|---|---|
default | undefinedContent to render inside the marker element |
| Exposed | Type |
|---|---|
marker | MarkerThe marker instance |
The component provides a context that can be injected by child components to access the marker instance.
export type MapboxMarkerContext = {
marker: Ref<Marker | null>;
};
export const [injectMapboxMarkerContext, provideMapboxMarkerContext]
= createContext<MapboxMarkerContext>("MapboxMarker");
<script setup lang="ts">
import type { Feature, Point } from 'geojson';
const markerFeature: Feature<Point> = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-74.5, 40]
},
properties: {
title: 'New York'
}
};
</script>
<template>
<MapboxMarker
id="custom-marker"
:feature="markerFeature"
:options="{ anchor: 'bottom' }"
as="button"
@click="handleClick"
>
<div class="marker-content">
<h3>{{ markerFeature.properties.title }}</h3>
<p>Click to view details</p>
</div>
</MapboxMarker>
</template>
<style scoped>
.marker-content {
background: white;
padding: 8px;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
</style>
<script setup lang="ts">
const markerFeature = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-74.5, 40]
},
properties: {
id: 'location-1'
}
};
</script>
<template>
<MapboxMarker
id="link-marker"
:feature="markerFeature"
to="/locations/1"
>
<div class="link-marker">
View Location
</div>
</MapboxMarker>
</template>
as prop to specify the appropriate element type based on your needs:
button for interactive markersdiv for static markersto prop for navigation markers