好的,我现在已经对此进行了一些实验;)
在 Leaflet.markercluster-src.js 中,我创建了一个名为 _spiderMan[] 的数组,其中填充了函数 spiderfy 中单击的对象。
spiderfy: function() {
if (this._group._spiderfied === this || this._group._inZoomAnimation) {
return;
}
var childMarkers = this.getAllChildMarkers(null, true),
group = this._group,
map = group._map,
center = map.latLngToLayerPoint(this._latlng),
positions;
// this._group._unspiderfy(); //deactivated
var markers = markers + childMarkers;
_spiderMan.push(this); //new
if (childMarkers.length >= this._circleSpiralSwitchover) {
positions = this._generatePointsSpiral(childMarkers.length, center);
} else {
center.y += 10;
positions = this._generatePointsCircle(childMarkers.length, center);
}
this._animationSpiderfy(childMarkers, positions);
},
然后我创建了一个 for 循环,它遍历数组并每次调用 _spiderMan[i].unspiderfy(zoomDetails)。我将此循环构建到函数_unspiderfyZoomAnim 中进行测试。意味着每次放大或缩小时,都会汇总所有打开的组。
_unspiderfyZoomAnim: function(zoomDetails) {
if (L.DomUtil.hasClass(this._map._mapPane, 'leaflet-touching')) {
return;
}
this._map.off('zoomanim', this._unspiderfyZoomAnim, this);
var i;
for (i = 0; i < _spiderMan.length; i++) {
_spiderMan[i].unspiderfy(zoomDetails);
}
_spiderMan = [];
},
此外,必须在unspiderfy 函数中禁用以下行:
unspiderfy: function(zoomDetails) {
/// <param Name="zoomDetails">Argument from zoomanim if being called in a zoom animation or null otherwise</param>
// if (this._group._inZoomAnimation) {
// return;
// }
this._animationUnspiderfy(zoomDetails);
// this._group._spiderfied = null;
},
所以现在可以打开和关闭多个集群组,但这只是一种解决方法,我认为由于注释掉或删除代码行,它会在某处产生一些不良影响。
我认为在 JS 和这个插件方面有更多经验的人应该找到更好、更舒适的解决方案;)。