Change zoomToBounds to only zoom in as far as it needs to to show all child markers. Fixes #185

This commit is contained in:
danzel
2013-11-13 15:26:41 +13:00
parent 9e36a0ba62
commit f693a49dec
2 changed files with 22 additions and 2 deletions
+1
View File
@@ -96,6 +96,7 @@ markers.on('clusterclick', function (a) {
### Zooming to the bounds of a cluster
When you recieve an event from a cluster you can zoom to its bounds in one easy step.
If all of the markers will appear at a higher zoom level, that zoom level is zoomed to instead.
See [marker-clustering-zoomtobounds.html](http://leaflet.github.com/Leaflet.markercluster/example/marker-clustering-zoomtobounds.html) for a working example.
```javascript
markers.on('clusterclick', function (a) {
+21 -2
View File
@@ -42,9 +42,28 @@ L.MarkerCluster = L.Marker.extend({
return this._childCount;
},
//Zoom to the extents of this cluster
//Zoom to the minimum of showing all of the child markers, or the extents of this cluster
zoomToBounds: function () {
this._group._map.fitBounds(this._bounds);
var childClusters = this._childClusters.slice(),
map = this._group._map,
boundsZoom = map.getBoundsZoom(this._bounds),
zoom = this._zoom + 1,
i;
while (childClusters.length > 0 && boundsZoom > zoom) {
zoom++;
var newClusters = [];
for (i = 0; i < childClusters.length; i++) {
newClusters = newClusters.concat(childClusters[i]._childClusters);
}
childClusters = newClusters;
}
if (boundsZoom > zoom) {
this._group._map.setView(this._latlng, zoom);
} else {
this._group._map.fitBounds(this._bounds);
}
},
getBounds: function () {