通过功能组添加传单搜索

Add Leaflet Search through a Feature Group

类似于

但我使用的是 Control group ontop of Marker Cluster,因此它们将在单击单选按钮时显示。

var map = L.map("map"),

parentGroup = L.markerClusterGroup(options), // Could be any other Layer Group type.
  
// arrayOfMarkers refers to layers to be added under the parent group as sub group feature
    
mySubGroup = L.featureGroup.subGroup(parentGroup, arrayOfMarkers);

parentGroup.addTo( map );
mySubGroup.addTo( map );

我正在尝试实施 Leaflet Search - 但根据文档所述,它需要 group layer 标记作为其工作的第二个参数。问题是使用 L.featureGroup.subGroup 需要一组标记。

尝试在 运行 时间遍历 mySubGroup 以使用 Leaflet eachLayer 获取标记层,但这将复制我在地图上用于搜索的标记数量工作。

var markersLayer = new L.LayerGroup().addTo( this.map );

forEach( mySubGroup, layers => {
    layers.eachLayer( function (layer ) {
        console.log ( layer );

        markersLayer.addLayer( layer );

    })
});

   map.addControl( new L.Control.Search({layer: markersLayer}) );

解决了这个问题 - 虽然效率很低。如果你能找到一个更优雅的解决方案来避免重复,那么请随意贡献它作为答案!

 var title = layer.options.title;

// iterate through the cluster points
forEach( mySubGroup, layers => {
    layers.eachLayer(function (layer) {
        var title = layer.options.title; // match the search title to marker title

         marker = new L.Circle(new L.LatLng(layer.getLatLng().lat,layer.getLatLng().lng),
            {radius: 0, title: title, fill: 'red', fillOpacity: 0, opacity: 0 }); // Create an invisible L circle marker for each cluseter marker 


        markersLayer.addLayer(marker);

    })
});

然后将 markersLayer 添加到传单搜索