[Pkg-javascript-commits] [leaflet-markercluster] 153/479: calculate point average as cluster center instead of bounds center

Jonas Smedegaard dr at jones.dk
Thu Oct 16 16:00:24 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 755cd3cb645063d639ae38e670835ba2165210b4
Author: Vladimir Agafonkin <agafonkin at gmail.com>
Date:   Thu Aug 9 18:10:20 2012 +0300

    calculate point average as cluster center instead of bounds center
---
 src/MarkerCluster.js | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js
index cc73b0c..5a3f887 100644
--- a/src/MarkerCluster.js
+++ b/src/MarkerCluster.js
@@ -44,7 +44,7 @@ L.MarkerCluster = L.Marker.extend({
 	_baseInit: function () {
 		L.Marker.prototype.initialize.call(this, this._latlng, { icon: this._group.options.iconCreateFunction(this._childCount) });
 	},
-	
+
 	_addChild: function (new1) {
 		if (new1 instanceof L.MarkerCluster) {
 			this._childClusters.push(new1);
@@ -63,13 +63,27 @@ L.MarkerCluster = L.Marker.extend({
 
 	_expandBounds: function (marker) {
 
+		var addedCount,
+		    addedLatLng;
+
 		if (marker instanceof L.MarkerCluster) {
 			this._bounds.extend(marker._bounds);
+			addedCount = marker._childCount;
+			addedLatLng = marker._latlng;
 		} else {
-			this._bounds.extend(marker.getLatLng());
+			addedLatLng = marker.getLatLng();
+			this._bounds.extend(addedLatLng);
+			addedCount = 1;
 		}
 
-		this._latlng = this._bounds.getCenter();
+		var totalCount = this._childCount + addedCount;
+
+		if (!this._latlng) {
+			this._latlng = addedLatLng;
+		} else {
+			this._latlng.lat = (addedLatLng.lat * addedCount + this._latlng.lat * this._childCount) / totalCount;
+			this._latlng.lng = (addedLatLng.lng * addedCount + this._latlng.lng * this._childCount) / totalCount;
+		}
 	},
 
 	//Set our markers position as given and add it to the map
@@ -187,7 +201,7 @@ L.MarkerCluster = L.Marker.extend({
 				markers.splice(i, 1);
 				this._childCount--;
 				this._recalculateBounds();
-		
+
 				if (!('_zoom' in this)) {
 					this.setIcon(group.options.iconCreateFunction(this._childCount));
 				}
@@ -213,7 +227,7 @@ L.MarkerCluster = L.Marker.extend({
 						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);
@@ -345,7 +359,7 @@ L.MarkerCluster = L.Marker.extend({
 			delete this._backupLatlng;
 		}
 	},
-	
+
 	//exceptBounds: If set, don't remove any markers/clusters in it
 	_recursivelyRemoveChildrenFromMap: function (previousBounds, depth, exceptBounds) {
 		var m, i;
@@ -438,4 +452,4 @@ L.MarkerCluster = L.Marker.extend({
 		//Don't need to check this._markers as the rest won't work if there are any
 		return this._childClusters.length > 0 && this._childClusters[0]._childCount === this._childCount;
 	}
-});
\ No newline at end of file
+});

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