Implement generating new clusters as required when zooming down

This commit is contained in:
danzel
2012-07-20 16:42:39 +12:00
parent 643757df40
commit 96b4d8b73e
2 changed files with 36 additions and 1 deletions
+31
View File
@@ -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,
+5 -1
View File
@@ -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);
}