What exactly does a layer represent in the Leaflet Mapping Library?
Conceptually, to me a layer would represent a single tier of some type of feature or object; for example all image tiles representing the base level map would be represented on a single layer, a set of polygons representing states in the US may be on their own separate layer.
Specifically looking at L.GeoJSON.addGeoJSON(geojson), it reads that each new polygon created is placed in it’s own layer (and then maybe merged with the layer you’re calling the method on?). My use case is that I need to add many geoJSON objects one at a time and want to ensure I’m not creating many unnecessary layers (or if I am, if this is actually a bad thing).
Thank you.
In Leaflet anything that can be added to the map is a layer. So polygons, circles, markers, popups, tiles are all layers. You can combine layers in a
L.LayerGroup(or FeatureGroup), if you for example want to treat a set of polygons as a single layer. So maybe your interpretation of layers matches better with what is modelled byL.LayerGroupin Leaflet.L.GeoJSONis a LayerGroup (specifically a FeatureGroup) that is initialized from GeoJSON. Each new polygon is added to theL.GeoJSONLayerGroup using addLayer, which is the method for adding anything (that is a layer) to aLayerGroup. It does not create a new layer for each polygon (other than theL.Polygonwhich is already considered a layer). It only creates new FeatureGroups (LayerGroups) for a GeometryCollection and MultiPoints, which (I assume) is in order to preserve the structure from the GeoJSON.If you want to add geoJSON objects to the same LayerGroup one at a time, you can just call
L.GeoJSON.geometryToLayerto convert your GeoJSON object, and then add it to your LayerGroup usingL.LayerGroup.addLayer.