@@ -7,10 +7,11 @@ import ReactMapGL, {
7
7
MapRef ,
8
8
MapEvent ,
9
9
} from 'react-map-gl' ;
10
- import { lineString as turfLineString , point as turfPoint } from '@turf/helpers' ;
10
+ import { point as turfPoint , featureCollection } from '@turf/helpers' ;
11
11
import { useDispatch , useSelector } from 'react-redux' ;
12
12
import turfNearestPointOnLine , { NearestPointOnLine } from '@turf/nearest-point-on-line' ;
13
- import { Feature , Position , LineString } from 'geojson' ;
13
+ import combine from '@turf/combine' ;
14
+ import { Position , LineString } from 'geojson' ;
14
15
import { useParams } from 'react-router-dom' ;
15
16
16
17
import { RootState } from 'reducers' ;
@@ -55,7 +56,6 @@ function Map() {
55
56
const { viewport, mapSearchMarker, mapStyle, mapTrackSources, showOSM, layersSettings } =
56
57
useSelector ( ( state : RootState ) => state . map ) ;
57
58
const [ idHover , setIdHover ] = useState < string | undefined > ( undefined ) ;
58
- const [ trackSectionHover , setTrackSectionHover ] = useState < Feature < any > > ( ) ;
59
59
const [ lngLatHover , setLngLatHover ] = useState < Position > ( ) ;
60
60
const [ trackSectionGeoJSON , setTrackSectionGeoJSON ] = useState < LineString > ( ) ;
61
61
const [ snappedPoint , setSnappedPoint ] = useState < NearestPointOnLine > ( ) ;
@@ -83,12 +83,7 @@ function Map() {
83
83
} ;
84
84
85
85
const onFeatureClick = ( e : MapEvent ) => {
86
- if (
87
- e . features &&
88
- e . features . length > 0 &&
89
- e . features [ 0 ] . properties . id !== undefined
90
- // && e.features[0].properties.type_voie === 'VP') {
91
- ) {
86
+ if ( e . features && e . features . length > 0 && e . features [ 0 ] . properties . id !== undefined ) {
92
87
dispatch (
93
88
updateFeatureInfoClickOSRD ( {
94
89
displayPopup : true ,
@@ -107,32 +102,9 @@ function Map() {
107
102
} ;
108
103
109
104
const getGeoJSONFeature = ( e : MapEvent ) => {
110
- if (
111
- trackSectionHover === undefined ||
112
- e ?. features ?. [ 0 ] . properties . id !== trackSectionHover ?. properties ?. id
113
- ) {
114
- setTrackSectionHover ( e ?. features ?. [ 0 ] ) ;
115
- }
116
-
117
- // Get GEOJSON of features hovered for snapping
118
- const width = 5 ;
119
- const height = 5 ;
120
- if ( mapRef . current ) {
121
- const features = mapRef . current . queryRenderedFeatures (
122
- [
123
- [ e . point [ 0 ] - width / 2 , e . point [ 1 ] - height / 2 ] ,
124
- [ e . point [ 0 ] + width / 2 , e . point [ 1 ] + height / 2 ] ,
125
- ] ,
126
- {
127
- layers :
128
- mapTrackSources === 'geographic'
129
- ? [ 'chartis/tracks-geo/main' ]
130
- : [ 'chartis/tracks-sch/main' ] ,
131
- }
132
- ) ;
133
- if ( features [ 0 ] !== undefined ) {
134
- setTrackSectionGeoJSON ( features [ 0 ] . geometry ) ;
135
- }
105
+ if ( e . features && e . features [ 0 ] !== undefined ) {
106
+ const mergedFeatures = combine ( featureCollection ( e . features ) ) ;
107
+ setTrackSectionGeoJSON ( mergedFeatures . features [ 0 ] . geometry ) ;
136
108
}
137
109
} ;
138
110
@@ -169,15 +141,14 @@ function Map() {
169
141
170
142
useEffect ( ( ) => {
171
143
if ( trackSectionGeoJSON !== undefined && lngLatHover !== undefined ) {
172
- const line = turfLineString ( trackSectionGeoJSON . coordinates ) ;
173
144
const point = turfPoint ( lngLatHover ) ;
174
145
try {
175
- setSnappedPoint ( turfNearestPointOnLine ( line , point ) ) ;
146
+ setSnappedPoint ( turfNearestPointOnLine ( trackSectionGeoJSON , point ) ) ;
176
147
} catch ( error ) {
177
148
console . warn ( `Ìmpossible to snapPoint - error ${ error } ` ) ;
178
149
}
179
150
}
180
- } , [ trackSectionGeoJSON , trackSectionHover , lngLatHover ] ) ;
151
+ } , [ trackSectionGeoJSON , lngLatHover ] ) ;
181
152
182
153
useEffect ( ( ) => {
183
154
if ( urlLat ) {
0 commit comments