I suggest to add an event for when data is added to a geoJson layer so plugins can hook into it if they want to work with that data.
Me from the viewpoint of the plugin currently can't tell if any data is added via addData to the geoJson layer. Is that correct? Is there any workaround to this?
It came up with this issue in my drawing/editing plugin.
GeoJSON derives from FeatureGroup which has the layeradd and layerremove event. Is that what you are looking for? This also provides better information than a generic "updated" event, as you don't have to reregister all event handlers for every existing layer as well.
@jwoyame I think layeradd and layerremove is when a layer is added to the group. But addData() adds geoJson data to an existing layer (it edits it, i guess?).
It's the difference between this:
var geoJsonLayer = L.geoJson(json).addTo(map);
and this:
var geoJsonLayer = L.geoJson().addTo(map);
geoJsonLayer.addData(json);
GeoJSON is internally a FeatureGroup, a layer that contains other layers. The layeradd and layerremove events are fired for the GeoJSON layer when its content layers are modified.
The addData function goes through each of the JSON objects in your array and calls addLayer on them, which fires layeradd for each one and provides you access to the new layer (via the layer property):
var geoJsonLayer = L.geoJson().addTo(map);
// called N times, where N is the number of objects in "json"
geoJsonLayer.on('layeradd', function (e) {
// do something with e.layer
});
geoJsonLayer.addData(json);
ahh good info I'll try that. Thank you very much.
I'll reopen if there are any issues left. Thanks again!
No problem, thanks for your plugin!
FYI: it worked perfectly
Most helpful comment
GeoJSONis internally aFeatureGroup, a layer that contains other layers. Thelayeraddandlayerremoveevents are fired for theGeoJSONlayer when its content layers are modified.The
addDatafunction goes through each of the JSON objects in your array and callsaddLayeron them, which fireslayeraddfor each one and provides you access to the new layer (via thelayerproperty):