|
| 1 | +# Using Leaflet.markercluster |
| 2 | + |
| 3 | +The guide explains how to use the [Leaflet.markercluster](https://github.com/Leaflet/Leaflet.markercluster) plugin. |
| 4 | +A dedicated composable is available to help you use this plugin. |
| 5 | + |
| 6 | +Options for the markers are the same as the ones available in the [Leaflet documentation](https://leafletjs.com/reference.html#marker). |
| 7 | + |
| 8 | +::: warning |
| 9 | +This is only possible in a client-side environment. You should either : |
| 10 | +- Use a [Client-Only Page](https://nuxt.com/docs/guide/directory-structure/pages#client-only-pages). |
| 11 | +- Wrap your component inside the [ClientOnly](https://nuxt.com/docs/api/components/client-only) component. |
| 12 | +- Set your [rendering strategy](https://nuxt.com/docs/guide/concepts/rendering#client-side-rendering) to `ssr: false` for the appropriate route. |
| 13 | +::: |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +- First install markercluster |
| 18 | + |
| 19 | +```bash |
| 20 | +npm install leaflet.markercluster |
| 21 | +``` |
| 22 | + |
| 23 | +- Update your Nuxt config to activate the plugin |
| 24 | + |
| 25 | +```ts{3-5} |
| 26 | +export default defineNuxtConfig({ |
| 27 | + modules: ['@nuxtjs/leaflet'], |
| 28 | + leaflet: { |
| 29 | + markerCluster: true |
| 30 | + } |
| 31 | +}) |
| 32 | +``` |
| 33 | + |
| 34 | +- Use the `useMarkerCluster` composable in your component |
| 35 | + |
| 36 | +:::warning |
| 37 | +It is very important to keep the manual import of Leaflet and the `:use-global-leaflet="true"` as leaflet.markercluster requires Leaflet to be loaded globally. |
| 38 | +::: |
| 39 | + |
| 40 | +```vue{9,23,28-61,65-68} |
| 41 | +<template> |
| 42 | + <div style="height:100vh; width:100vw"> |
| 43 | + <h1>Marker Cluster</h1> |
| 44 | + <LMap |
| 45 | + ref="map" |
| 46 | + :zoom="6" |
| 47 | + :max-zoom="18" |
| 48 | + :center="[47.21322, -1.559482]" |
| 49 | + :use-global-leaflet="true" |
| 50 | + @ready="onMapReady" |
| 51 | + > |
| 52 | + <LTileLayer |
| 53 | + url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" |
| 54 | + attribution="&copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors" |
| 55 | + layer-type="base" |
| 56 | + name="OpenStreetMap" |
| 57 | + /> |
| 58 | + </LMap> |
| 59 | + </div> |
| 60 | +</template> |
| 61 | +
|
| 62 | +<script setup lang="ts"> |
| 63 | +import L from 'leaflet' |
| 64 | +import { ref } from 'vue'; |
| 65 | +
|
| 66 | +const map = ref(null) as any; |
| 67 | +
|
| 68 | +// Create locations data (20 locations around Nantes) |
| 69 | +const locations = [ |
| 70 | + { name: 'Nantes', lat: 47.218371, lng: -1.553621, options: { |
| 71 | + // Standard Leaflet Marker options |
| 72 | + draggable: true, |
| 73 | + icon: L.icon({ |
| 74 | + iconUrl: '/my-icon.png', |
| 75 | + iconSize: [30, 30], |
| 76 | + }) |
| 77 | + } }, |
| 78 | + { |
| 79 | + // name is optional (no tooltip will be displayed if not provided) |
| 80 | + /* name: 'Saint-Nazaire', */ |
| 81 | + lat: 47.273018, lng: -2.213733 |
| 82 | + }, |
| 83 | + { name: 'La Baule', lat: 47.286835, lng: -2.393108 }, |
| 84 | + { name: 'Pornic', lat: 47.112, lng: -2.102 }, |
| 85 | + { name: 'Guérande', lat: 47.328, lng: -2.429 }, |
| 86 | + { name: 'Clisson', lat: 47.087, lng: -1.276 }, |
| 87 | + { name: 'Ancenis', lat: 47.366, lng: -1.176 }, |
| 88 | + { name: 'Châteaubriant', lat: 47.716, lng: -1.376 }, |
| 89 | + { name: 'Redon', lat: 47.652, lng: -2.084 }, |
| 90 | + { name: 'Pontchâteau', lat: 47.433, lng: -2.117 }, |
| 91 | + { name: 'Savenay', lat: 47.327, lng: -1.952 }, |
| 92 | + { name: 'Rezé', lat: 47.183, lng: -1.55 }, |
| 93 | + { name: 'Vertou', lat: 47.166, lng: -1.466 }, |
| 94 | + { name: 'Carquefou', lat: 47.283, lng: -1.5 }, |
| 95 | + { name: 'Orvault', lat: 47.283, lng: -1.633 }, |
| 96 | + { name: 'Saint-Herblain', lat: 47.216, lng: -1.65 }, |
| 97 | + { name: 'Sainte-Luce-sur-Loire', lat: 47.233, lng: -1.483 }, |
| 98 | + { name: 'Bouguenais', lat: 47.183, lng: -1.583 }, |
| 99 | + { name: 'Saint-Sébastien-sur-Loire', lat: 47.183, lng: -1.483 }, |
| 100 | + { name: 'Basse-Goulaine', lat: 47.2, lng: -1.483 } |
| 101 | +]; |
| 102 | +
|
| 103 | +// When the map is ready |
| 104 | +const onMapReady = () => { |
| 105 | + useMarkerCluster({ |
| 106 | + leafletObject: map.value.leafletObject, |
| 107 | + markers: locations |
| 108 | + }); |
| 109 | +} |
| 110 | +</script> |
| 111 | +``` |
0 commit comments