[Pkg-javascript-commits] [leaflet-markercluster] 55/479: Starting on addLayer after clustering, mostly works now, stops working after a zoom out

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 6a55a17e955153a80ee3c8d9cd74645927138cf3
Author: danzel <danzel at localhost.geek.nz>
Date:   Fri Jul 20 09:40:34 2012 +1200

    Starting on addLayer after clustering, mostly works now, stops working after a zoom out
---
 src/MarkerCluster.js      | 32 +++++++++++++++++++++++++++++
 src/MarkerClusterGroup.js | 52 ++++++++++++++++++++++-------------------------
 2 files changed, 56 insertions(+), 28 deletions(-)

diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js
index e84b14c..039ab81 100644
--- a/src/MarkerCluster.js
+++ b/src/MarkerCluster.js
@@ -76,6 +76,38 @@ L.MarkerCluster = L.Marker.extend({
 		L.FeatureGroup.prototype.addLayer.call(this._group, this);
 	},
 
+	//layer: The layer to try add
+	_recursivelyAddChildMarker: function (layer) {
+		var childReturn = null;
+
+		if (!this._haveGeneratedChildClusters) {
+			this._addChild(layer);
+		} else {
+			var addedToChild = false;
+			for (var i = this._childClusters.length - 1; i >= 0; i--) {
+				var c = this._childClusters[i];
+				if (c._bounds.contains(layer.getLatLng())) { //TODO: Use a layer distance calculation
+					childReturn = c._recursivelyAddChildMarker(layer);
+					addedToChild = true;
+					break;
+				}
+			}
+
+			if (!addedToChild) {
+				//Add to ourself instead
+				this._addChild(layer); //TODO: This should be done in a clustering aware way
+			}
+
+			this._childCount++;
+			if (this._icon) {
+				this.setIcon(this._group.options.iconCreateFunction(this._childCount));
+			}
+		}
+
+		console.log('added ' + (this._icon ? this._latlng : childReturn));
+		return this._icon ? this._latlng : childReturn;
+	},
+
 	//Removes the given node from this marker cluster (or its child as required)
 	//Returns true if it (or a child cluster) removes the marker
 	_recursivelyRemoveChildMarker: function(layer) {
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index 41031a1..8ca90be 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -117,6 +117,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 			while (this._topClusterLevel._zoom > this._map._zoom) {
 				console.log('generating new topCluster for ' + (this._topClusterLevel._zoom - 1));
 				this._topClusterLevel = this._clusterToMarkerCluster([], [], this._topClusterLevel._childClusters.concat(this._topClusterLevel._markers), this._topClusterLevel._zoom - 1);
+				this._topClusterLevel = this._clusterToMarkerCluster([], [], this._topClusterLevel._childClusters.concat(this._topClusterLevel._markers), this._topClusterLevel._zoom - 1);
 			}
 
 			this._animationZoomOut(this._zoom, this._map._zoom);
@@ -129,41 +130,35 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 			return this;
 		}
 
+		var me = this,
+			position;
 
-		//TODO: If we have already clustered we'll need to add this one to a cluster
-		//Should be able to use _cluster with the current clusters and just this layer
-		L.FeatureGroup.prototype.addLayer.call(this, layer);
+		//If we have already clustered we'll need to add this one to a cluster
+		L.FeatureGroup.prototype.addLayer.call(this, layer); //TODO: If not animated maybe don't add it yet
 
-		var zoom = this._map._zoom,
-		    current = this._markersAndClustersAtZoom[zoom],
-		    newClusters = this._cluster(current.clusters, current.unclustered, [layer], zoom);
+		position = this._topClusterLevel._recursivelyAddChildMarker(layer);
 
-		//TODO: At the moment we blow away all other zoom levels, but really we could probably get away with not doing that
-		this._highestZoom = this._zoom = zoom;
-		this._markersAndClustersAtZoom = {};
-		this._markersAndClustersAtZoom[zoom] = newClusters;
+		if (position) {
+			//TODO Tidy up
+			//this._animateSingleMarkerIn(layer, position);
+			setTimeout(function () {
+				me._animationStart.call(me);
 
-		var bounds = this._getExpandedVisibleBounds();
+				var backupLatlng = layer.getLatLng();
 
-		for (var i = 0; i < newClusters.clusters.length; i++) {
-			var c = newClusters.clusters[i];
+				layer.setLatLng(position);
 
-			//Flatten all child clusters as they are now wrong
-			c._markers = c.getAllChildMarkers();
-			c._childClusters = [];
-		}
+				setTimeout(function () {
+					L.FeatureGroup.prototype.removeLayer.call(me, layer);
+					layer.setLatLng(backupLatlng);
 
-		var me = this;
-		setTimeout(function () {
-			me._animationStart();
-			for (var j = 0; j < newClusters.clusters.length; j++) {
-				var v = newClusters.clusters[j];
-				if (v._icon) {
-					v.setLatLng(v._latlng);
-				}
-			}
-			me._animationZoomOut(newClusters.clusters, 1);
-		}, 0);
+					//HACKS
+					map._mapPane.className = map._mapPane.className.replace(' leaflet-cluster-anim', ''); me._inZoomAnimation--;
+				}, 250);
+			}, 0);
+		} else {
+			//Do nothing, marker should be shown on map at own position at this level
+		}
 
 		return this;
 	},
@@ -321,6 +316,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 		}
 		console.log('generated clusters: ' + result._childClusters.length + ", markers: " + result._markers.length + " at " + zoom);
 		result._zoom = zoom;
+		result._haveGeneratedChildClusters = true;
 		return result;
 	},
 

-- 
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