Skip to content

Commit f97b8a0

Browse files
committed
Add only basic styles on initialization and add baselayer on on.load event which allows to load all base layers and toggle switch using its visibility property
It helps to avoid the re-adding map style entirely so it prevents all custom layers, sources, controls, and event bindings lost
1 parent 88e6a0b commit f97b8a0

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

frontend/src/components/projectCreate/projectCreationMap.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import { useDropzone } from 'react-dropzone';
1111
import { maplibreLayerDefn } from '../projects/projectsMap';
1212
import useMapboxSupportedLanguage from '../../hooks/UseMapboxSupportedLanguage';
1313

14-
import { MAPBOX_TOKEN, MAP_STYLE, CHART_COLOURS, TASK_COLOURS } from '../../config';
14+
import {
15+
MAPBOX_TOKEN,
16+
CHART_COLOURS,
17+
TASK_COLOURS,
18+
baseLayers,
19+
DEFAULT_MAP_STYLE,
20+
} from '../../config';
1521
import { fetchLocalJSONAPI } from '../../network/genericJSONRequest';
1622
import { useDebouncedCallback } from '../../hooks/UseThrottle';
1723
import isWebglSupported from '../../utils/isWebglSupported';
@@ -50,7 +56,7 @@ const ProjectCreationMap = ({ mapObj, setMapObj, metadata, updateMetadata, step,
5056
let bounds = mapObj.map.getBounds();
5157
let bbox = `${bounds._sw.lng},${bounds._sw.lat},${bounds._ne.lng},${bounds._ne.lat}`;
5258
fetchLocalJSONAPI(`projects/queries/bbox/?bbox=${bbox}&srid=4326`, token).then((res) => {
53-
mapObj.map.getSource('otherProjects').setData(res);
59+
mapObj.map.getSource('otherProjects')?.setData(res);
5460
setIsAoiLoading(false);
5561
});
5662
}
@@ -74,7 +80,7 @@ const ProjectCreationMap = ({ mapObj, setMapObj, metadata, updateMetadata, step,
7480
if (!isWebglSupported()) return;
7581
const map = new maplibregl.Map({
7682
container: mapRef.current,
77-
style: MAP_STYLE,
83+
style: DEFAULT_MAP_STYLE,
7884
center: [0, 0],
7985
zoom: 1.3,
8086
attributionControl: false,
@@ -103,6 +109,14 @@ const ProjectCreationMap = ({ mapObj, setMapObj, metadata, updateMetadata, step,
103109
}, []);
104110

105111
const addMapLayers = (map) => {
112+
// load all base layer and toggle visibility
113+
Object.entries(baseLayers).forEach(([key, value]) => {
114+
if (mapObj.map.getSource(`${key}-source`) === undefined) {
115+
mapObj.map.addSource(`${key}-source`, value.source);
116+
mapObj.map.addLayer(value.layer);
117+
}
118+
});
119+
106120
if (map.getSource('aoi') === undefined) {
107121
map.addSource('aoi', {
108122
type: 'geojson',
@@ -273,6 +287,7 @@ const ProjectCreationMap = ({ mapObj, setMapObj, metadata, updateMetadata, step,
273287
}
274288
});
275289
}
290+
// eslint-disable-next-line
276291
}, [mapObj, metadata, updateMetadata, step]);
277292

278293
if (!isWebglSupported()) {

frontend/src/components/projectEdit/priorityAreasForm.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import messages from './messages';
1313
import { StateContext, styleClasses } from '../../views/projectEdit';
1414
import { CustomButton } from '../button';
1515
import { MappedIcon, WasteIcon, MappedSquareIcon, FileImportIcon } from '../svgIcons';
16-
import { MAPBOX_TOKEN, MAP_STYLE, CHART_COLOURS } from '../../config';
16+
import { MAPBOX_TOKEN, CHART_COLOURS, baseLayers, DEFAULT_MAP_STYLE } from '../../config';
1717
import { BasemapMenu } from '../basemapMenu';
1818
import {
1919
verifyGeometry,
@@ -122,7 +122,7 @@ export const PriorityAreasForm = () => {
122122
isWebglSupported() &&
123123
new maplibregl.Map({
124124
container: mapRef.current,
125-
style: MAP_STYLE,
125+
style: DEFAULT_MAP_STYLE,
126126
center: [0, 0],
127127
zoom: 1,
128128
attributionControl: false,
@@ -138,6 +138,14 @@ export const PriorityAreasForm = () => {
138138
}, []);
139139

140140
const addMapLayers = (map) => {
141+
// load all base layer and toggle visibility
142+
Object.entries(baseLayers).forEach(([key, value]) => {
143+
if (mapObj.map.getSource(`${key}-source`) === undefined) {
144+
mapObj.map.addSource(`${key}-source`, value.source);
145+
mapObj.map.addLayer(value.layer);
146+
}
147+
});
148+
141149
if (map.getSource('aoi') === undefined) {
142150
map.addSource('aoi', {
143151
type: 'geojson',

0 commit comments

Comments
 (0)