@@ -19,6 +19,7 @@ import {
19
19
leafletGestureHandlingText ,
20
20
searchControlMessages ,
21
21
} from './translations' ;
22
+ import { GeoJsonGeometry } from './types' ;
22
23
23
24
// Run some Leaflet-specific patches...
24
25
initialize ( ) ;
@@ -92,18 +93,6 @@ const LeaftletMap = ({
92
93
onGeoJsonGeometrySet ?. ( newFeatureLayer . toGeoJSON ( ) . geometry ) ;
93
94
} ;
94
95
95
- useEffect ( ( ) => {
96
- // Remove all shapes from the map.
97
- // Only `geoJsonGeometry` should be shown on the map.
98
- featureGroupRef . current ?. clearLayers ( ) ;
99
- if ( ! geoJsonGeometry ) {
100
- return ;
101
- }
102
- // Add the current `geoJsonGeometry` as shape
103
- const layer = Leaflet . GeoJSON . geometryToLayer ( geoJsonGeometry ) ;
104
- featureGroupRef . current ?. addLayer ( layer ) ;
105
- } , [ geoJsonGeometry ] ) ;
106
-
107
96
return (
108
97
< >
109
98
< MapContainer
@@ -143,6 +132,7 @@ const LeaftletMap = ({
143
132
} }
144
133
/>
145
134
) }
135
+ < Geometry geoJsonGeometry = { geoJsonGeometry } featureGroupRef = { featureGroupRef } />
146
136
</ FeatureGroup >
147
137
{ coordinates && < MapView coordinates = { coordinates } /> }
148
138
{ ! disabled && (
@@ -171,21 +161,7 @@ const LeaftletMap = ({
171
161
} ;
172
162
173
163
LeaftletMap . propTypes = {
174
- geoJsonGeometry : PropTypes . oneOfType ( [
175
- PropTypes . shape ( {
176
- type : PropTypes . oneOf ( [ 'Point' ] ) . isRequired ,
177
- coordinates : PropTypes . arrayOf ( PropTypes . number ) . isRequired ,
178
- } ) ,
179
- PropTypes . shape ( {
180
- type : PropTypes . oneOf ( [ 'LineString' ] ) . isRequired ,
181
- coordinates : PropTypes . arrayOf ( PropTypes . arrayOf ( PropTypes . number ) ) . isRequired ,
182
- } ) ,
183
- PropTypes . shape ( {
184
- type : PropTypes . oneOf ( [ 'Polygon' ] ) . isRequired ,
185
- coordinates : PropTypes . arrayOf ( PropTypes . arrayOf ( PropTypes . arrayOf ( PropTypes . number ) ) )
186
- . isRequired ,
187
- } ) ,
188
- ] ) ,
164
+ geoJsonGeometry : GeoJsonGeometry ,
189
165
onGeoJsonGeometrySet : PropTypes . func ,
190
166
interactions : PropTypes . shape ( {
191
167
polyline : PropTypes . bool ,
@@ -196,6 +172,33 @@ LeaftletMap.propTypes = {
196
172
tileLayerUrl : PropTypes . string ,
197
173
} ;
198
174
175
+ const Geometry = ( { geoJsonGeometry, featureGroupRef} ) => {
176
+ useEffect ( ( ) => {
177
+ if ( ! featureGroupRef . current ) {
178
+ // If there is no feature group, nothing should be done...
179
+ return ;
180
+ }
181
+
182
+ // Remove all shapes from the map.
183
+ // Only the data from `geoJsonGeometry` should be shown on the map.
184
+ featureGroupRef . current . clearLayers ( ) ;
185
+ if ( ! geoJsonGeometry ) {
186
+ return ;
187
+ }
188
+
189
+ // Add the `geoJsonGeometry` data as shape.
190
+ const layer = Leaflet . GeoJSON . geometryToLayer ( geoJsonGeometry ) ;
191
+ featureGroupRef . current . addLayer ( layer ) ;
192
+ } , [ featureGroupRef , geoJsonGeometry ] ) ;
193
+
194
+ return null ;
195
+ } ;
196
+
197
+ Geometry . propTypes = {
198
+ geoJsonGeometry : GeoJsonGeometry ,
199
+ featureGroupRef : PropTypes . object . isRequired ,
200
+ } ;
201
+
199
202
// Set the map view if coordinates are provided
200
203
const MapView = ( { coordinates = null } ) => {
201
204
const map = useMap ( ) ;
0 commit comments