From 9381d3ea9200a881a007dcfee94c31ecb977bb51 Mon Sep 17 00:00:00 2001 From: Ken Schwencke Date: Sat, 4 Jan 2014 15:26:04 -0800 Subject: [PATCH 1/4] Add ability to use a function for maxClusterRadius I find it's necessary to use a different radius at different zoom levels. This patch lets you continue to use a single number for all zoom levels, but also lets you use a function to vary the radius for each zoom level. --- src/MarkerClusterGroup.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 7f8f0fb1a..221858307 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -1,4 +1,3 @@ - /* * L.MarkerClusterGroup extends L.FeatureGroup by clustering the markers contained within */ @@ -696,7 +695,17 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ _generateInitialClusters: function () { var maxZoom = this._map.getMaxZoom(), - radius = this.options.maxClusterRadius; + radius = this.options.maxClusterRadius, + radiusFunction = null; + + //If we just set maxClusterRadius to a single number, we need to create + //a simple function to return that number. Otherwise, we just have to + //use the function we've passed in. + if(typeof radius == "number") { + radiusFunction = function(){ return radius }; + } else if(typeof radius == "function") { + radiusFunction = radius; + } if (this.options.disableClusteringAtZoom) { maxZoom = this.options.disableClusteringAtZoom - 1; @@ -704,12 +713,12 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ this._maxZoom = maxZoom; this._gridClusters = {}; this._gridUnclustered = {}; - - //Set up DistanceGrids for each zoom - for (var zoom = maxZoom; zoom >= 0; zoom--) { - this._gridClusters[zoom] = new L.DistanceGrid(radius); - this._gridUnclustered[zoom] = new L.DistanceGrid(radius); - } + + //Set up DistanceGrids for each zoom + for (var zoom = maxZoom; zoom >= 0; zoom--) { + this._gridClusters[zoom] = new L.DistanceGrid(radiusFunction(zoom)); + this._gridUnclustered[zoom] = new L.DistanceGrid(radiusFunction(zoom)); + } this._topClusterLevel = new L.MarkerCluster(this, -1); }, From f7bd0835c7077ca4f9abd75792d14fc58e485bea Mon Sep 17 00:00:00 2001 From: Ken Schwencke Date: Sat, 4 Jan 2014 15:36:08 -0800 Subject: [PATCH 2/4] Update README.md added reference to the maxClusterRadius function ability. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 88bc51e69..216ea38db 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ Enabled by default (boolean options): Other options * **animateAddingMarkers**: If set to true then adding individual markers to the MarkerClusterGroup after it has been added to the map will add the marker and animate it in to the cluster. Defaults to false as this gives better performance when bulk adding markers. addLayers does not support this, only addLayer with individual Markers. * **disableClusteringAtZoom**: If set, at this zoom level and below markers will not be clustered. This defaults to disabled. [See Example](http://leaflet.github.com/Leaflet.markercluster/example/marker-clustering-realworld-maxzoom.388.html) -* **maxClusterRadius**: The maximum radius that a cluster will cover from the central marker (in pixels). Default 80. Decreasing will make more smaller clusters. +* **maxClusterRadius**: The maximum radius that a cluster will cover from the central marker (in pixels). Default 80. Decreasing will make more smaller clusters. You can also use a function that accepts the current map zoom and returns the maximum cluster radius in pixels. * **polygonOptions**: Options to pass when creating the L.Polygon(points, options) to show the bounds of a cluster * **singleMarkerMode**: If set to true, overrides the icon for all added markers to make them appear as a 1 size cluster * **spiderfyDistanceMultiplier**: Increase from 1 to increase the distance away from the center that spiderfied markers are placed. Use if you are using big marker icons (Default:1) @@ -138,4 +138,4 @@ Performance optimizations could be done so these are handled more gracefully (Ru Leaflet.markercluster is free software, and may be redistributed under the MIT-LICENSE. -[![Build Status](https://travis-ci.org/Leaflet/Leaflet.markercluster.png?branch=master)](https://travis-ci.org/Leaflet/Leaflet.markercluster) \ No newline at end of file +[![Build Status](https://travis-ci.org/Leaflet/Leaflet.markercluster.png?branch=master)](https://travis-ci.org/Leaflet/Leaflet.markercluster) From 05f2131714de0fa862f1974bcfb59beca632b914 Mon Sep 17 00:00:00 2001 From: Ken Schwencke Date: Sat, 4 Jan 2014 15:44:13 -0800 Subject: [PATCH 3/4] fixed tabs and spacing --- src/MarkerClusterGroup.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 221858307..885ec44f6 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -694,18 +694,18 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ }, _generateInitialClusters: function () { - var maxZoom = this._map.getMaxZoom(), + var maxZoom = this._map.getMaxZoom(), radius = this.options.maxClusterRadius, - radiusFunction = null; + radiusFunction = null; - //If we just set maxClusterRadius to a single number, we need to create - //a simple function to return that number. Otherwise, we just have to - //use the function we've passed in. - if(typeof radius == "number") { - radiusFunction = function(){ return radius }; - } else if(typeof radius == "function") { - radiusFunction = radius; - } + //If we just set maxClusterRadius to a single number, we need to create + //a simple function to return that number. Otherwise, we just have to + //use the function we've passed in. + if(typeof radius == "number") { + radiusFunction = function(){ return radius }; + } else if(typeof radius == "function") { + radiusFunction = radius; + } if (this.options.disableClusteringAtZoom) { maxZoom = this.options.disableClusteringAtZoom - 1; @@ -714,11 +714,11 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ this._gridClusters = {}; this._gridUnclustered = {}; - //Set up DistanceGrids for each zoom - for (var zoom = maxZoom; zoom >= 0; zoom--) { - this._gridClusters[zoom] = new L.DistanceGrid(radiusFunction(zoom)); - this._gridUnclustered[zoom] = new L.DistanceGrid(radiusFunction(zoom)); - } + //Set up DistanceGrids for each zoom + for (var zoom = maxZoom; zoom >= 0; zoom--) { + this._gridClusters[zoom] = new L.DistanceGrid(radiusFunction(zoom)); + this._gridUnclustered[zoom] = new L.DistanceGrid(radiusFunction(zoom)); + } this._topClusterLevel = new L.MarkerCluster(this, -1); }, From f7de94b0536cff97cbe1bee8f607072f19cfdfe2 Mon Sep 17 00:00:00 2001 From: Ken Schwencke Date: Sat, 4 Jan 2014 15:47:05 -0800 Subject: [PATCH 4/4] fixed other style issues --- src/MarkerClusterGroup.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 885ec44f6..c559b5329 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -701,9 +701,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ //If we just set maxClusterRadius to a single number, we need to create //a simple function to return that number. Otherwise, we just have to //use the function we've passed in. - if(typeof radius == "number") { - radiusFunction = function(){ return radius }; - } else if(typeof radius == "function") { + if (typeof radius === "number") { + radiusFunction = function () { return radius; }; + } else if (typeof radius === "function") { radiusFunction = radius; }