[GIS] leaflet GeoJson vs polyline

geojsonleafletline

Is there any real advantage in using L.GeoJson instead of Polyline when drawing paths in a map powered by Leaflet?

I know that GeoJson can attach additional options. It is also possible with Polyline but one has to extend it or have a wrapper object.

Our project needs dynamic addition and removal of paths. Can GeoJson still handle it?

Best Answer

The L.GeoJSON layer group is just a Feature Group with extra methods specific for vector layers.

It provides a convenient conversion from a GeoJSON data object into a layer group made of Leaflet vector shapes converted from the GeoJSON data. In particular, a LineString is converted into an L.Polyline.

So your L.GeoJSON group is in the end just a container for your Polylines.

Therefore you can directly add and remove child layers like with a normal group (addLayer and removeLayer methods). You can add and remove child Polylines, whether built automatically at construction from the GeoJSON object, or added later on through addLayer (or even through addData).

If you want to shorten / extend a Polyline (through spliceLatLngs and addLatLng methods), of course you can also do it, provided that you get the direct reference to your Polyline.