From fa4d9913f6a7ea9b9dd14c7f8d0b04d19aaf2c9a Mon Sep 17 00:00:00 2001 From: danzel Date: Tue, 11 Sep 2012 14:43:26 +1200 Subject: [PATCH] New, working removeLayer code --- src/MarkerCluster.js | 23 +----------- src/MarkerClusterGroup.js | 79 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 25 deletions(-) diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js index 5eb9c42dc..fb15cebeb 100644 --- a/src/MarkerCluster.js +++ b/src/MarkerCluster.js @@ -89,28 +89,7 @@ L.MarkerCluster = L.Marker.extend({ } }, - _removeChildMarker: function (marker) { - var markers = this._markers, - i; - - for (i = markers.length - 1; i >= 0; i--) { - if (markers[i] === marker) { - markers.splice(i, 1); - - var p = this; - while (p) { - p._childCount--; - p._recalculateBounds(); - p._updateIcon(); - p = p._parent; - } - return true; - } - } - - }, - - //Expand our bounds and tell our parent to + //Expand our bounds and tell our parent to _expandBounds: function (marker) { var addedCount, addedLatLng = marker._wLatLng || marker._latlng; diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 17627620d..8ad5c0e92 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -103,11 +103,84 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ this._unspiderfyLayer(layer); } - layer.__cluster._removeChildMarker(layer); + //Remove the marker from clusters + this._removeLayer(layer, true); + if (layer._icon) { + L.FeatureGroup.prototype.removeLayer.call(this, layer); + } return this; }, + //Remove the given object from the given array + _arraySplice: function (anArray, obj) { + for (var i = anArray.length - 1; i >= 0; i--) { + if (anArray[i] === obj) { + anArray.splice(i, 1); + return; + } + } + }, + + _removeLayer: function (marker, removeFromDistanceGrid) { + var gridClusters = this._gridClusters, + gridUnclustered = this._gridUnclustered, + map = this._map; + + //Remove the marker from distance clusters it might be in + if (removeFromDistanceGrid) { + for (var z = this._maxZoom; z >= 0; z--) { + if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) { + break; + } + } + } + + //Work our way up the clusters removing them as we go if required + var cluster = marker.__cluster, + markers = cluster._markers, + otherMarker; + + //Remove the marker from the immediate parents marker list + this._arraySplice(markers, marker); + + while (cluster) { + cluster._childCount--; + + if (cluster._zoom < 0) { + //Top level, do nothing? + + } else if (removeFromDistanceGrid && cluster._childCount <= 1) { //Cluster no longer required + //We need to push the other marker up to the parent + otherMarker = cluster._markers[0] == marker ? cluster._markers[1] : cluster._markers[0]; + + if (!otherMarker) { + debugger; + } + + //Update distance grid + gridClusters[cluster._zoom].removeObject(cluster, map.project(cluster._cLatLng, cluster._zoom)); + gridUnclustered[cluster._zoom].addObject(otherMarker, map.project(otherMarker.getLatLng(), cluster._zoom)); + + //Move otherMarker up to parent + this._arraySplice(cluster._parent._childClusters, cluster); + cluster._parent._markers.push(otherMarker); + otherMarker.__cluster = cluster._parent; + + if (cluster._icon) { + //Cluster is currently on the map, need to put the marker on the map instead + L.FeatureGroup.prototype.removeLayer.call(this, cluster); + L.FeatureGroup.prototype.addLayer.call(this, otherMarker); + } + } else { + cluster._recalculateBounds(); + cluster._updateIcon(); + } + + cluster = cluster._parent; + } + }, + clearLayers: function () { //Need our own special implementation as the LayerGroup one doesn't work for us @@ -343,7 +416,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ closest = gridUnclustered[zoom].getNearObject(markerPoint); if (closest) { if (closest.__cluster) { - closest.__cluster._removeChildMarker(closest); + this._removeLayer(closest, false); } var parent = closest.__cluster || this._topClusterLevel; @@ -351,7 +424,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ console.log('creating new cluster with 2 markers at zoom ' + zoom); var newCluster = new L.MarkerCluster(this, zoom, closest, layer); - gridClusters[zoom].addObject(newCluster, this._map.project(closest.getLatLng(), zoom)); + gridClusters[zoom].addObject(newCluster, this._map.project(newCluster._cLatLng, zoom)); closest.__cluster = newCluster; layer.__cluster = newCluster;