diff options
author | Anton Tananaev <anton@traccar.org> | 2022-06-12 06:30:41 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-06-12 06:30:41 -0700 |
commit | 127a8bfc5df8cbe66d5f60023fcf983e15ee13e8 (patch) | |
tree | 4df390424bd2671851fcb69121d4db36be757be9 /modern/src/map/overlay/MapOverlay.js | |
parent | e7995f3b7a6b0828b81bebaf556ef7afb59bef61 (diff) | |
download | trackermap-web-127a8bfc5df8cbe66d5f60023fcf983e15ee13e8.tar.gz trackermap-web-127a8bfc5df8cbe66d5f60023fcf983e15ee13e8.tar.bz2 trackermap-web-127a8bfc5df8cbe66d5f60023fcf983e15ee13e8.zip |
Support map overlays
Diffstat (limited to 'modern/src/map/overlay/MapOverlay.js')
-rw-r--r-- | modern/src/map/overlay/MapOverlay.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/modern/src/map/overlay/MapOverlay.js b/modern/src/map/overlay/MapOverlay.js new file mode 100644 index 00000000..ec51c7c9 --- /dev/null +++ b/modern/src/map/overlay/MapOverlay.js @@ -0,0 +1,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; |