|
3 | 3 | The guide explains how to use the [Leaflet.markercluster](https://github.com/Leaflet/Leaflet.markercluster) plugin.
|
4 | 4 | A dedicated composable is available to help you use this plugin.
|
5 | 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 | 6 | ::: warning
|
9 | 7 | This is only possible in a client-side environment. You should either :
|
10 | 8 | - Use a [Client-Only Page](https://nuxt.com/docs/guide/directory-structure/pages#client-only-pages).
|
@@ -37,7 +35,7 @@ export default defineNuxtConfig({
|
37 | 35 | 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 | 36 | :::
|
39 | 37 |
|
40 |
| -```vue{9,23,28-61,65-68} |
| 38 | +```vue{9,23,28-50,54-57} |
41 | 39 | <template>
|
42 | 40 | <div style="height:100vh; width:100vw">
|
43 | 41 | <h1>Marker Cluster</h1>
|
@@ -67,19 +65,8 @@ const map = ref(null) as any;
|
67 | 65 |
|
68 | 66 | // Create locations data (20 locations around Nantes)
|
69 | 67 | 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 |
| - }, |
| 68 | + { name: 'Nantes', lat: 47.218371, lng: -1.553621 }, |
| 69 | + { name: 'Saint-Nazaire', lat: 47.273018, lng: -2.213733 }, |
83 | 70 | { name: 'La Baule', lat: 47.286835, lng: -2.393108 },
|
84 | 71 | { name: 'Pornic', lat: 47.112, lng: -2.102 },
|
85 | 72 | { name: 'Guérande', lat: 47.328, lng: -2.429 },
|
@@ -109,3 +96,98 @@ const onMapReady = () => {
|
109 | 96 | }
|
110 | 97 | </script>
|
111 | 98 | ```
|
| 99 | + |
| 100 | +## Options |
| 101 | + |
| 102 | +Here are the options available for each marker in the `markers` prop. |
| 103 | + |
| 104 | +| Event name | Type | Description | |
| 105 | +| ---------- | ------ | ------------------------------------------------------------------------------------------------------------------ | |
| 106 | +| name | string | **(optional)** Name of the location, will be displayed in a tooltip. If not provided, no tooltip will be displayed | |
| 107 | +| lat | number | Latitude of the location | |
| 108 | +| lng | number | Longitude of the location | |
| 109 | +| options | object | **(optional)** Standard [Leaflet Marker options](https://leafletjs.com/reference.html#marker) | |
| 110 | +| popup | string | **(optional)** If provided, it is considered as an HTML string and will be displayed as a popup on the marker | |
| 111 | + |
| 112 | +## Recipes |
| 113 | + |
| 114 | +### Accessing the cluster group |
| 115 | + |
| 116 | +You can access the cluster group created by `useLMarkerCluster` to call any method from the [leaflet.markercluster API](https://github.com/Leaflet/Leaflet.markercluster?tab=readme-ov-file#events). |
| 117 | + |
| 118 | +```ts |
| 119 | +const locations = [{ |
| 120 | + name: 'Nantes', |
| 121 | + lat: 47.218371, |
| 122 | + lng: -1.553621 |
| 123 | +}]; |
| 124 | +const { markerCluster } = useLMarkerCluster({ |
| 125 | + leafletObject: map.value.leafletObject, |
| 126 | + markers: locations |
| 127 | +}); |
| 128 | +markerCluster.on('clusterclick', (event) => { |
| 129 | + console.log('Cluster clicked'); |
| 130 | +}); |
| 131 | +``` |
| 132 | + |
| 133 | +### Customizing the markers |
| 134 | + |
| 135 | +You can use a customize the markers by providing the `icon` option in the location object. |
| 136 | + |
| 137 | +```ts |
| 138 | +const locations = [{ |
| 139 | + name: 'Nantes', |
| 140 | + lat: 47.218371, |
| 141 | + lng: -1.553621, |
| 142 | + options: { |
| 143 | + draggable: true, |
| 144 | + icon: L.icon({ |
| 145 | + iconUrl: '/my-icon.png', |
| 146 | + iconSize: [30, 30], |
| 147 | + }) |
| 148 | + } |
| 149 | +}]; |
| 150 | + |
| 151 | +useLMarkerCluster({ |
| 152 | + leafletObject: map.value.leafletObject, |
| 153 | + markers: locations |
| 154 | +}); |
| 155 | +``` |
| 156 | + |
| 157 | +### Displaying a popup on a marker |
| 158 | + |
| 159 | +You can add a popup to a marker by providing the `popup` option in the location object. |
| 160 | + |
| 161 | +```ts |
| 162 | +const locations = [{ |
| 163 | + name: 'Nantes', |
| 164 | + lat: 47.218371, |
| 165 | + lng: -1.553621, |
| 166 | + popup: '<h1>This is Nantes</h1>' |
| 167 | +}]; |
| 168 | + |
| 169 | +useLMarkerCluster({ |
| 170 | + leafletObject: map.value.leafletObject, |
| 171 | + markers: locations |
| 172 | +}); |
| 173 | +``` |
| 174 | + |
| 175 | +### Calling legacy methods from Leaflet |
| 176 | + |
| 177 | +You can access the markers created by `useLMarkerCluster`, and call any method from the [Leaflet Marker API](https://leafletjs.com/reference.html#marker). |
| 178 | + |
| 179 | +```ts |
| 180 | +const locations = [{ |
| 181 | + name: 'Nantes', |
| 182 | + lat: 47.218371, |
| 183 | + lng: -1.553621 |
| 184 | +}]; |
| 185 | + |
| 186 | +const { markers } = useLMarkerCluster({ |
| 187 | + leafletObject: map.value.leafletObject, |
| 188 | + markers: locations |
| 189 | +}); |
| 190 | +markers.forEach(marker => { |
| 191 | + marker.bindPopup(L.popup().setContent('</h1>Hello marker</h1>')); |
| 192 | +}); |
| 193 | +``` |
0 commit comments