diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js index a47749628..bb6386dad 100644 --- a/src/MarkerCluster.js +++ b/src/MarkerCluster.js @@ -85,7 +85,7 @@ L.MarkerCluster = L.Marker.extend({ for (i = markers.length - 1; i >= 0; i--) { if (markers[i] == layer) { markers.splice(i, 1); - //TODO? Recalculate bounds + this._recalculateBounds(); this._childCount--; if (this._icon) { @@ -97,9 +97,17 @@ L.MarkerCluster = L.Marker.extend({ //Otherwise check our childClusters for (i = childClusters.length - 1; i >= 0; i--) { - if (childClusters[i]._recursivelyRemoveChildMarker(layer)) { + var child = childClusters[i]; + if (child._recursivelyRemoveChildMarker(layer)) { this._childCount--; - //TODO? Recalculate bounds + + //if our child cluster is no longer a cluster, remove it and replace with just the marker + if (child._childCount === 1) { + markers.push(child._markers[0]); + childClusters.splice(i, 1); + } + + this._recalculateBounds(); if (this._icon) { this.setIcon(this._group.options.iconCreateFunction(this._childCount)); @@ -231,6 +239,24 @@ L.MarkerCluster = L.Marker.extend({ } }, + _recalculateBounds: function () { + var markers = this._markers, + childClusters = this._childClusters, + i; + + this._bounds = new L.LatLngBounds(); + + for (i = markers.length - 1; i >= 0; i--) { + this._bounds.extend(markers[i].getLatLng()); + } + for (i = childClusters.length - 1; i >= 0; i--) { + this._bounds.extend(childClusters[i]._bounds); + } + + this.setLatLng(this._bounds.getCenter()); + }, + + //Returns true if we are the parent of only one cluster and that cluster is the same as us _isSingleParent: function () { //Don't need to check this._markers as the rest won't work if there are any diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 3cef2765d..5ca61203d 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -235,7 +235,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ if (c._childCount == 1) { //Remove cluster and add individual marker L.FeatureGroup.prototype.removeLayer.call(this, c); - var marker = c.getAllChildMarkers()[0]; + var marker = c._markers[0]; L.FeatureGroup.prototype.addLayer.call(this, marker); current.unclustered.push(marker); current.clusters.splice(i, 1);