parent
aec4d090f3
commit
04e340ad64
@ -1,5 +1,5 @@ |
||||
import leaflet from 'leaflet'; |
||||
import { Marker } from './types'; |
||||
import { Marker } from '../../types'; |
||||
|
||||
export const getLayersForMarkers = (markers: Marker[]) => [ |
||||
// lines
|
@ -0,0 +1,29 @@ |
||||
import leaflet from 'leaflet'; |
||||
import { Ref, useEffect } from 'preact/hooks'; |
||||
import { getLayersForMarkers } from './getLayersForMarkers'; |
||||
import { Marker } from '../../types'; |
||||
|
||||
export const useMarkersOnMap = ( |
||||
mapRef: Readonly<Ref<leaflet.Map | undefined>>, |
||||
markers: Marker[], |
||||
) => { |
||||
useEffect(() => { |
||||
const map = mapRef.current; |
||||
if (!map) { |
||||
return; |
||||
} |
||||
|
||||
const layers = getLayersForMarkers(markers); |
||||
|
||||
for (const layer of layers) { |
||||
layer.addTo(map); |
||||
} |
||||
|
||||
return () => { |
||||
for (const layer of layers) { |
||||
layer.removeFrom(map); |
||||
layer.remove(); |
||||
} |
||||
}; |
||||
}, [mapRef, markers]); |
||||
}; |
Loading…
Reference in new issue