[Pkg-javascript-commits] [leaflet-markercluster] 62/479: More work towards _recursivelyAddLayer. Still some bugs

Jonas Smedegaard dr at jones.dk
Thu Oct 16 16:00:10 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 a39f71fad5588f17a6b042ddb50df365bc6bcafd
Author: danzel <danzel at localhost.geek.nz>
Date:   Fri Jul 20 14:44:21 2012 +1200

    More work towards _recursivelyAddLayer. Still some bugs
---
 src/MarkerCluster.js      | 12 +++++++++++-
 src/MarkerClusterGroup.js | 45 ++++++++++++++++++++++++++++++---------------
 2 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js
index 3607ace..c2e5f10 100644
--- a/src/MarkerCluster.js
+++ b/src/MarkerCluster.js
@@ -82,6 +82,7 @@ L.MarkerCluster = L.Marker.extend({
 			added = false;
 
 		if (!this._haveGeneratedChildClusters && this._canAcceptPosition(layer.getLatLng(), zoom)) {
+			//Don't need to cluster it in as we haven't clustered
 			this._addChild(layer);
 			added = true;
 		} else {
@@ -97,9 +98,18 @@ L.MarkerCluster = L.Marker.extend({
 				}
 			}
 
+			//Couldn't add it to a child, but it should be part of us
 			if (!added && this._canAcceptPosition(layer.getLatLng(), zoom)) {
+
 				//Add to ourself instead
-				this._addChild(layer); //TODO: This should be done in a clustering aware way
+				var newCluster = this._group._clusterOne(this._markers, layer, zoom);
+				if (newCluster && newCluster._markers[0]._icon) {
+					//Remove children and add cluster
+					newCluster._recursivelyRemoveChildrenFromMap(newCluster._bounds, 0);
+					newCluster._addToMap();
+				}
+				this._addChild(newCluster || layer);
+
 				added = true;
 			}
 
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index 508a142..8260595 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -176,6 +176,29 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 		this._map.on('moveend', this._moveEnd, this);
 	},
 
+	//Takes a list of markers and clusters the new marker in to them
+	//Will return null or the new MarkerCluster. The clustered in marker is removed from the given array
+	_clusterOne: function (unclusteredMarkers, newMarker, zoom) {
+		var markerPos = newMarker._projCenter || this._map.project(newMarker.getLatLng(), zoom),
+			clusterRadiusSqrd = this.options.maxClusterRadius * this.options.maxClusterRadius,
+			i, m, mPos;
+
+		for (i = unclusteredMarkers.length - 1; i >= 0; i--) {
+			m = unclusteredMarkers[i];
+			mPos = m._projCenter || this._map.project(m.getLatLng(), zoom);
+
+			if (this._sqDist(markerPos, mPos) <= clusterRadiusSqrd) {
+				//Create a new cluster with these 2
+				var newCluster = new L.MarkerCluster(this, m, newMarker);
+
+				unclusteredMarkers.splice(i, 1);
+				return newCluster;
+			}
+		}
+
+		return null;
+	},
+
 	//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
@@ -208,21 +231,13 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 
 			//otherwise, look through all of the markers we haven't managed to cluster and see if we should form a cluster with them
 			if (!used) {
-				for (j = 0 ; j < unclustered.length; j++) {
-					if (this._sqDist(point._projCenter, unclustered[j]._projCenter) <= clusterRadiusSqrd) {
-						//Create a new cluster with these 2
-						var newCluster = new L.MarkerCluster(this, point, unclustered[j]);
-						newCluster._haveGeneratedChildClusters = hasChildClusters;
-						clusters.push(newCluster);
-
-						newCluster._projCenter = this._map.project(newCluster.getLatLng(), zoom);
-
-						unclustered.splice(j, 1);
-						used = true;
-						break;
-					}
-				}
-				if (!used) {
+				var newCluster = this._clusterOne(unclustered, point, hasChildClusters);
+				if (newCluster) {
+					newCluster._haveGeneratedChildClusters = hasChildClusters;
+					newCluster._projCenter = this._map.project(newCluster.getLatLng(), zoom);
+					clusters.push(newCluster);
+				} else {
+					//Didn't manage to use it
 					unclustered.push(point);
 				}
 			}

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