米脂
2022-06-09 17:23:57
類似于Search for marker in a markercluster group Leaflet-MarkerCluster但是我在Marker Cluster頂部使用了一個Control 組,因此它們將在單擊單選按鈕時顯示。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}) );
1 回答

精慕HU
TA貢獻1845條經驗 獲得超8個贊
解決了這個問題——盡管它的效率很低。如果您能找到更優雅的解決方案來避免重復,請隨時將其作為答案!
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);
})
});
然后,您將 markerLayer 添加到 Leaflet Search
添加回答
舉報
0/150
提交
取消