2022-01-15 04:27:29 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import type mapboxgl from "mapbox-gl";
|
2022-01-15 03:46:39 +00:00
|
|
|
|
|
|
|
import styles from "../styles/ToggleLayersControl.module.css";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
map: React.MutableRefObject<mapboxgl.Map>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ToggleLayersControl: React.FC<Props> = ({ map }) => {
|
|
|
|
const [heatmapVisible, setHeatmapVisible] = useState(true);
|
|
|
|
const [gridVisible, setGridVisible] = useState(true);
|
|
|
|
const [labelsVisible, setLabelsVisible] = useState(true);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (map.current && map.current.isStyleLoaded()) {
|
|
|
|
map.current.setLayoutProperty(
|
2022-01-17 22:10:59 +00:00
|
|
|
"heatmap-layer",
|
2022-01-15 03:46:39 +00:00
|
|
|
"visibility",
|
|
|
|
heatmapVisible ? "visible" : "none"
|
|
|
|
);
|
|
|
|
}
|
2022-01-15 04:27:29 +00:00
|
|
|
}, [map, heatmapVisible]);
|
2022-01-15 03:46:39 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (map.current && map.current.isStyleLoaded()) {
|
|
|
|
map.current.setLayoutProperty(
|
|
|
|
"graticule",
|
|
|
|
"visibility",
|
|
|
|
gridVisible ? "visible" : "none"
|
|
|
|
);
|
|
|
|
}
|
2022-01-15 04:27:29 +00:00
|
|
|
}, [map, gridVisible]);
|
2022-01-15 03:46:39 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (map.current && map.current.isStyleLoaded()) {
|
|
|
|
map.current.setLayoutProperty(
|
|
|
|
"grid-labels-layer",
|
|
|
|
"visibility",
|
|
|
|
labelsVisible ? "visible" : "none"
|
|
|
|
);
|
|
|
|
}
|
2022-01-15 04:27:29 +00:00
|
|
|
}, [map, labelsVisible]);
|
2022-01-15 03:46:39 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="mapboxgl-ctrl-top-left">
|
|
|
|
<div className="mapboxgl-ctrl mapboxgl-ctrl-group">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => setHeatmapVisible(!heatmapVisible)}
|
|
|
|
className={`${styles["heatmap-toggle"]} ${
|
|
|
|
!heatmapVisible ? styles["toggle-off"] : ""
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<span className="mapboxgl-ctrl-icon" />
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => setGridVisible(!gridVisible)}
|
|
|
|
className={`${styles["grid-toggle"]} ${
|
|
|
|
!gridVisible ? styles["toggle-off"] : ""
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<span className="mapboxgl-ctrl-icon" />
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => setLabelsVisible(!labelsVisible)}
|
|
|
|
className={`${styles["labels-toggle"]} ${
|
|
|
|
!labelsVisible ? styles["toggle-off"] : ""
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<span className="mapboxgl-ctrl-icon" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ToggleLayersControl;
|