-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathBaseMap.tsx
More file actions
189 lines (174 loc) · 5.12 KB
/
Copy pathBaseMap.tsx
File metadata and controls
189 lines (174 loc) · 5.12 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { type MutableRefObject, type PropsWithChildren, useEffect, useState } from 'react';
import type { Geometry } from 'geojson';
import type { MapLayerMouseEvent, MapLibreEvent } from 'maplibre-gl';
import ReactMapGL, { AttributionControl, ScaleControl } from 'react-map-gl/maplibre';
import type { MapRef } from 'react-map-gl/maplibre';
import { useParams } from 'react-router-dom';
import {
IGNLayers,
InfraObjectLayers,
LineSearchLayer,
OSMLayers,
SearchMarker,
VirtualLayers,
useMapBlankStyle,
} from 'common/Map/Layers';
import { colors } from 'common/Map/theme';
import { LAYER_GROUPS_ORDER, LAYERS } from 'config/layerOrder';
import type { MapSettings, Viewport } from 'reducers/commonMap/types';
import { CUSTOM_ATTRIBUTION } from './const';
type MapProps = {
mapSettings: MapSettings;
mapId: string;
mapRef: MutableRefObject<MapRef | null>;
interactiveLayerIds: string[];
infraId?: number;
updatePartialViewPort: (
newPartialViewPort: Partial<Viewport>,
options?: { updateRouter: boolean }
) => void;
cursor?: 'default' | 'pointer' | 'normal';
hideAttribution?: boolean;
hoveredOperationalPointId?: string;
onClick?: (e: MapLayerMouseEvent) => void;
onMouseEnter?: (e: MapLayerMouseEvent) => void;
onMouseMove?: (e: MapLayerMouseEvent) => void;
onIdle?: (e: MapLibreEvent) => void;
/**
* If an area is provided, then the map style is focus on it :
* - filtering all data layouts on this area
* - OP & tracks are full displayed, but elements ouside the area are muted
*/
highlightedArea?: Geometry;
highlightedOperationalPoints?: number[];
};
const BaseMap = ({
mapId,
mapRef,
children,
interactiveLayerIds,
infraId,
mapSettings,
cursor = 'default',
hideAttribution = false,
hoveredOperationalPointId,
updatePartialViewPort,
onClick,
onMouseEnter,
onMouseMove,
onIdle,
highlightedArea,
highlightedOperationalPoints,
}: PropsWithChildren<MapProps>) => {
const mapBlankStyle = useMapBlankStyle();
const [mapIsLoaded, setMapIsLoaded] = useState(false);
const { urlLat = '', urlLon = '', urlZoom = '', urlBearing = '', urlPitch = '' } = useParams();
const {
viewport,
mapStyle,
layersSettings,
showOSM,
showOSM3dBuildings,
showOSMtracksections,
terrain3DExaggeration,
mapSearchMarker,
lineSearchCode,
showIGNBDORTHO,
showIGNSCAN25,
showIGNCadastre,
} = mapSettings;
useEffect(() => {
if (urlLat) {
updatePartialViewPort({
latitude: parseFloat(urlLat),
longitude: parseFloat(urlLon),
zoom: parseFloat(urlZoom),
bearing: parseFloat(urlBearing),
pitch: parseFloat(urlPitch),
});
}
}, []);
return (
<ReactMapGL
id={mapId}
ref={mapRef}
{...viewport}
interactiveLayerIds={interactiveLayerIds}
canvasContextAttributes={{ preserveDrawingBuffer: true }}
cursor={cursor}
mapStyle={mapBlankStyle}
terrain={
terrain3DExaggeration
? { source: 'terrain', exaggeration: terrain3DExaggeration }
: undefined
}
onMouseEnter={onMouseEnter}
onMouseMove={onMouseMove}
onClick={onClick}
onIdle={onIdle}
// default behavior
onMove={(e) => {
updatePartialViewPort(e.viewState);
}}
onMoveEnd={(e) => updatePartialViewPort(e.viewState, { updateRouter: true })}
onResize={(e) => {
updatePartialViewPort({
width: e.target.getContainer().offsetWidth,
height: e.target.getContainer().offsetHeight,
});
}}
onLoad={() => {
setMapIsLoaded(true);
}}
attributionControl={false} // Defined below
dragPan
maxPitch={85}
scrollZoom
style={{ width: '100%', height: '100%' }}
touchZoomRotate
>
<VirtualLayers />
{!hideAttribution && (
<AttributionControl position="bottom-right" customAttribution={CUSTOM_ATTRIBUTION} />
)}
<ScaleControl
maxWidth={100}
unit="metric"
style={{
left: 20,
bottom: 20,
}}
/>
{infraId && (
<InfraObjectLayers
infraId={infraId}
mapStyle={mapStyle}
hoveredOperationalPointId={hoveredOperationalPointId}
layersSettings={layersSettings}
highlightedArea={highlightedArea}
highlightedOperationalPoints={highlightedOperationalPoints}
/>
)}
<OSMLayers
hidePlatforms={!layersSettings.platforms}
mapStyle={mapStyle}
showOSM={showOSM && mapIsLoaded}
showOSM3dBuildings={showOSM3dBuildings && mapIsLoaded}
showOSMtracksections={showOSMtracksections && mapIsLoaded}
/>
<IGNLayers
showIGNBDORTHO={showIGNBDORTHO}
showIGNCadastre={showIGNCadastre}
showIGNSCAN25={showIGNSCAN25}
/>
<LineSearchLayer
layerOrder={LAYER_GROUPS_ORDER[LAYERS.LINE_SEARCH.GROUP]}
infraID={infraId}
lineSearchCode={lineSearchCode}
/>
{mapSearchMarker && <SearchMarker data={mapSearchMarker} colors={colors[mapStyle]} />}
{children}
</ReactMapGL>
);
};
export default BaseMap;