diff --git a/example/marker-clustering.html b/example/marker-clustering.html index 63bfbf7ac..e7b9a665e 100644 --- a/example/marker-clustering.html +++ b/example/marker-clustering.html @@ -58,7 +58,8 @@
- + + diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js index 72b35c0a9..9a34e86f2 100644 --- a/src/MarkerCluster.js +++ b/src/MarkerCluster.js @@ -69,6 +69,45 @@ L.MarkerCluster = L.Marker.extend({ L.FeatureGroup.prototype.addLayer.call(this._group, this); }, + //Removes the given node from this marker cluster (or its child as required) + //Returns true if it (or its childCluster) removes the marker + _recursivelyRemoveChildMarker: function(layer) { + var markers = this._markers, + childClusters = this._childClusters, + newChildCount = 0, + i; + + //Check our children + for (i = markers.length - 1; i >= 0; i--) { + if (markers[i] == layer) { + markers.splice(i, 1); + //TODO? Recalculate bounds + + this._childCount--; + if (this._icon) { + this.setIcon(this._group.options.iconCreateFunction(this._childCount)); + } + return true; + } + } + + //Otherwise check our childClusters + for (i = childClusters.length - 1; i >= 0; i--) { + if (childClusters[i]._recursivelyRemoveChildMarker(layer)) { + this._childCount--; + //TODO: If child is now 1 then remove it and add a marker + //TODO? Recalculate bounds + + if (this._icon) { + this.setIcon(this._group.options.iconCreateFunction(this._childCount)); + } + return true; + } + } + + return false; + }, + _recursivelyAnimateChildrenIn: function (center, depth) { var markers = this._markers, markersLength = markers.length, diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 0a846d432..4f94c171f 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -181,6 +181,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ 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 this._highestZoom = this._zoom = zoom; this._markersAndClustersAtZoom = {}; this._markersAndClustersAtZoom[zoom] = newClusters; @@ -211,6 +212,41 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ }, removeLayer: function (layer) { + var current = this._markersAndClustersAtZoom[this._map._zoom], + i = current.unclustered.indexOf(layer), + cluster, result; + + //Remove the marker + L.FeatureGroup.prototype.removeLayer.call(this, layer); + + if (i !== -1) //Is unclustered at the current zoom level + { + current.unclustered.splice(i, 1); + + //blow away all parent levels as they are now wrong + for (i in this._markersAndClustersAtZoom) + { + if (i > this._map._zoom) { + delete this._markersAndClustersAtZoom[i]; + } + } + //TODO: This could probably use something like below and then remove the marker from the map + } + else //it is a child of a cluster + { + //Find the cluster + for (i = current.clusters.length - 1; i >= 0; i--) { + var c = current.clusters[i]; + + if (c._recursivelyRemoveChildMarker(layer)) { + if (c._childCount == 1) { + //TODO remove cluster and add individual marker + } + break; + } + }; + } + //TODO Hrm.... //Will need to got through each cluster and find the marker, removing it as required, possibly turning its parent from a cluster into an individual marker.