diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js index 310acffd7..c98715bbf 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 e14bd785b..2c4e893c6 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); }