VMarker
Add interactive markers to your map
Usage
<script setup lang="ts">
import { VMap, VMarker } from '@geoql/v-maplibre';
const mapOptions = {
style: 'https://demotiles.maplibre.org/style.json',
center: [-74.5, 40],
zoom: 9,
};
const markerPosition = { lng: -74.5, lat: 40 };
</script>
<template>
<VMap :options="mapOptions" style="height: 500px">
<VMarker :coordinates="markerPosition"></VMarker>
</VMap>
</template>
Props
coordinates
- Type:
LngLatLike - Required:
true
The geographic location of the marker. Can be:
- Object:
{ lng: number, lat: number } - Array:
[lng, lat] - LngLat instance
<!-- Object notation -->
<VMarker :coordinates="{ lng: -74.5, lat: 40 }"></VMarker>
<!-- Array notation -->
<VMarker :coordinates="[-74.5, 40]"></VMarker>
draggable
- Type:
boolean - Required:
false - Default:
false
Make the marker draggable. Changing it updates the live marker.
<VMarker :coordinates="position" draggable @dragend="handleDragEnd"></VMarker>
anchor
- Type:
PositionAnchor('center' | 'top' | 'bottom' | 'left' | 'right' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right') - Required:
false
The part of the marker that sits on coordinates.
offset
- Type:
PointLike - Required:
false
Pixel offset from coordinates. Changing it updates the live marker.
color
- Type:
string - Required:
false
Color of the default marker. It has no effect when you pass a custom element.
scale
- Type:
number - Required:
false
Scale of the default marker. It has no effect when you pass a custom element.
draggable, anchor, offset, color and scale are shorthands for the same keys in options. When both are set, the top-level prop wins.
<VMarker
:coordinates="position"
anchor="bottom"
:offset="[0, -8]"
color="#e11d48"
:scale="1.25"
></VMarker>
options
- Type:
MarkerOptions - Required:
false
Additional MapLibre GL marker options. See MapLibre GL Marker for all available options.
Common options:
color: Marker colorrotation: Marker rotation in degreesrotationAlignment: How marker rotates with mappitchAlignment: How marker aligns with map pitchoffset: Pixel offset from the lngLat position
<VMarker
:coordinates="position"
:options="{
color: '#FF0000',
rotation: 45,
offset: [0, -10],
}"
></VMarker>
Events
@click
Emitted when the marker is clicked.
<VMarker :coordinates="position" @click="handleMarkerClick"></VMarker>
@dragstart
Emitted when marker dragging starts.
<VMarker :coordinates="position" draggable @dragstart="handleDragStart"></VMarker>
@drag
Emitted continuously while dragging.
<VMarker :coordinates="position" draggable @drag="handleDrag"></VMarker>
@dragend
Emitted when marker dragging ends.
<VMarker :coordinates="position" draggable @dragend="handleDragEnd"></VMarker>
Slots
markers
Replaces the default pin with your own element. The slot passes setRef: bind it to the root element with :ref. VMarker then builds the MapLibre marker from that element.
<VMarker :coordinates="position">
<template #markers="{ setRef }">
<div :ref="setRef" class="custom-marker">
<img src="/marker-icon.png" alt="Custom marker" />
</div>
</template>
</VMarker>
Without :ref="setRef" no marker is created.
default
The content of a popup that opens when the marker is clicked. Use popupOptions to configure the popup.
<VMarker :coordinates="position" :popup-options="{ offset: 25 }">
<strong>Office</strong>
<p>Open 9:00 to 18:00</p>
</VMarker>
You can use both slots on one marker: #markers for the pin and the default slot for its popup.
Examples
Custom Color
<VMarker :coordinates="[-74.5, 40]" :options="{ color: '#FF5733' }"></VMarker>
Draggable Marker
<script setup lang="ts">
import { ref } from 'vue';
import { VMap, VMarker } from '@geoql/v-maplibre';
const position = ref({ lng: -74.5, lat: 40 });
const handleDragEnd = (event: any) => {
const lngLat = event.target.getLngLat();
position.value = { lng: lngLat.lng, lat: lngLat.lat };
console.log('New position:', position.value);
};
</script>
<template>
<VMap :options="mapOptions" style="height: 500px">
<VMarker :coordinates="position" draggable @dragend="handleDragEnd"></VMarker>
</VMap>
</template>
Custom Marker with Slot
<script setup lang="ts">
import { VMap, VMarker } from '@geoql/v-maplibre';
const position = { lng: -74.5, lat: 40 };
</script>
<template>
<VMap :options="mapOptions" style="height: 500px">
<VMarker :coordinates="position">
<template #markers="{ setRef }">
<div :ref="setRef" class="custom-marker">
<div class="marker-pin"></div>
<div class="marker-label">Custom Pin</div>
</div>
</template>
</VMarker>
</VMap>
</template>
<style scoped>
.custom-marker {
position: relative;
text-align: center;
}
.marker-pin {
width: 30px;
height: 30px;
border-radius: 50% 50% 50% 0;
background: #00cae9;
transform: rotate(-45deg);
margin: 0 auto;
}
.marker-label {
position: absolute;
top: 35px;
left: 50%;
transform: translateX(-50%);
background: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
white-space: nowrap;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
</style>
Multiple Markers
<script setup lang="ts">
import { VMap, VMarker } from '@geoql/v-maplibre';
const locations = [
{ id: 1, lng: -74.5, lat: 40, name: 'Location 1', color: '#FF5733' },
{ id: 2, lng: -74.3, lat: 40.1, name: 'Location 2', color: '#33FF57' },
{ id: 3, lng: -74.4, lat: 39.9, name: 'Location 3', color: '#3357FF' },
];
</script>
<template>
<VMap :options="mapOptions" style="height: 500px">
<VMarker
v-for="location in locations"
:key="location.id"
:coordinates="[location.lng, location.lat]"
:options="{ color: location.color }"
@click="() => console.log('Clicked:', location.name)"
></VMarker>
</VMap>
</template>