Fixing up _recursivelyAddLayer to work correctly. This supports adding markers to existing clusters better.

This commit is contained in:
danzel
2012-07-20 14:07:31 +12:00
parent 003fceab14
commit 96e3a8cbf7
2 changed files with 39 additions and 16 deletions
+37 -14
View File
@@ -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)
+2 -2
View File
@@ -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