25 lines
656 B
JavaScript
25 lines
656 B
JavaScript
// hooks/useLayerVisibility.js
|
|
import { useEffect } from "react";
|
|
import { useRecoilValue } from "recoil";
|
|
import { mapLayersState } from "../store/atoms/mapLayersState";
|
|
|
|
const useLayerVisibility = (map, markers, mapLayersVisibility, layerKey) => {
|
|
useEffect(() => {
|
|
if (!map || !markers) return;
|
|
|
|
const toggleLayer = (isVisible) => {
|
|
markers.forEach((marker) => {
|
|
if (isVisible) {
|
|
marker.addTo(map);
|
|
} else {
|
|
map.removeLayer(marker);
|
|
}
|
|
});
|
|
};
|
|
|
|
toggleLayer(mapLayersVisibility[layerKey]);
|
|
}, [map, markers, mapLayersVisibility, layerKey]);
|
|
};
|
|
|
|
export default useLayerVisibility;
|