google_maps_flexible_cluster library
A highly customizable clustering solution for Google Maps in Flutter.
This package provides efficient clustering of map markers with customizable icons, configurable clustering behavior, and smooth performance.
Features
- Efficient clustering algorithm
- Customizable cluster icons
- Type-safe generic API
- Built-in icon caching
- Configurable clustering parameters
- Support for custom item data
Getting started
import 'package:flutter_custom_cluster/flutter_custom_cluster.dart';
// 1. Create your custom item class
class MyLocation extends ClusterableItem {
final String name;
MyLocation({
required String id,
required LatLng position,
required this.name,
}) : super(id: id, position: position);
}
// 2. Create a cluster manager
final manager = ClusterManager<MyLocation>(
items: myLocations,
config: ClusterConfig(
radius: 150,
minZoom: 0,
maxZoom: 19,
),
);
// 3. Get markers for your map
final markers = await manager.getMarkers(
zoom: currentZoom,
onMarkerTap: (id, item) {
print('Tapped: ${item.name}');
},
onClusterTap: (id, cluster) {
print('Tapped cluster with ${cluster.clusterSize} items');
},
);
// 4. Update your map
setState(() {
_markers = markers;
});
Custom Icons
class MyCustomPainter extends ClusterIconPainter {
@override
Future<Uint8List> paintCluster(ClusterItem cluster) async {
// Your custom cluster icon painting
}
@override
Future<Uint8List> paintItem(ClusterableItem item) async {
// Your custom item icon painting
}
}
final manager = ClusterManager<MyLocation>(
items: myLocations,
painter: MyCustomPainter(),
);
Classes
- ClusterableItem
- Base class for items that can be clustered on a map.
- ClusterConfig
- Configuration options for the clustering behavior.
- ClusterIconPainter
- Abstract base class for painting cluster icons.
- ClusterItem
- Represents a cluster of multiple items at a specific location.
-
ClusterManager<
T extends ClusterableItem> - Manages clustering of map items based on zoom level and proximity.
- ClusterUtils
- Utility functions for marker and clustering operations.
- DefaultClusterPainter
- Default implementation of ClusterIconPainter.