From a547ba2c1776ff762f40f23a5be0ff831d20db6a Mon Sep 17 00:00:00 2001 From: Ben Milanko Date: Tue, 22 Apr 2025 12:09:38 +1000 Subject: [PATCH] Add spiderfy marker limit --- lib/src/marker_cluster_layer.dart | 13 ++++++++++--- lib/src/marker_cluster_layer_options.dart | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/src/marker_cluster_layer.dart b/lib/src/marker_cluster_layer.dart index 55dc689..87e1388 100755 --- a/lib/src/marker_cluster_layer.dart +++ b/lib/src/marker_cluster_layer.dart @@ -50,6 +50,13 @@ class _MarkerClusterLayerState extends State _MarkerClusterLayerState(); + bool _shouldSpiderfy(MarkerClusterNode cluster) { + return widget.options.spiderfyCluster && + (widget.options.disableSpiderfyAboveMarkerCount <= 0 || + cluster.markers.length <= + widget.options.disableSpiderfyAboveMarkerCount); + } + bool _isSpiderfyCluster(MarkerClusterNode cluster) { return spiderfyCluster != null && spiderfyCluster!.bounds.center == cluster.bounds.center; @@ -623,7 +630,7 @@ class _MarkerClusterLayerState extends State widget.options.onClusterTap?.call(cluster); if (!widget.options.zoomToBoundsOnClick) { - if (widget.options.spiderfyCluster) { + if (_shouldSpiderfy(cluster)) { if (spiderfyCluster != null) { if (spiderfyCluster == cluster) { _unspiderfy(); @@ -691,7 +698,7 @@ class _MarkerClusterLayerState extends State zoomTween.begin == zoomTween.end; if (isAlreadyFit) { - if (cannotDivide && widget.options.spiderfyCluster) { + if (cannotDivide && _shouldSpiderfy(cluster)) { _spiderfy(cluster); } return; @@ -711,7 +718,7 @@ class _MarkerClusterLayerState extends State ..removeListener(listener) ..reset(); - if (cannotDivide && widget.options.spiderfyCluster) { + if (cannotDivide && _shouldSpiderfy(cluster)) { _spiderfy(cluster); } }); diff --git a/lib/src/marker_cluster_layer_options.dart b/lib/src/marker_cluster_layer_options.dart index 2097a6f..683bfd5 100755 --- a/lib/src/marker_cluster_layer_options.dart +++ b/lib/src/marker_cluster_layer_options.dart @@ -130,6 +130,10 @@ class MarkerClusterLayerOptions { /// If false remove spiderfy effect on tap final bool spiderfyCluster; + /// If set, spiderfy will be disabled above this marker count + /// 0 -> No limit + final int disableSpiderfyAboveMarkerCount; + /// Increase to increase the distance away that circle spiderfied markers appear from the center final int spiderfyCircleRadius; @@ -210,6 +214,7 @@ class MarkerClusterLayerOptions { this.circleSpiralSwitchover = 9, this.spiderfyShapePositions, this.spiderfyCluster = true, + this.disableSpiderfyAboveMarkerCount = 0, this.polygonOptions = const PolygonOptions(), this.showPolygon = true, this.onMarkerTap,