marker-cluster.svelte 870 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <script lang="ts" context="module">
  2. import { createContext } from '$lib/utils/context';
  3. const { get: getContext, set: setClusterContext } = createContext<() => L.MarkerClusterGroup>();
  4. export const getClusterContext = () => {
  5. return getContext()();
  6. };
  7. </script>
  8. <script lang="ts">
  9. import { onDestroy, onMount } from 'svelte';
  10. import 'leaflet.markercluster';
  11. import { getMapContext } from './map.svelte';
  12. const map = getMapContext();
  13. let cluster: L.MarkerClusterGroup;
  14. setClusterContext(() => cluster);
  15. onMount(() => {
  16. cluster = new L.MarkerClusterGroup({
  17. showCoverageOnHover: false,
  18. zoomToBoundsOnClick: true,
  19. spiderfyOnMaxZoom: true,
  20. maxClusterRadius: 30,
  21. spiderLegPolylineOptions: { opacity: 0 }
  22. });
  23. map.addLayer(cluster);
  24. });
  25. onDestroy(() => {
  26. if (cluster) cluster.remove();
  27. });
  28. </script>
  29. {#if cluster}
  30. <slot />
  31. {/if}