Starting on addLayer after clustering, mostly works now, stops working after a zoom out

This commit is contained in:
danzel
2012-07-20 09:40:34 +12:00
parent d1b1df7cae
commit 6a55a17e95
2 changed files with 56 additions and 28 deletions
+32
View File
@@ -76,6 +76,38 @@ L.MarkerCluster = L.Marker.extend({
L.FeatureGroup.prototype.addLayer.call(this._group, this); L.FeatureGroup.prototype.addLayer.call(this._group, this);
}, },
//layer: The layer to try add
_recursivelyAddChildMarker: function (layer) {
var childReturn = null;
if (!this._haveGeneratedChildClusters) {
this._addChild(layer);
} 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._recursivelyAddChildMarker(layer);
addedToChild = true;
break;
}
}
if (!addedToChild) {
//Add to ourself instead
this._addChild(layer); //TODO: This should be done in a clustering aware way
}
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;
},
//Removes the given node from this marker cluster (or its child as required) //Removes the given node from this marker cluster (or its child as required)
//Returns true if it (or a child cluster) removes the marker //Returns true if it (or a child cluster) removes the marker
_recursivelyRemoveChildMarker: function(layer) { _recursivelyRemoveChildMarker: function(layer) {
+26 -30
View File
@@ -117,6 +117,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
while (this._topClusterLevel._zoom > this._map._zoom) { while (this._topClusterLevel._zoom > this._map._zoom) {
console.log('generating new topCluster for ' + (this._topClusterLevel._zoom - 1)); console.log('generating new topCluster for ' + (this._topClusterLevel._zoom - 1));
this._topClusterLevel = this._clusterToMarkerCluster([], [], this._topClusterLevel._childClusters.concat(this._topClusterLevel._markers), this._topClusterLevel._zoom - 1); this._topClusterLevel = this._clusterToMarkerCluster([], [], this._topClusterLevel._childClusters.concat(this._topClusterLevel._markers), this._topClusterLevel._zoom - 1);
this._topClusterLevel = this._clusterToMarkerCluster([], [], this._topClusterLevel._childClusters.concat(this._topClusterLevel._markers), this._topClusterLevel._zoom - 1);
} }
this._animationZoomOut(this._zoom, this._map._zoom); this._animationZoomOut(this._zoom, this._map._zoom);
@@ -129,41 +130,35 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
return this; return this;
} }
var me = this,
position;
//TODO: If we have already clustered we'll need to add this one to a cluster //If we have already clustered we'll need to add this one to a cluster
//Should be able to use _cluster with the current clusters and just this layer L.FeatureGroup.prototype.addLayer.call(this, layer); //TODO: If not animated maybe don't add it yet
L.FeatureGroup.prototype.addLayer.call(this, layer);
var zoom = this._map._zoom, position = this._topClusterLevel._recursivelyAddChildMarker(layer);
current = this._markersAndClustersAtZoom[zoom],
newClusters = this._cluster(current.clusters, current.unclustered, [layer], zoom);
//TODO: At the moment we blow away all other zoom levels, but really we could probably get away with not doing that if (position) {
this._highestZoom = this._zoom = zoom; //TODO Tidy up
this._markersAndClustersAtZoom = {}; //this._animateSingleMarkerIn(layer, position);
this._markersAndClustersAtZoom[zoom] = newClusters;
var bounds = this._getExpandedVisibleBounds();
for (var i = 0; i < newClusters.clusters.length; i++) {
var c = newClusters.clusters[i];
//Flatten all child clusters as they are now wrong
c._markers = c.getAllChildMarkers();
c._childClusters = [];
}
var me = this;
setTimeout(function () { setTimeout(function () {
me._animationStart(); me._animationStart.call(me);
for (var j = 0; j < newClusters.clusters.length; j++) {
var v = newClusters.clusters[j]; var backupLatlng = layer.getLatLng();
if (v._icon) {
v.setLatLng(v._latlng); layer.setLatLng(position);
}
} setTimeout(function () {
me._animationZoomOut(newClusters.clusters, 1); L.FeatureGroup.prototype.removeLayer.call(me, layer);
layer.setLatLng(backupLatlng);
//HACKS
map._mapPane.className = map._mapPane.className.replace(' leaflet-cluster-anim', ''); me._inZoomAnimation--;
}, 250);
}, 0); }, 0);
} else {
//Do nothing, marker should be shown on map at own position at this level
}
return this; return this;
}, },
@@ -321,6 +316,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
} }
console.log('generated clusters: ' + result._childClusters.length + ", markers: " + result._markers.length + " at " + zoom); console.log('generated clusters: ' + result._childClusters.length + ", markers: " + result._markers.length + " at " + zoom);
result._zoom = zoom; result._zoom = zoom;
result._haveGeneratedChildClusters = true;
return result; return result;
}, },