Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-leaflet add layers dynamically

I'm starting to use react-leaflet and I came across a question: In my application the user fills out a form that then requests a rest service that returns a GeoJSON, which is then added as a new layer on my map. My question: How do I implement adding layers dynamically in react-leaflet?

Thank you.

like image 957
Davi Custodio Avatar asked Jan 29 '17 18:01

Davi Custodio


1 Answers

The best approach is to create GeoJSON Layer wrapper for react-leaflet. There is a similar implementation of GeoJSON layer with clustering available in react-leaflet's plugins section. Then you should add this layer to your map component and change it's data when you need to. So there is no need to add the layer dynamically but dynamically change data for it. Check the leaflet's GeoJSON example to get the idea http://leafletjs.com/examples/geojson/.

The approach will work if you have one layer with changing data. But if you have different data sets you will need to add a GeoJSON layer for each of them.

<Map ...>
{this.props.datasets.map((ds, ix) => {
  return (<GeoJSONOverlay data={ds} key={ix} />);
})}
</Map>
like image 114
Eugene G Avatar answered Nov 18 '22 17:11

Eugene G