[Pkg-javascript-commits] [leaflet-markercluster] 58/479: removeLayer mostly works! Sometimes the resulting markers don't end up in parent clusters. (Hit remove 1 lots then zoom out lots)

Jonas Smedegaard dr at jones.dk
Thu Oct 16 16:00:09 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository leaflet-markercluster.

commit 603e985d58b410172c8102a0dc5a8301b328409a
Author: danzel <danzel at localhost.geek.nz>
Date:   Fri Jul 20 11:30:20 2012 +1200

    removeLayer mostly works! Sometimes the resulting markers don't end up in parent clusters. (Hit remove 1 lots then zoom out lots)
---
 src/MarkerCluster.js      | 20 +++++++++++++++-----
 src/MarkerClusterGroup.js | 45 +--------------------------------------------
 2 files changed, 16 insertions(+), 49 deletions(-)

diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js
index 769bf75..080a94b 100644
--- a/src/MarkerCluster.js
+++ b/src/MarkerCluster.js
@@ -111,7 +111,8 @@ L.MarkerCluster = L.Marker.extend({
 	//Removes the given node from this marker cluster (or its child as required)
 	//Returns true if it (or a child cluster) removes the marker
 	_recursivelyRemoveLayer: function (layer) {
-		var markers = this._markers,
+		var group = this._group,
+			markers = this._markers,
 			childClusters = this._childClusters,
 			i;
 
@@ -123,7 +124,7 @@ L.MarkerCluster = L.Marker.extend({
 		
 				this._childCount--;
 				if (this._icon) {
-					this.setIcon(this._group.options.iconCreateFunction(this._childCount));
+					this.setIcon(group.options.iconCreateFunction(this._childCount));
 				}
 				return true;
 			}
@@ -132,19 +133,28 @@ L.MarkerCluster = L.Marker.extend({
 		//Otherwise check our childClusters
 		for (i = childClusters.length - 1; i >= 0; i--) {
 			var child = childClusters[i];
-			if (child._recursivelyRemoveChildMarker(layer)) {
+
+			if (child._bounds.contains(layer._latlng) && child._recursivelyRemoveLayer(layer)) {
 				this._childCount--;
 
 				//if our child cluster is no longer a cluster, remove it and replace with just the marker
 				if (child._childCount === 1) {
+
+					//If the child is visible, remove it and put the marker on the map
+					if (child._icon) {
+						L.FeatureGroup.prototype.removeLayer.call(group, child);
+						L.FeatureGroup.prototype.addLayer.call(group, child._markers[0]);
+					}
+					
+					//Take ownership of its only marker and bin the cluster
 					markers.push(child._markers[0]);
 					childClusters.splice(i, 1);
 				}
 
 				this._recalculateBounds();
 
-				if (this._icon) {
-					this.setIcon(this._group.options.iconCreateFunction(this._childCount));
+				if (this._icon && this._childCount > 1) { //No need to update if we are getting removed anyway
+					this.setIcon(group.options.iconCreateFunction(this._childCount));
 				}
 				return true;
 			}
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index dc471a0..1566efc 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -163,50 +163,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 	},
 
 	removeLayer: function (layer) {
-		var current = this._markersAndClustersAtZoom[this._map._zoom],
-			i = current.unclustered.indexOf(layer),
-			killParents = false;
-
-		//TODO: This whole thing could probably be better
-
-		//Remove the marker
-		L.FeatureGroup.prototype.removeLayer.call(this, layer);
-
-		if (i !== -1) { //Is unclustered at the current zoom level
-			current.unclustered.splice(i, 1);
-			
-			killParents = true; //Need to rebuild parents as they may be clusters just because this marker makes them one
-		} else { //it is a child of a cluster
-			//Find the cluster
-			for (i = current.clusters.length - 1; i >= 0; i--) {
-				var c = current.clusters[i];
-				
-				if (c._recursivelyRemoveChildMarker(layer)) {
-					if (c._childCount == 1) {
-						//Remove cluster and add individual marker
-						L.FeatureGroup.prototype.removeLayer.call(this, c);
-						var marker = c._markers[0];
-						L.FeatureGroup.prototype.addLayer.call(this, marker);
-						current.unclustered.push(marker);
-						current.clusters.splice(i, 1);
-						killParents = true; //Need to rebuild parents as they could have references to this cluster
-					}
-					console.log('remaining clusters ' + current.clusters.length);
-					console.log('remaining markers' + current.unclustered.length);
-					break;
-				}
-			}
-		}
-
-		if (killParents) {
-			//blow away all parent levels as they are now wrong
-			for (i in this._markersAndClustersAtZoom)
-			{
-				if (i > this._map._zoom) {
-					delete this._markersAndClustersAtZoom[i];
-				}
-			}
-		}
+		this._topClusterLevel._recursivelyRemoveLayer(layer);
 
 		return this;
 	},

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet-markercluster.git



More information about the Pkg-javascript-commits mailing list