When you are displaying geojson with leaflet overlays, onEachFeature is a function will be applied on each feature, e.g. a point or a polygon in a features collection.

I overwrote the onEachFeature with a cusmerized function, when this funtion is running, it get the information from current display/undisplay layer, and changes the global variable gDisplayLayersInfo accordingly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function overlayOnEachFeature(feature, layer){
layer.on({
// when a overlay is on display
add: function (e) {
// get information from the current layer
var layerInfo = getLayerInfo(layer);
// add the information to a global variable
gDisplayLayersInfo.push('<br>' + layerInfo);
},
remove: function (e) {
var layerInfo = getLayerInfo(layer);
var listElement = '<br>' + layerInfo;
for (var l in gDisplayLayersInfo){
// if the information is in the global variable, delete the information
if (listElement === gDisplayLayersInfo[l]){
gDisplayLayersInfo.splice(l,1);
};
};
}
});
}

Apply the cusmerized onEachFeature to the layer which will be added:

1
2
3
4
5
6
//add geojson polygon to the map
var transportationLayer = new L.geoJSON(gTransportation, {
style: transportationStyle,
// overwrite the function
onEachFeature: overlayOnEachFeature
});