有没有一种方法可以使用GridLayer复制L.LayerGroup.eachLayer()的行为?我正在使用Leaflet.VectorGrid(L.VectorGrid.Slicer)来在世界范围内包装一些GeoJSON,并且我想将每个要素添加到一个或多个图层组中。例如,下面的代码适用于GeoJSON对象,但不适用于网格图层。

// want to do something like this; layer groups defined previously
L.geoJSON(usData, {
  style: // styling logic
})
 .eachLayer(layer => {
   layerGroup1.addLayer(layer);
   if (category2.indexOf(layer.someProperty) !== -1) {
     layerGroup2.addLayer(layer);
   }
   if (category3.indexOf(layer.someProperty) !== -1) {
     layerGroup3.addLayer(layer);
   }
})
 .addTo(mapObject);


// no eachLayer() method for grid layers or slicers; how could you do this with grid layers?
L.vectorGrid.slicer(usData, {
  vectorTileLayerStyles: {
    sliced: properties => someFunction(properties)
  },
  interactive: true
})
 .eachLayer(layer => {
   // do something with each layer
})
 .addTo(mapObject);

最佳答案

创建几个L.VectorGrid.Slicer实例,每个所需的LayerGroup实例。相应地过滤您的GeoJSON数据。

10-08 13:13