From 96e3a8cbf71368fd41d2df9119cb38541ddfc053 Mon Sep 17 00:00:00 2001 From: danzel Date: Fri, 20 Jul 2012 14:07:31 +1200 Subject: [PATCH] Fixing up _recursivelyAddLayer to work correctly. This supports adding markers to existing clusters better. --- src/MarkerCluster.js | 51 ++++++++++++++++++++++++++++----------- src/MarkerClusterGroup.js | 4 +-- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js index 3f9949534..3607acee5 100644 --- a/src/MarkerCluster.js +++ b/src/MarkerCluster.js @@ -77,35 +77,58 @@ L.MarkerCluster = L.Marker.extend({ }, //layer: The layer to try add - _recursivelyAddLayer: function (layer) { - var childReturn = null; + _recursivelyAddLayer: function (layer, zoom) { + var childReturn = null, + added = false; - if (!this._haveGeneratedChildClusters) { + if (!this._haveGeneratedChildClusters && this._canAcceptPosition(layer.getLatLng(), zoom)) { this._addChild(layer); + added = true; } else { - var addedToChild = false; for (var i = this._childClusters.length - 1; i >= 0; i--) { var c = this._childClusters[i]; - if (c._bounds.contains(layer.getLatLng())) { //TODO: Use a layer distance calculation - childReturn = c._recursivelyAddLayer(layer); - addedToChild = true; - break; + //Recurse into children where their bounds fits the layer or they can just take it + if (c._bounds.contains(layer.getLatLng()) || c._canAcceptPosition(layer.getLatLng(), zoom + 1)) { + childReturn = c._recursivelyAddLayer(layer, zoom + 1); + if (childReturn !== false) { + added = true; + break; + } } } - if (!addedToChild) { + if (!added && this._canAcceptPosition(layer.getLatLng(), zoom)) { //Add to ourself instead this._addChild(layer); //TODO: This should be done in a clustering aware way + added = true; } - this._childCount++; - if (this._icon) { - this.setIcon(this._group.options.iconCreateFunction(this._childCount)); + if (added) { + this._childCount++; + if (this._icon) { + this.setIcon(this._group.options.iconCreateFunction(this._childCount)); + } } } - console.log('added ' + (this._icon ? this._latlng : childReturn)); - return this._icon ? this._latlng : childReturn; + if (!added) { + if (this._zoom) { //Means we are the root node, we get it anyway + this._addChild(layer); + } + console.log('not added'); + return false; + } else { + console.log('added ' + (this._icon ? this._latlng : childReturn)); + return this._icon ? this._latlng : childReturn; + } + }, + + _canAcceptPosition: function (latlng, zoom) { + var clusterRadiusSqrd = this._group.options.maxClusterRadius * this._group.options.maxClusterRadius, + pos = this._group._map.project(this._latlng, zoom), + otherpos = this._group._map.project(latlng, zoom); + + return (this._group._sqDist(pos, otherpos) <= clusterRadiusSqrd); }, //Removes the given node from this marker cluster (or its child as required) diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 1566efcb0..2fa2dec0b 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -53,7 +53,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ if (this._inZoomAnimation > 0) { return; } - return; //FIXME OBV + return; //TODO FIXME OBV var l, i, layers = this._layers, bounds = this._getExpandedVisibleBounds(), @@ -135,7 +135,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ //If we have already clustered we'll need to add this one to a cluster L.FeatureGroup.prototype.addLayer.call(this, layer); //TODO: If not animated maybe don't add it yet - position = this._topClusterLevel._recursivelyAddLayer(layer); + position = this._topClusterLevel._recursivelyAddLayer(layer, this._topClusterLevel._zoom); if (position) { //TODO Tidy up