[Pkg-javascript-commits] [leaflet-markercluster] 69/479: Implement generating new clusters as required when zooming down

Jonas Smedegaard dr at jones.dk
Thu Oct 16 16:00:11 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 96b4d8b73e91ecde2dc8b80d889f2839f8f22948
Author: danzel <danzel at localhost.geek.nz>
Date:   Fri Jul 20 16:42:39 2012 +1200

    Implement generating new clusters as required when zooming down
---
 src/MarkerCluster.js      | 31 +++++++++++++++++++++++++++++++
 src/MarkerClusterGroup.js |  6 +++++-
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js
index 310acff..c98715b 100644
--- a/src/MarkerCluster.js
+++ b/src/MarkerCluster.js
@@ -351,6 +351,10 @@ L.MarkerCluster = L.Marker.extend({
 		//TODO: When zooming down we need to generate new clusters for levels that don't have them yet
 
 		if (depthToStartAt > 0) { //Still going down to required depth, just recurse to child clusters
+			if (!this._haveGeneratedChildClusters) {
+				this._generateChildClusters();
+			}
+
 			for (i = childClusters.length - 1; i >= 0; i--) {
 				c = childClusters[i];
 				if (boundsToApplyTo.intersects(c._bounds)) {
@@ -378,6 +382,33 @@ L.MarkerCluster = L.Marker.extend({
 		}
 	},
 
+	_generateChildClusters: function () {
+		var res = this._group._cluster(this._markers, this._zoomForCluster),
+			unclustered = res.unclustered,
+			clusters = res.clusters,
+			i;
+
+		var oldMarkers = this._markers;
+		var old = this._childCount;
+
+		this._markers = [];
+		this._childCount = 0;
+
+		for (i = unclustered.length - 1; i >= 0; i--) {
+			this._addChild(unclustered[i]);
+		}
+		for (i = clusters.length - 1; i >= 0; i--) {
+			this._addChild(clusters[i]);
+		}
+
+		if (this._childCount != old) {
+			debugger;
+		}
+
+		delete this._zoomForCluster;
+		this._haveGeneratedChildClusters = true;
+	},
+
 	_recalculateBounds: function () {
 		var markers = this._markers,
 			childClusters = this._childClusters,
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index e14bd78..2c4e893 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -181,7 +181,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 	//Takes a list of objects that have a 'getLatLng()' function (Marker / MarkerCluster)
 	//Performs clustering on them (using a greedy algorithm) and returns those clusters.
 	//toCluster: List of Markers/MarkerClusters to cluster. MarkerClusters MUST be first in the list
-	//Returns FIXME TODO
+	//Returns { 'clusters': [new clusters], 'unclustered': [unclustered markers] }
 	_cluster: function (toCluster, zoom) {
 		var clusterRadiusSqrd = this.options.maxClusterRadius * this.options.maxClusterRadius,
 		    clusters = [],
@@ -213,6 +213,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 				var newCluster = this._clusterOne(unclustered, point);
 				if (newCluster) {
 					newCluster._haveGeneratedChildClusters = hasChildClusters;
+					if (!hasChildClusters) {
+						newCluster._zoomForCluster = zoom + 1;
+					}
 					newCluster._projCenter = this._map.project(newCluster.getLatLng(), zoom);
 					clusters.push(newCluster);
 				} else {
@@ -229,6 +232,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 
 			if (c instanceof L.MarkerCluster) {
 				var nc = new L.MarkerCluster(this, c);
+				nc._haveGeneratedChildClusters = true;
 				clusters.push(nc);
 				unclustered.splice(i, 1);
 			}

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