Javascript 如何获取传单层组ID?

Javascript 如何获取传单层组ID?,javascript,leaflet,geojson,Javascript,Leaflet,Geojson,我有两层传单。每一层都是美国的geoJSON地图,具有51个特征 我将mouseout上的事件处理程序附加到两个: function resetHighlight(e, whichLayer) { if (whichLayer == terpsLayer) { console.log('reset'); layerTerps.resetStyle(e.target); } else if (whichLayer == lawsLayer) {

我有两层传单。每一层都是美国的geoJSON地图,具有51个特征

我将mouseout上的事件处理程序附加到两个:

function resetHighlight(e, whichLayer) {
    if (whichLayer == terpsLayer) {
        console.log('reset');
        layerTerps.resetStyle(e.target);
    } else if (whichLayer == lawsLayer) {
        layerLaws.resetStyle(e.target);
    }
}

function onEachFeature(feature, layer) {
    layer.on({
        mouseout: function(e) {
           resetHighlight(e, layer);
        }
    });
}
在创建geoJSON层时,这在典型的
onEachFeature
函数中调用


我不知道如何使用
whichLayer
来表示geoJSON层,而不是单独的51个特性之一。例如,如果您单击某个功能,如何获取传单以识别它来自哪个组图层?

您可以轻松创建一个图层组,并将两个图层添加到其中,如下所示:

layerGroup = new L.LayerGroup()
layerGroup.addLayer(Layer1)
layerGroup.addLayer(Layer2)
然后在图层组中循环以获取图层Id,并在每个图层中循环以获取要素Id,如下所示:

layerGroup.eachLayer(function (layer) {
    const layerId = layerGroup.getLayerId(layer)
    layer.eachLayer(function (layer) {
        const FeatureId = layer.feature.properties.Id
    });
});

您可以调用
hasLayer(layer)
来检查您的图层所在的图层组

layerGroup1 = new L.LayerGroup();
layerGroup2 = new L.LayerGroup();

function getLayerGroup(layer){
    if(layerGroup1.hasLayer(layer)){
        return layerGroup1;
    }else if(layerGroup2.hasLayer(layer)){
        return layerGroup2;
    }else{
        return null;
    }
}