【问题标题】:Add Leaflet Search through a Feature Group通过功能组添加传单搜索
【发布时间】:2020-04-02 22:13:33
【问题描述】:

类似于Search for markers in a markercluster group Leaflet-MarkerCluster

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

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}) );

【问题讨论】:

  • 在eachLayer函数中调用layers.removeLayer(layer)
  • @FalkeDesign 这将删除我的 mySubGroup 集群并用单点标记替换它
  • 然后不要将图层组添加到地图var markersLayer = new L.LayerGroup() 否则您将始终复制标记。但我不知道这是否适用于 SearchControl

标签: javascript leaflet leaflet.markercluster


【解决方案1】:

解决了这个问题——虽然效率很低。如果您能找到更优雅的解决方案来避免重复,请随时将其作为答案!

 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);

    })
});

然后将标记层添加到传单搜索

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2021-08-14
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多