From 43a1742b89afbfece07f78aec5c9d3120a30b569 Mon Sep 17 00:00:00 2001 From: danzel Date: Wed, 25 Jul 2012 16:32:23 +1200 Subject: [PATCH] Fix addLayer (was broken since making everything in to a tree) --- src/MarkerCluster.js | 34 +++++++++++++++++++++++++++++----- src/MarkerClusterGroup.js | 10 ++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js index e5109022b..37012ec98 100644 --- a/src/MarkerCluster.js +++ b/src/MarkerCluster.js @@ -81,9 +81,9 @@ L.MarkerCluster = L.Marker.extend({ //layer: The layer to try add //returns: - // true: was able to put this marker in, but don't know its current visible parents position + // true: was able to put this marker in, but don't know its current visible parents position (If returned externally, add this marker at its position) // false: wasn't able to put this marker in - // a Marker/MarkerCluster: the visible parent of the marker (or the marker itself if it should be visible) + // a MarkerCluster: the visible parent of the marker _recursivelyAddLayer: function (layer, zoom) { var result = false; @@ -103,11 +103,37 @@ L.MarkerCluster = L.Marker.extend({ if (!result && (this._canAcceptPosition(layer.getLatLng(), zoom) || this._zoom)) { //Add to ourself instead - result = this._group._clusterOne(this._markers, layer, zoom); + result = this._group._clusterOne(this._markers, layer, zoom + 1); if (result) { result._baseInit(); + this._childCount--; this._addChild(result); + + //We may be above the zoom that these 2 markers would initially cluster at + // so push the new cluster as deep as it can go + var wantedZoom = this._group._map.getZoom() - 1, + maxZoom = this._group._map.getMaxZoom(), + newResult, + finalResult = (zoom === wantedZoom) ? result : true; + while (zoom < maxZoom) { + zoom++; + newResult = this._group._clusterOne([result._markers[0]], layer, zoom + 1); + + if (newResult == null) { + break; + } + newResult._baseInit(); + result._markers = []; + result._childClusters.push(newResult); + result = newResult; + + if (zoom == wantedZoom) { + finalResult = result; + } + } + result = finalResult; + } else { this._addChild(layer); result = true; @@ -123,8 +149,6 @@ L.MarkerCluster = L.Marker.extend({ if (result === true) { if (this._icon) { result = this; - } else if ((this._markers.length > 0 && this._markers[0]._icon) || (this._childClusters.length > 1 && this._childClusters[0]._icon)) { - result = layer; } } diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 6f57ab09c..0577f0955 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -257,9 +257,11 @@ L.MarkerClusterGroup.include(!L.DomUtil.TRANSITION ? { this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel - this._topClusterLevel._zoom + 1, this._getExpandedVisibleBounds()); }, _animationAddLayer: function (layer, newCluster) { - L.FeatureGroup.prototype.addLayer.call(this, layer); - if (newCluster !== true && newCluster._childCount === 2) { - newCluster._recursivelyRemoveChildrenFromMap(newCluster._bounds, 1); + if (newCluster === true) { + L.FeatureGroup.prototype.addLayer.call(this, layer); + } else if (newCluster._childCount === 2) { + newCluster._addToMap(); + newCluster._recursivelyRemoveChildrenFromMap(newCluster._bounds, this._map.getMaxZoom()); //getMaxZoom will always get all children } } } : { @@ -398,7 +400,7 @@ L.MarkerClusterGroup.include(!L.DomUtil.TRANSITION ? { this._forceLayout(); me._animationStart(); - me._animationZoomOutSingle(newCluster, 0, 1); + me._animationZoomOutSingle(newCluster, 0, this._map.getMaxZoom()); } } },