From 9a6e7004b0d07f7b13fe90c39305cb9354784255 Mon Sep 17 00:00:00 2001 From: danzel Date: Mon, 20 Aug 2012 10:18:34 +1200 Subject: [PATCH] Respect disableClusteringAtZoom when adding individual layers --- src/MarkerCluster.js | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js index f900e724c..3d7500bbb 100644 --- a/src/MarkerCluster.js +++ b/src/MarkerCluster.js @@ -107,8 +107,9 @@ L.MarkerCluster = L.Marker.extend({ // false: wasn't able to put this marker in // a MarkerCluster: the visible parent of the marker _recursivelyAddLayer: function (layer, zoom) { - var map = this._group._map, - maxClusterRadius = this._group.options.maxClusterRadius, + var group = this._group, + map = group._map, + maxClusterRadius = group.options.maxClusterRadius, result = false, i; @@ -127,21 +128,24 @@ L.MarkerCluster = L.Marker.extend({ //Couldn't add it to a child, but it should be part of us (this._zoom -> we are the root node) if (!result && (this._canAcceptPosition(layer.getLatLng(), zoom) || ('_zoom' in this))) { - //Add to ourself instead - var layerPos = map.project(layer.getLatLng(), zoom + 1), - sqDist = this._group._sqDist; + //If we are allowed to cluster at our childs level + if (zoom + 1 !== group.options.disableClusteringAtZoom) { - //var distanceGrid = new L.DistanceGrid(maxClusterRadius); - for (i = this._markers.length - 1; i >= 0; i--) { - var m = this._markers[i]; - if (sqDist(layerPos, map.project(m.getLatLng(), zoom + 1)) < (maxClusterRadius * maxClusterRadius)) { - result = m; - this._markers.splice(i, 1); - this._childCount--; - break; + //Add to ourself instead + var layerPos = map.project(layer.getLatLng(), zoom + 1), + sqDist = this._group._sqDist; + + //var distanceGrid = new L.DistanceGrid(maxClusterRadius); + for (i = this._markers.length - 1; i >= 0; i--) { + var m = this._markers[i]; + if (sqDist(layerPos, map.project(m.getLatLng(), zoom + 1)) < (maxClusterRadius * maxClusterRadius)) { + result = m; + this._markers.splice(i, 1); + this._childCount--; + break; + } } } - //result = distanceGrid.getNearObject(map.project(layer.getLatLng(), zoom + 1)); if (result) { @@ -158,6 +162,11 @@ L.MarkerCluster = L.Marker.extend({ maxZoom = map.getMaxZoom(), newResult, finalResult = (zoom === wantedZoom) ? result : true; + + if (group.options.disableClusteringAtZoom) { + maxZoom = group.options.disableClusteringAtZoom - 2; + } + while (zoom < maxZoom) { zoom++;