aboutsummaryrefslogtreecommitdiff
path: root/modern/src/map/overlay/MapOverlay.js
blob: ec51c7c919aec83d32b8ed6a1d90e5c97d28a05a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { useEffect } from 'react';
import usePersistedState from '../../common/util/usePersistedState';
import { map } from '../core/MapView';
import useMapOverlays from './useMapOverlays';

const MapOverlay = () => {
  const id = 'overlay';

  const mapOverlays = useMapOverlays();
  const [selectedMapOverlay] = usePersistedState('selectedMapOverlay');

  const activeOverlay = mapOverlays.filter((overlay) => overlay.available).find((overlay) => overlay.id === selectedMapOverlay);

  useEffect(() => {
    if (activeOverlay) {
      map.addSource(id, activeOverlay.source);
      map.addLayer({
        id,
        type: 'raster',
        source: id,
        layout: {
          visibility: 'visible',
        },
      });
    }
    return () => {
      if (map.getLayer(id)) {
        map.removeLayer(id);
      }
      if (map.getSource(id)) {
        map.removeSource(id);
      }
    };
  });

  return null;
};

export default MapOverlay;