### Dart Fluster Marker Clustering Example Source: https://context7.com/alfonsocejudo/fluster/llms.txt Demonstrates initializing Fluster with sample geographic markers, generating clusters at a specific zoom level, and iterating through clusters to display their properties and contained points. It also shows how to retrieve child markers and all leaf points within a cluster. This example requires the fluster package. ```dart import 'package:fluster/fluster.dart'; import 'package:fluster/src/base_cluster.dart'; void main() { // Sample data: landmarks in NYC and Washington DC var markers = [ MapMarker( locationName: 'One World Trade Center', markerId: '9000001', latitude: 40.736291, longitude: -73.990243 ), MapMarker( locationName: 'Brooklyn Bridge', markerId: '9000002', latitude: 40.731349, longitude: -73.997723 ), MapMarker( locationName: 'Statue of Liberty', markerId: '9000003', latitude: 40.670274, longitude: -73.964054 ), MapMarker( locationName: 'White House', markerId: '9000004', latitude: 38.889974, longitude: -77.019908 ), ]; // Initialize clustering engine var fluster = Fluster( minZoom: 0, maxZoom: 21, radius: 150, extent: 2048, nodeSize: 64, points: markers, createCluster: (BaseCluster cluster, double longitude, double latitude) => MapMarker( locationName: null, latitude: latitude, longitude: longitude, isCluster: true, clusterId: cluster.id, pointsSize: cluster.pointsSize, markerId: cluster.id.toString(), childMarkerId: cluster.childMarkerId ) ); // Get clusters at zoom level 10 const currentZoom = 10; var clusters = fluster.clusters([-180, -85, 180, 85], currentZoom); print('Number of clusters at zoom $currentZoom: ${clusters.length}'); // Analyze each cluster/point for (var item in clusters) { if (item.isCluster!) { print('\nCluster ID: ${item.clusterId}'); print(' Contains: ${item.pointsSize} points'); print(' Center: (${item.latitude}, ${item.longitude})'); // Get children var children = fluster.children(item.clusterId); if (children != null && children.isNotEmpty) { print(' Direct children: ${children.length}'); } // Get all leaf points var points = fluster.points(item.clusterId!); print(' Leaf points:'); for (var point in points) { print(' - ${point.locationName}'); } } else { print('\nIndividual marker: ${item.locationName}'); print(' Location: (${item.latitude}, ${item.longitude})'); } } } class MapMarker extends Clusterable { String? locationName; String? thumbnailSrc; MapMarker({ this.locationName, latitude, longitude, this.thumbnailSrc, isCluster = false, clusterId, pointsSize, markerId, childMarkerId }) : super( latitude: latitude, longitude: longitude, isCluster: isCluster, clusterId: clusterId, pointsSize: pointsSize, markerId: markerId, childMarkerId: childMarkerId ); } ``` -------------------------------- ### Initialize Fluster with Configuration in Dart Source: https://context7.com/alfonsocejudo/fluster/llms.txt Initialize the `Fluster` class with specific clustering parameters such as `minZoom`, `maxZoom`, `radius`, `extent`, and `nodeSize`. A `createCluster` factory function is required to generate cluster objects, which should extend `BaseCluster` and return a `MapMarker` in this example. ```dart import 'package:fluster/fluster.dart'; import 'package:fluster/src/base_cluster.dart'; // Initialize Fluster with clustering configuration var fluster = Fluster( minZoom: 0, // No clustering below zoom 0 maxZoom: 21, // No clustering above zoom 21 radius: 150, // Cluster radius in pixels extent: 2048, // Tile extent (affects clustering distance) nodeSize: 64, // KD-tree leaf node size (performance tuning) points: markers, // List of MapMarker objects to cluster createCluster: (BaseCluster cluster, double longitude, double latitude) { // Factory function to create cluster markers return MapMarker( locationName: null, latitude: latitude, longitude: longitude, isCluster: true, clusterId: cluster.id, pointsSize: cluster.pointsSize, markerId: cluster.id.toString(), childMarkerId: cluster.childMarkerId ); } ); // Fluster is now ready to provide clusters for any zoom level and bounding box ``` -------------------------------- ### Get Cluster Children in Dart Source: https://github.com/alfonsocejudo/fluster/blob/master/README.md Provides a method to retrieve the immediate children (points and sub-clusters) of a specific cluster, identified by its `clusterId`. This is useful for drilling down into clusters when a user interacts with them on a map. ```dart List chilren = fluster.children(int clusterId); ``` -------------------------------- ### Define Custom Clusterable Markers in Dart Source: https://context7.com/alfonsocejudo/fluster/llms.txt Implement the `Clusterable` abstract class to define custom marker types for Fluster. This involves extending the base class and adding specific properties like location names or thumbnail sources. The example demonstrates creating `MapMarker` objects with latitude, longitude, and other relevant details. ```dart import 'package:fluster/fluster.dart'; class MapMarker extends Clusterable { String? locationName; String? thumbnailSrc; MapMarker({ this.locationName, latitude, longitude, this.thumbnailSrc, isCluster = false, clusterId, pointsSize, markerId, childMarkerId }) : super( latitude: latitude, longitude: longitude, isCluster: isCluster, clusterId: clusterId, pointsSize: pointsSize, markerId: markerId, childMarkerId: childMarkerId ); } // Create markers var markers = [ MapMarker( locationName: 'Empire State Building', markerId: 'marker_001', latitude: 40.748817, longitude: -73.985428 ), MapMarker( locationName: 'Times Square', markerId: 'marker_002', latitude: 40.758896, longitude: -73.985130 ), MapMarker( locationName: 'Central Park', markerId: 'marker_003', latitude: 40.785091, longitude: -73.968285 ) ]; ``` -------------------------------- ### Get Clusters within Bounding Box in Dart Source: https://github.com/alfonsocejudo/fluster/blob/master/README.md Shows how to retrieve clustered points for a specific geographic bounding box and zoom level. The bounding box is represented as `[southwestLng, southwestLat, northeastLng, northeastLat]`. This is useful for displaying markers on a map based on the current viewport. ```dart List clusters = fluster.clusters([-180, -85, 180, 85], _currentZoom); ``` -------------------------------- ### Get Immediate Children of a Cluster in Dart Source: https://context7.com/alfonsocejudo/fluster/llms.txt This snippet demonstrates how to retrieve the direct children (points and sub-clusters) of a specified cluster using its ID. It handles cases where the cluster ID might not exist or the cluster has no children. Assumes the 'fluster' object and 'MapMarker' class are available. ```dart // Find a cluster in the results var clusters = fluster.clusters([-180.0, -85.0, 180.0, 85.0], 8); // Get the first cluster var firstCluster = clusters.firstWhere( (marker) => marker.isCluster == true, orElse: () => clusters.first ); if (firstCluster.isCluster!) { int clusterId = firstCluster.clusterId!; // Get immediate children of this cluster List? children = fluster.children(clusterId); if (children != null) { print('Cluster ${clusterId} has ${children.length} children:'); for (var child in children) { if (child.isCluster!) { print(' - Sub-cluster with ${child.pointsSize} points'); } else { print(' - Point: ${child.locationName}'); } } } } // Handle null case var childrenResult = fluster.children(999999); if (childrenResult == null) { print('Cluster ID not found or has no children'); } ``` -------------------------------- ### Get All Child Points of a Cluster in Dart Source: https://github.com/alfonsocejudo/fluster/blob/master/README.md Enables fetching all individual points contained within a cluster and its sub-clusters across all remaining zoom levels, given a `clusterId`. This function is valuable for displaying all constituent points when a cluster is fully expanded. ```dart List points = fluster.points(clusterId); ``` -------------------------------- ### Get Clusters within Bounding Box and Zoom Level in Dart Source: https://context7.com/alfonsocejudo/fluster/llms.txt Retrieve clustered points and individual markers visible within a given geographic bounding box at a specific map zoom level using the `clusters` method. The bounding box is defined as `[southwestLng, southwestLat, northeastLng, northeastLat]`. The results can be iterated to distinguish between clusters and individual markers. ```dart // Define current map zoom level const currentZoom = 10; // Define bounding box [southwestLng, southwestLat, northeastLng, northeastLat] // World bounds example: var worldBounds = [-180.0, -85.0, 180.0, 85.0]; // Get all clusters visible in the bounding box at zoom level 10 List clusters = fluster.clusters(worldBounds, currentZoom); // Process results for (var cluster in clusters) { if (cluster.isCluster!) { print('Cluster at (${cluster.latitude}, ${cluster.longitude}) ' 'contains ${cluster.pointsSize} points'); } else { print('Individual marker: ${cluster.locationName} ' 'at (${cluster.latitude}, ${cluster.longitude})'); } } // Regional bounding box example (New York City area) var nycBounds = [-74.25, 40.50, -73.70, 40.92]; var nycClusters = fluster.clusters(nycBounds, 12); print('NYC area has ${nycClusters.length} clusters/markers at zoom 12') ``` -------------------------------- ### Get All Leaf Points within a Cluster in Dart Source: https://context7.com/alfonsocejudo/fluster/llms.txt This code shows how to extract all individual points, recursively, from a given cluster ID, regardless of their zoom level. It's useful for displaying all locations within a large cluster when it's tapped. Assumes 'fluster' object and 'MapMarker' class are available, along with a 'showLocationList' function. ```dart // Get clusters at a high-level zoom var topLevelClusters = fluster.clusters([-180.0, -85.0, 180.0, 85.0], 5); // Find a cluster with multiple points var largeCluster = topLevelClusters.firstWhere( (marker) => marker.isCluster! && marker.pointsSize! > 5, orElse: () => topLevelClusters.first ); if (largeCluster.isCluster!) { // Get ALL individual points in this cluster (recursively) List allPoints = fluster.points(largeCluster.clusterId!); print('Cluster ${largeCluster.clusterId} contains ${allPoints.length} total points:'); for (var point in allPoints) { print(' - ${point.locationName} (${point.markerId})'); print(' Location: ${point.latitude}, ${point.longitude}'); } } // Example: Display all markers when user clicks a cluster void onClusterTapped(MapMarker cluster) { if (cluster.isCluster!) { List expandedPoints = fluster.points(cluster.clusterId!); // Show list dialog with all locations in the cluster showLocationList(expandedPoints); } } ``` -------------------------------- ### Set Up Fluster Instance in Dart Source: https://github.com/alfonsocejudo/fluster/blob/master/README.md Demonstrates how to instantiate `Fluster` with a list of `MapMarker` objects. It configures clustering parameters such as `minZoom`, `maxZoom`, `radius`, `extent`, `nodeSize`, and provides a `createCluster` callback for custom cluster generation. ```dart List markers = getMarkers(); Fluster fluster = Fluster( minZoom: 0, maxZoom: 20, radius: 150, extent: 2048, nodeSize: 64, points: markers, createCluster: (BaseCluster cluster, double longitude, double latitude) { return MapMarker( markerId: cluster.id.toString(), latitude: latitude, longitude: longitude); }); ``` -------------------------------- ### Implement Clusterable Interface in Dart Source: https://github.com/alfonsocejudo/fluster/blob/master/README.md Defines the `MapMarker` class that extends `Clusterable`, providing latitude, longitude, and custom properties like `locationName` and `thumbnailSrc`. This class is used to represent individual points to be clustered. ```dart class MapMarker extends Clusterable { String locationName; String thumbnailSrc; MapMarker( {this.locationName, latitude, longitude, this.thumbnailSrc, isCluster = false, clusterId, pointsSize, markerId, childMarkerId}) : super( latitude: latitude, longitude: longitude, isCluster: isCluster, clusterId: clusterId, pointsSize: pointsSize, markerId: markerId, childMarkerId: childMarkerId); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.