2017-03-17 58 views
0

我設立一個以GeoJSON層,並在其頂部的MarkerCluster層刷新標記集羣已經改變

this.itemLayer = L.geoJson(items, layerOptions) 
this.clusterLayer = L.markerClusterGroup() 
this.clusterLayer.addLayer(this.itemLayer) 
this.clusterLayer.addTo(this.map) 

在更新我做:

this.itemLayer.clearLayers() 
this.itemLayer.addData(newItems) 
this.clusterLayer.refreshClusters(this.itemLayer) 

但集羣做沒有出現,也沒有在itemLayer

解決方案做的項目

this.itemLayer.clearLayers() 
this.itemLayer.addData(this.props.items) 
this.clusterLayer.clearLayers() 
this.clusterLayer.addLayer(this.itemLayer) 

回答

0

不幸的是,Leaflet.markercluster沒有跟蹤圖層組(例如您的this.itemLayer GeoJSON圖層組)。當一個組通過clusterLayer.addLayer()時,MCG將從該組中提取所有單個(即非組)圖層,並忘記對該組的任何引用。

另請參閱Leaflet.markercluster issue #647

因此,使用this.itemLayer.clearLayers()清除您的羣組時,它會從this.itemLayer中有效移除所有子女,但this.clusterLayer不受影響。

同樣,當向this.itemLayer添加數據時,該組將創建新的子圖層,但MCG不受影響。

然後調用this.clusterLayer.refreshClusters(this.itemLayer)時,沒有this.itemLayer子層是this.clusterLayer一部分,所以它有意想不到的效果結束了(也許只是無爲特種)。

如果要更改聚簇層,請確保將其從MCG中移除(例如,只需執行this.clusterLayer.clearLayers()),然後將新層添加到其中。您也可以刪除當前的MCG並構建一個新的MCG。

+0

謝謝,我會在本週晚些時候進行測試。 – philk

+0

'this.itemLayer.clearLayers() this.itemLayer.addData(this.props.items) this.clusterLayer.clearLayers() this.clusterLayer.addLayer(this.itemLayer) ' 似乎工作。它看起來對你正確嗎@ghybs – philk

+0

對我來說很好看:-) – ghybs