1
- import 'common/Map/Map.scss' ;
2
-
3
1
import React , { useCallback , useEffect , useRef , useState } from 'react' ;
4
- import ReactMapGL , { AttributionControl , FlyToInterpolator , ScaleControl } from 'react-map-gl' ;
2
+
3
+ import ReactMapGL , {
4
+ AttributionControl ,
5
+ FlyToInterpolator ,
6
+ ScaleControl ,
7
+ MapRef ,
8
+ MapEvent ,
9
+ } from 'react-map-gl' ;
5
10
import { lineString as turfLineString , point as turfPoint } from '@turf/helpers' ;
6
11
import { useDispatch , useSelector } from 'react-redux' ;
12
+ import turfNearestPointOnLine , { NearestPointOnLine } from '@turf/nearest-point-on-line' ;
13
+ import { Feature , Position , LineString } from 'geojson' ;
14
+ import { useParams } from 'react-router-dom' ;
15
+
16
+ import { RootState } from 'reducers' ;
17
+ import { updateFeatureInfoClickOSRD } from 'reducers/osrdconf' ;
18
+ import { updateViewport } from 'reducers/map' ;
7
19
8
20
/* Main data & layers */
9
21
import Background from 'common/Map/Layers/Background' ;
@@ -34,26 +46,24 @@ import TracksGeographic from 'common/Map/Layers/TracksGeographic';
34
46
import TracksSchematic from 'common/Map/Layers/TracksSchematic' ;
35
47
import colors from 'common/Map/Consts/colors' ;
36
48
import osmBlankStyle from 'common/Map/Layers/osmBlankStyle' ;
37
- import turfNearestPointOnLine from '@turf/nearest-point-on-line' ;
38
- import { updateFeatureInfoClickOSRD } from 'reducers/osrdconf' ;
39
- import { updateViewport } from 'reducers/map' ;
40
- import { useParams } from 'react-router-dom' ;
49
+
50
+ import 'common/Map/Map.scss' ;
41
51
42
52
function Map ( ) {
43
53
const { viewport, mapSearchMarker, mapStyle, mapTrackSources, showOSM, layersSettings } =
44
- useSelector ( ( state ) => state . map ) ;
45
- const [ idHover , setIdHover ] = useState ( undefined ) ;
46
- const [ trackSectionHover , setTrackSectionHover ] = useState ( undefined ) ;
47
- const [ lngLatHover , setLngLatHover ] = useState ( undefined ) ;
48
- const [ trackSectionGeoJSON , setTrackSectionGeoJSON ] = useState ( undefined ) ;
49
- const [ snappedPoint , setSnappedPoint ] = useState ( undefined ) ;
50
- const { urlLat, urlLon, urlZoom, urlBearing, urlPitch } = useParams ( ) ;
54
+ useSelector ( ( state : RootState ) => state . map ) ;
55
+ const [ idHover , setIdHover ] = useState ( ) ;
56
+ const [ trackSectionHover , setTrackSectionHover ] = useState < Feature < any > > ( ) ;
57
+ const [ lngLatHover , setLngLatHover ] = useState < Position > ( ) ;
58
+ const [ trackSectionGeoJSON , setTrackSectionGeoJSON ] = useState < LineString > ( ) ;
59
+ const [ snappedPoint , setSnappedPoint ] = useState < NearestPointOnLine > ( ) ;
60
+ const { urlLat = '' , urlLon = '' , urlZoom = '' , urlBearing = '' , urlPitch = '' } = useParams ( ) ;
51
61
const dispatch = useDispatch ( ) ;
52
62
const updateViewportChange = useCallback (
53
63
( value ) => dispatch ( updateViewport ( value , undefined ) ) ,
54
64
[ dispatch ]
55
65
) ;
56
- const mapRef = useRef ( null ) ;
66
+ const mapRef = useRef < MapRef > ( null ) ;
57
67
58
68
const scaleControlStyle = {
59
69
left : 20 ,
@@ -63,8 +73,8 @@ function Map() {
63
73
const resetPitchBearing = ( ) => {
64
74
updateViewportChange ( {
65
75
...viewport ,
66
- bearing : parseFloat ( 0 ) ,
67
- pitch : parseFloat ( 0 ) ,
76
+ bearing : 0 ,
77
+ pitch : 0 ,
68
78
transitionDuration : 1000 ,
69
79
transitionInterpolator : new FlyToInterpolator ( ) ,
70
80
} ) ;
@@ -94,47 +104,49 @@ function Map() {
94
104
}
95
105
} ;
96
106
97
- const getGeoJSONFeature = ( e ) => {
107
+ const getGeoJSONFeature = ( e : MapEvent ) => {
98
108
if (
99
109
trackSectionHover === undefined ||
100
- e . features [ 0 ] . properties . id !== trackSectionHover . properties . id
110
+ e ? .features ?. [ 0 ] . properties . id !== trackSectionHover ? .properties ? .id
101
111
) {
102
- setTrackSectionHover ( e . features [ 0 ] ) ;
112
+ setTrackSectionHover ( e ? .features ?. [ 0 ] ) ;
103
113
}
104
114
105
115
// Get GEOJSON of features hovered for snapping
106
116
const width = 5 ;
107
117
const height = 5 ;
108
- const features = mapRef . current . queryRenderedFeatures (
109
- [
110
- [ e . point [ 0 ] - width / 2 , e . point [ 1 ] - height / 2 ] ,
111
- [ e . point [ 0 ] + width / 2 , e . point [ 1 ] + height / 2 ] ,
112
- ] ,
113
- {
114
- layers :
115
- mapTrackSources === 'geographic'
116
- ? [ 'chartis/tracks-geo/main' ]
117
- : [ 'chartis/tracks-sch/main' ] ,
118
+ if ( mapRef . current ) {
119
+ const features = mapRef . current . queryRenderedFeatures (
120
+ [
121
+ [ e . point [ 0 ] - width / 2 , e . point [ 1 ] - height / 2 ] ,
122
+ [ e . point [ 0 ] + width / 2 , e . point [ 1 ] + height / 2 ] ,
123
+ ] ,
124
+ {
125
+ layers :
126
+ mapTrackSources === 'geographic'
127
+ ? [ 'chartis/tracks-geo/main' ]
128
+ : [ 'chartis/tracks-sch/main' ] ,
129
+ }
130
+ ) ;
131
+ if ( features [ 0 ] !== undefined ) {
132
+ setTrackSectionGeoJSON ( features [ 0 ] . geometry ) ;
118
133
}
119
- ) ;
120
- if ( features [ 0 ] !== undefined ) {
121
- setTrackSectionGeoJSON ( features [ 0 ] . geometry ) ;
122
134
}
123
135
} ;
124
136
125
- const onFeatureHover = ( e ) => {
126
- if ( e . features !== null && e . features [ 0 ] !== undefined ) {
137
+ const onFeatureHover = ( e : MapEvent ) => {
138
+ if ( e . features !== null && e ? .features ?. [ 0 ] !== undefined ) {
127
139
getGeoJSONFeature ( e ) ;
128
140
setIdHover ( e . features [ 0 ] . properties . id ) ;
129
- setLngLatHover ( e . lngLat ) ;
141
+ setLngLatHover ( e ? .lngLat ) ;
130
142
} else {
131
143
setIdHover ( undefined ) ;
132
144
setSnappedPoint ( undefined ) ;
133
145
}
134
146
} ;
135
147
136
148
const defineInteractiveLayers = ( ) => {
137
- const interactiveLayersLocal = [ ] ;
149
+ const interactiveLayersLocal : Array < string > = [ ] ;
138
150
if ( mapTrackSources === 'geographic' ) {
139
151
interactiveLayersLocal . push ( 'chartis/tracks-geo/main' ) ;
140
152
if ( layersSettings . operationalpoints ) {
@@ -215,7 +227,7 @@ function Map() {
215
227
{ /* Have to duplicate objects with sourceLayer to avoid cache problems in mapbox */ }
216
228
{ mapTrackSources === 'geographic' ? (
217
229
< >
218
- < TVDs geomType = "geo" colors = { colors [ mapStyle ] } idHover = { idHover } />
230
+ < TVDs geomType = "geo" idHover = { idHover } />
219
231
< Catenaries geomType = "geo" colors = { colors [ mapStyle ] } />
220
232
< Platform colors = { colors [ mapStyle ] } />
221
233
< TracksGeographic colors = { colors [ mapStyle ] } />
@@ -247,7 +259,7 @@ function Map() {
247
259
< RenderItinerary />
248
260
< RenderItineraryMarkers />
249
261
{ mapSearchMarker !== undefined ? (
250
- < SearchMarker data = { mapSearchMarker } colors = { colors [ mapStyle ] } />
262
+ < SearchMarker data = { mapSearchMarker as object } colors = { colors [ mapStyle ] } />
251
263
) : null }
252
264
{ snappedPoint !== undefined ? < SnappedMarker geojson = { snappedPoint } /> : null }
253
265
</ ReactMapGL >
0 commit comments