|
| 1 | +# Using Leaflet.heat |
| 2 | + |
| 3 | +The guide explains how to use the [Leaflet.heat](https://github.com/Leaflet/Leaflet.heat) plugin. |
| 4 | +A dedicated composable is available to help you use this plugin. |
| 5 | + |
| 6 | +::: warning |
| 7 | +This is only possible in a client-side environment. You should either : |
| 8 | +- Use a [Client-Only Page](https://nuxt.com/docs/guide/directory-structure/pages#client-only-pages). |
| 9 | +- Wrap your component inside the [ClientOnly](https://nuxt.com/docs/api/components/client-only) component. |
| 10 | +- Set your [rendering strategy](https://nuxt.com/docs/guide/concepts/rendering#client-side-rendering) to `ssr: false` for the appropriate route. |
| 11 | +::: |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +- First install `leaflet.heat` |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install leaflet.heat |
| 19 | +``` |
| 20 | + |
| 21 | +- Update your Nuxt config to activate the plugin |
| 22 | + |
| 23 | +```ts{3-5} |
| 24 | +export default defineNuxtConfig({ |
| 25 | + modules: ['@nuxtjs/leaflet'], |
| 26 | + leaflet: { |
| 27 | + heat: true |
| 28 | + } |
| 29 | +}) |
| 30 | +``` |
| 31 | + |
| 32 | +- Use the `useLHeat` composable in your component |
| 33 | + |
| 34 | +:::warning |
| 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. |
| 36 | +::: |
| 37 | + |
| 38 | +```vue{9,23,29-46,50-55} |
| 39 | +<template> |
| 40 | + <div style="height:100vh; width:100vw"> |
| 41 | + <h1>Heat</h1> |
| 42 | + <LMap |
| 43 | + ref="map" |
| 44 | + :zoom="17" |
| 45 | + :max-zoom="22" |
| 46 | + :center="[47.21322, -1.559482]" |
| 47 | + :use-global-leaflet="true" |
| 48 | + @ready="onMapReady" |
| 49 | + > |
| 50 | + <LTileLayer |
| 51 | + url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" |
| 52 | + attribution="&copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors" |
| 53 | + layer-type="base" |
| 54 | + name="OpenStreetMap" |
| 55 | + /> |
| 56 | + </LMap> |
| 57 | + </div> |
| 58 | +</template> |
| 59 | +
|
| 60 | +<script setup lang="ts"> |
| 61 | +import L from 'leaflet'; |
| 62 | +import { ref } from 'vue'; |
| 63 | +
|
| 64 | +const isDrawing = ref(false); |
| 65 | +const map = ref(null) as any; |
| 66 | +
|
| 67 | +// Create heat data |
| 68 | +const heatPoints = [{ |
| 69 | + lat: 47.21322, |
| 70 | + lng: -1.559482, |
| 71 | + intensity: 100.0 |
| 72 | +}, { |
| 73 | + lat: 47.21322, |
| 74 | + lng: -1.558482, |
| 75 | + intensity: 50.0 |
| 76 | +}, { |
| 77 | + lat: 47.21322, |
| 78 | + lng: -1.557482, |
| 79 | + intensity: 25.0 |
| 80 | +}, { |
| 81 | + lat: 47.21322, |
| 82 | + lng: -1.556482, |
| 83 | + intensity: 10.0 |
| 84 | +}]; |
| 85 | +
|
| 86 | +// When the map is ready |
| 87 | +const onMapReady = async () => { |
| 88 | + const heat = await useLHeat({ |
| 89 | + leafletObject: map.value.leafletObject, |
| 90 | + heatPoints: heatPoints, |
| 91 | + // (optional) radius : default 50 |
| 92 | + radius: 50, |
| 93 | + }); |
| 94 | +
|
| 95 | + // (optional) Make the heat layer drawable |
| 96 | + map.value.leafletObject.on({ |
| 97 | + movestart: function () { isDrawing.value = false; }, |
| 98 | + moveend: function () { isDrawing.value = true; }, |
| 99 | + mousemove: function (e: any) { |
| 100 | + if (isDrawing.value) { |
| 101 | + heat.addLatLng(e.latlng); |
| 102 | + } |
| 103 | + } |
| 104 | + }) |
| 105 | +} |
| 106 | +</script> |
| 107 | +``` |
0 commit comments