TransWikia.com

How to remove features from a Leaflet GeoJSON layer?

Geographic Information Systems Asked on May 26, 2021

In a Leaflet map, it is possible to add features to a GeoJSON layer using the addData method:

L.geoJSON: Creates a GeoJSON layer. Optionally accepts an object in
GeoJSON format (you can alternatively add it later with addData
method)

addData(data): Adds a GeoJSON object to the layer.

Is it possible to remove all features from the GeoJSON layer, without destroying the layer and recreating it?

One Answer

It seems that GeoJSON extends FeatureGroup, which extends LayerGroup, which has a method clearLayers().

This means that jsonLayer.clearLayers() can be used to remove the features from a GeoJSON layer.

https://leafletjs.com/reference-1.7.1.html#layergroup

Correct answer by Stephen Lead on May 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP