[Pkg-javascript-commits] [leaflet-markercluster] 60/479: Fixing up _recursivelyAddLayer to work correctly. This supports adding markers to existing clusters better.

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 96e3a8cbf71368fd41d2df9119cb38541ddfc053
Author: danzel <danzel at localhost.geek.nz>
Date:   Fri Jul 20 14:07:31 2012 +1200

    Fixing up _recursivelyAddLayer to work correctly. This supports adding markers to existing clusters better.
---
 src/MarkerCluster.js      | 51 ++++++++++++++++++++++++++++++++++-------------
 src/MarkerClusterGroup.js |  4 ++--
 2 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js
index 3f99495..3607ace 100644
--- a/src/MarkerCluster.js
+++ b/src/MarkerCluster.js
@@ -77,35 +77,58 @@ L.MarkerCluster = L.Marker.extend({
 	},
 
 	//layer: The layer to try add
-	_recursivelyAddLayer: function (layer) {
-		var childReturn = null;
+	_recursivelyAddLayer: function (layer, zoom) {
+		var childReturn = null,
+			added = false;
 
-		if (!this._haveGeneratedChildClusters) {
+		if (!this._haveGeneratedChildClusters && this._canAcceptPosition(layer.getLatLng(), zoom)) {
 			this._addChild(layer);
+			added = true;
 		} 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._recursivelyAddLayer(layer);
-					addedToChild = true;
-					break;
+				//Recurse into children where their bounds fits the layer or they can just take it
+				if (c._bounds.contains(layer.getLatLng()) || c._canAcceptPosition(layer.getLatLng(), zoom + 1)) {
+					childReturn = c._recursivelyAddLayer(layer, zoom + 1);
+					if (childReturn !== false) {
+						added = true;
+						break;
+					}
 				}
 			}
 
-			if (!addedToChild) {
+			if (!added && this._canAcceptPosition(layer.getLatLng(), zoom)) {
 				//Add to ourself instead
 				this._addChild(layer); //TODO: This should be done in a clustering aware way
+				added = true;
 			}
 
-			this._childCount++;
-			if (this._icon) {
-				this.setIcon(this._group.options.iconCreateFunction(this._childCount));
+			if (added) {
+				this._childCount++;
+				if (this._icon) {
+					this.setIcon(this._group.options.iconCreateFunction(this._childCount));
+				}
+			}
+		}
+
+		if (!added) {
+			if (this._zoom) { //Means we are the root node, we get it anyway
+				this._addChild(layer);
 			}
+			console.log('not added');
+			return false;
+		} else {
+			console.log('added ' + (this._icon ? this._latlng : childReturn));
+			return this._icon ? this._latlng : childReturn;
 		}
+	},
+
+	_canAcceptPosition: function (latlng, zoom) {
+		var clusterRadiusSqrd = this._group.options.maxClusterRadius * this._group.options.maxClusterRadius,
+			pos = this._group._map.project(this._latlng, zoom),
+			otherpos = this._group._map.project(latlng, zoom);
 
-		console.log('added ' + (this._icon ? this._latlng : childReturn));
-		return this._icon ? this._latlng : childReturn;
+		return (this._group._sqDist(pos, otherpos) <= clusterRadiusSqrd);
 	},
 
 	//Removes the given node from this marker cluster (or its child as required)
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index 1566efc..2fa2dec 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -53,7 +53,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 		if (this._inZoomAnimation > 0) {
 			return;
 		}
-		return; //FIXME OBV
+		return; //TODO FIXME OBV
 		var l, i,
 		    layers = this._layers,
 		    bounds = this._getExpandedVisibleBounds(),
@@ -135,7 +135,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 		//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
 
-		position = this._topClusterLevel._recursivelyAddLayer(layer);
+		position = this._topClusterLevel._recursivelyAddLayer(layer, this._topClusterLevel._zoom);
 
 		if (position) {
 			//TODO Tidy up

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