From fc28ce44fa7e452ba2c12b53f8bd6a3dcc843b0d Mon Sep 17 00:00:00 2001 From: quangnh Date: Thu, 24 Jul 2025 11:46:27 +0700 Subject: [PATCH] update local cluster --- lib/src/cluster_item.dart | 3 ++- lib/src/cluster_manager.dart | 48 +++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/src/cluster_item.dart b/lib/src/cluster_item.dart index a81833a..470a666 100644 --- a/lib/src/cluster_item.dart +++ b/lib/src/cluster_item.dart @@ -1,11 +1,12 @@ import 'package:google_maps_cluster_manager/google_maps_cluster_manager.dart'; import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart' hide ClusterManager; +import 'cluster_manager.dart' as local; mixin ClusterItem { LatLng get location; String? _geohash; String get geohash => _geohash ??= - Geohash.encode(location, codeLength: ClusterManager.precision); + Geohash.encode(location, codeLength: local.ClusterManager.precision); } diff --git a/lib/src/cluster_manager.dart b/lib/src/cluster_manager.dart index 3acfaa9..e471e08 100644 --- a/lib/src/cluster_manager.dart +++ b/lib/src/cluster_manager.dart @@ -7,6 +7,7 @@ import 'package:google_maps_cluster_manager/google_maps_cluster_manager.dart'; import 'package:google_maps_cluster_manager/src/max_dist_clustering.dart'; import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart' hide Cluster; +import 'cluster.dart' as local; enum ClusterAlgorithm { GEOHASH, MAX_DIST } @@ -18,18 +19,18 @@ class MaxDistParams { class ClusterManager { ClusterManager(this._items, this.updateMarkers, - {Future Function(Cluster)? markerBuilder, + {Future Function(local.Cluster)? markerBuilder, this.levels = const [1, 4.25, 6.75, 8.25, 11.5, 14.5, 16.0, 16.5, 20.0], this.extraPercent = 0.5, this.maxItemsForMaxDistAlgo = 200, this.clusterAlgorithm = ClusterAlgorithm.GEOHASH, this.maxDistParams, this.stopClusteringZoom}) - : this.markerBuilder = markerBuilder ?? _basicMarkerBuilder, + : this.markerBuilder = markerBuilder ?? _basicMarkerBuilder(), assert(levels.length <= precision); /// Method to build markers - final Future Function(Cluster) markerBuilder; + final Future Function(local.Cluster) markerBuilder; // Num of Items to switch from MAX_DIST algo to GEOHASH final int maxItemsForMaxDistAlgo; @@ -79,7 +80,7 @@ class ClusterManager { } void _updateClusters() async { - List> mapMarkers = await getMarkers(); + List> mapMarkers = await getMarkers(); final Set markers = Set.from(await Future.wait(mapMarkers.map((m) => markerBuilder(m)))); @@ -108,7 +109,7 @@ class ClusterManager { } /// Retrieve cluster markers - Future>> getMarkers() async { + Future>> getMarkers() async { if (_mapId == null) return List.empty(); final LatLngBounds mapBounds = await GoogleMapsFlutterPlatform.instance @@ -126,9 +127,9 @@ class ClusterManager { }).toList(); if (stopClusteringZoom != null && _zoom >= stopClusteringZoom!) - return visibleItems.map((i) => Cluster.fromItems([i])).toList(); + return visibleItems.map((i) => local.Cluster.fromItems([i])).toList(); - List> markers; + List> markers; if (clusterAlgorithm == ClusterAlgorithm.GEOHASH || visibleItems.length >= maxItemsForMaxDistAlgo) { @@ -188,7 +189,7 @@ class ClusterManager { return 1; } - List> _computeClustersWithMaxDist( + List> _computeClustersWithMaxDist( List inputItems, double zoom) { MaxDistClustering scanner = MaxDistClustering( epsilon: maxDistParams?.epsilon ?? 20, @@ -197,8 +198,8 @@ class ClusterManager { return scanner.run(inputItems, _getZoomLevel(zoom)); } - List> _computeClusters( - List inputItems, List> markerItems, + List> _computeClusters( + List inputItems, List> markerItems, {int level = 5}) { if (inputItems.isEmpty) return markerItems; String nextGeohash = inputItems[0].geohash.substring(0, level); @@ -207,7 +208,7 @@ class ClusterManager { .where((p) => p.geohash.substring(0, level) == nextGeohash) .toList(); - markerItems.add(Cluster.fromItems(items)); + markerItems.add(local.Cluster.fromItems(items)); List newInputList = List.from( inputItems.where((i) => i.geohash.substring(0, level) != nextGeohash)); @@ -215,18 +216,19 @@ class ClusterManager { return _computeClusters(newInputList, markerItems, level: level); } - static Future Function(Cluster) get _basicMarkerBuilder => - (cluster) async { - return Marker( - markerId: MarkerId(cluster.getId()), - position: cluster.location, - onTap: () { - print(cluster); - }, - icon: await _getBasicClusterBitmap(cluster.isMultiple ? 125 : 75, - text: cluster.isMultiple ? cluster.count.toString() : null), - ); - }; + static Future Function(local.Cluster) + _basicMarkerBuilder() => + (local.Cluster cluster) async { + return Marker( + markerId: MarkerId(cluster.getId()), + position: cluster.location, + onTap: () { + print(cluster); + }, + icon: await _getBasicClusterBitmap(cluster.isMultiple ? 125 : 75, + text: cluster.isMultiple ? cluster.count.toString() : null), + ); + }; static Future _getBasicClusterBitmap(int size, {String? text}) async {