Skip to content

Commit

Permalink
front: fix osm layers render
Browse files Browse the repository at this point in the history
  • Loading branch information
clarani committed Dec 6, 2023
1 parent 5f3df61 commit b7a8a8e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
12 changes: 10 additions & 2 deletions front/src/applications/referenceMap/Map.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNil } from 'lodash';
import React, { useCallback, useEffect, useRef } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useParams } from 'react-router-dom';
import ReactMapGL, { AttributionControl, ScaleControl, MapRef } from 'react-map-gl/maplibre';
import { useDispatch, useSelector } from 'react-redux';
Expand Down Expand Up @@ -48,6 +48,7 @@ function Map() {
const { viewport, mapSearchMarker, mapStyle, showOSM, layersSettings } = useSelector(
(state: RootState) => state.map
);
const [mapLoaded, setMapLoaded] = useState(false);
const terrain3DExaggeration = useSelector(getTerrain3DExaggeration);
const mapRef = useRef<MapRef | null>(null);
const { urlLat, urlLon, urlZoom, urlBearing, urlPitch } = useParams();
Expand Down Expand Up @@ -127,6 +128,9 @@ function Map() {
? { source: 'terrain', exaggeration: terrain3DExaggeration }
: undefined
}
onLoad={() => {
setMapLoaded(true);
}}
>
<VirtualLayers />
<AttributionControl customAttribution={CUSTOM_ATTRIBUTION} />
Expand All @@ -144,7 +148,11 @@ function Map() {

{!showOSM ? null : (
<>
<OSM mapStyle={mapStyle} layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
<OSM
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
mapIsLoaded={mapLoaded}
/>
<Hillshade
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
Expand Down
13 changes: 4 additions & 9 deletions front/src/common/Map/Layers/OSM.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { LayerProps, Source } from 'react-map-gl/maplibre';

import mapStyleBluePrintJson from 'assets/mapstyles/OSMBluePrintStyle.json';
Expand All @@ -12,6 +11,7 @@ import OrderedLayer, { OrderedLayerProps } from 'common/Map/Layers/OrderedLayer'

interface OSMProps {
mapStyle: string;
mapIsLoaded?: boolean;
layerOrder?: number;
}

Expand Down Expand Up @@ -47,13 +47,12 @@ export function genOSMLayers(mapStyle: string, layerOrder?: number) {
return genOSMLayerProps(mapStyle, layerOrder).map((props) => <OrderedLayer {...props} />);
}

function OSM(props: OSMProps) {
const { mapStyle, layerOrder } = props;

function OSM({ mapStyle, layerOrder, mapIsLoaded }: OSMProps) {
// Hack to full reload layers to avoid glitches
// when switching map style (see #5777)
const [reload, setReload] = useState(true);
useEffect(() => setReload(true), [mapStyle]);

useEffect(() => setReload(true), [mapStyle, mapIsLoaded]);
useEffect(() => {
if (reload) setReload(false);
}, [reload]);
Expand All @@ -65,8 +64,4 @@ function OSM(props: OSMProps) {
) : null;
}

OSM.propTypes = {
mapStyle: PropTypes.string.isRequired,
};

export default OSM;
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,11 @@ const Map: FC<MapProps> = ({ setExtViewport }) => {

{!showOSM ? null : (
<>
<OSM mapStyle={mapStyle} layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
<OSM
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
mapIsLoaded={mapLoaded}
/>
<Hillshade
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ function Map() {
(value: Partial<Viewport>) => dispatch(updateViewport(value, undefined)),
[dispatch]
);

const [mapLoaded, setMapLoaded] = useState(false);

const mapRef = useRef<MapRef | null>(null);

const scaleControlStyle = {
Expand Down Expand Up @@ -149,6 +152,10 @@ function Map() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const handleLoadFinished = () => {
setMapLoaded(true);
};

return (
<>
<MapButtons map={mapRef.current ?? undefined} resetPitchBearing={resetPitchBearing} />
Expand Down Expand Up @@ -176,6 +183,7 @@ function Map() {
? { source: 'terrain', exaggeration: terrain3DExaggeration }
: undefined
}
onLoad={handleLoadFinished}
>
<VirtualLayers />
<AttributionControl position="bottom-right" customAttribution={CUSTOM_ATTRIBUTION} />
Expand All @@ -193,7 +201,11 @@ function Map() {

{!showOSM ? null : (
<>
<OSM mapStyle={mapStyle} layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
<OSM
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
mapIsLoaded={mapLoaded}
/>
<Hillshade
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
Expand Down

0 comments on commit b7a8a8e

Please sign in to comment.