From 97d2eaa746febfc70224a94f42cbb751102a8158 Mon Sep 17 00:00:00 2001 From: Remo Laubacher Date: Tue, 15 Dec 2015 13:14:10 +0100 Subject: [PATCH 1/2] ignore hidden markers in cluster --- src/markerclusterer.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/markerclusterer.js b/src/markerclusterer.js index a31e3a9..1398437 100755 --- a/src/markerclusterer.js +++ b/src/markerclusterer.js @@ -859,7 +859,7 @@ Cluster.prototype.addMarker = function(marker) { marker.isAdded = true; this.markers_.push(marker); - var len = this.markers_.length; + var len = this.getVisibleMarkers().length; if (len < this.minClusterSize_ && marker.getMap() != this.map_) { // Min cluster size not reached so show the marker. marker.setMap(this.map_); @@ -936,6 +936,23 @@ Cluster.prototype.getMarkers = function() { }; +/** + * Returns the array of markers in the cluster. + * + * @return {Array.} The markers. + */ +Cluster.prototype.getVisibleMarkers = function() { + var visibleMarkers = []; + for (var i in this.markers_) { + var marker = this.markers_[i]; + if (marker.visible) { + visibleMarkers.push(marker); + } + } + return visibleMarkers; +}; + + /** * Returns the center of the cluster. * @@ -984,23 +1001,24 @@ Cluster.prototype.getMap = function() { Cluster.prototype.updateIcon = function() { var zoom = this.map_.getZoom(); var mz = this.markerClusterer_.getMaxZoom(); + var markers = this.getVisibleMarkers(); if (mz && zoom > mz) { // The zoom is greater than our max zoom so show all the markers in cluster. - for (var i = 0, marker; marker = this.markers_[i]; i++) { + for (var i = 0, marker; marker = markers[i]; i++) { marker.setMap(this.map_); } return; } - if (this.markers_.length < this.minClusterSize_) { + if (markers_.length < this.minClusterSize_) { // Min cluster size not yet reached. this.clusterIcon_.hide(); return; } var numStyles = this.markerClusterer_.getStyles().length; - var sums = this.markerClusterer_.getCalculator()(this.markers_, numStyles); + var sums = this.markerClusterer_.getCalculator()(markers, numStyles); this.clusterIcon_.setCenter(this.center_); this.clusterIcon_.setSums(sums); this.clusterIcon_.show(); @@ -1298,6 +1316,7 @@ MarkerClusterer.prototype['draw'] = MarkerClusterer.prototype.draw; Cluster.prototype['getCenter'] = Cluster.prototype.getCenter; Cluster.prototype['getSize'] = Cluster.prototype.getSize; Cluster.prototype['getMarkers'] = Cluster.prototype.getMarkers; +Cluster.prototype['getVisibleMarkers'] = Cluster.prototype.getVisibleMarkers; ClusterIcon.prototype['onAdd'] = ClusterIcon.prototype.onAdd; ClusterIcon.prototype['draw'] = ClusterIcon.prototype.draw; From a52aa4a721bd58db4ae0c0e88320428c31ffdb83 Mon Sep 17 00:00:00 2001 From: Remo Laubacher Date: Tue, 15 Dec 2015 13:19:00 +0100 Subject: [PATCH 2/2] hide correct marker --- src/markerclusterer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/markerclusterer.js b/src/markerclusterer.js index 1398437..ebf218c 100755 --- a/src/markerclusterer.js +++ b/src/markerclusterer.js @@ -859,7 +859,8 @@ Cluster.prototype.addMarker = function(marker) { marker.isAdded = true; this.markers_.push(marker); - var len = this.getVisibleMarkers().length; + var visibleMarkers = this.getVisibleMarkers(); + var len = visibleMarkers.length; if (len < this.minClusterSize_ && marker.getMap() != this.map_) { // Min cluster size not reached so show the marker. marker.setMap(this.map_); @@ -868,7 +869,7 @@ Cluster.prototype.addMarker = function(marker) { if (len == this.minClusterSize_) { // Hide the markers that were showing. for (var i = 0; i < len; i++) { - this.markers_[i].setMap(null); + visibleMarkers[i].setMap(null); } }