fluster 1.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 89

Build Status Pub

Fluster = Flutter + Cluster

A geospatial point clustering library for Dart (Flutter not actually required). The algorithm is a Dart port of supercluster.

Image

Usage #

Implement Clusterable #

Fluster will cluster a List of objects that conform to the Clusterable abstract class, which includes necessary information such as latitude and 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);
}

Set up Fluster #

Create a Fluster instance once you have a set of points you'd like to cluster:

List<MapMarker> markers = getMarkers();

Fluster<MapMarker> fluster = Fluster<MapMarker>(
      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);
      });

Parameters:

/// Any zoom value below minZoom will not generate clusters.
int minZoom;

/// Any zoom value above maxZoom will not generate clusters.
int maxZoom;

/// Cluster radius in pixels.
int radius;

/// Adjust the extent by powers of 2 (e.g. 512. 1024, ... max 8192) to get the
/// desired distance between markers where they start to cluster.
int extent;

/// The size of the KD-tree leaf node, which affects performance.
int nodeSize;

/// The List to be clustered.
List<T> points;

/// A callback to generate clusters of the given input type.
T Function(BaseCluster, double, double) createCluster;

Get the clusters #

You can then get the clusters for a given bounding box and zoom value, where the bounding box = [southwestLng, southwestLat, northeastLng, northeastLat]:

List<MapMarker> clusters = fluster.clusters([-180, -85, 180, 85], _currentZoom);

Get the cluster children #

You can also get the children (points and sub-clusters inside a cluster at the next zoom level, given the cluster id:

List<MapMarker> chilren = fluster.children(int clusterId);

Get the cluster points #

Get only the child points (not sub-clusters) of a cluster in all the remaining zoom levels, given the cluster id:

List<MapMarker> points = fluster.points(clusterId);

1.1.2 #

  • Updated description field in pubspec.

1.1.1 #

  • Added Travis CI support

1.1.0 #

  • Updated fluster_example.dart

1.0.6 #

  • Added Clusterable default value isCluster = false

1.0.5 #

  • Added Clusterable constructor

1.0.4 #

  • Removed dependency on meta package

1.0.3 #

  • Exporting base_cluster.dart

1.0.2 #

  • Exporting clusterable.dart

1.0.1 #

  • Updated version of meta package

1.0.0 #

  • Initial version

example/fluster_example.dart

/*
 * Created by Alfonso Cejudo, Wednesday, July 24th 2019.
 */

import 'package:fluster/fluster.dart';
import 'package:fluster/src/base_cluster.dart';
import 'package:fluster/src/clusterable.dart';

main() {
  const currentZoom = 10;

  List<MapMarker> markers = [
    MapMarker(
        locationName: 'One',
        markerId: '9000001',
        latitude: 40.736291,
        longitude: -73.990243),
    MapMarker(
        locationName: 'Two',
        markerId: '9000002',
        latitude: 40.731349,
        longitude: -73.997723),
    MapMarker(
        locationName: 'Three',
        markerId: '9000003',
        latitude: 40.670274,
        longitude: -73.964054),
    MapMarker(
        locationName: 'Four',
        markerId: '9000004',
        latitude: 38.889974,
        longitude: -77.019908),
  ];

  Fluster<MapMarker> fluster = Fluster<MapMarker>(
      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));

  List<MapMarker> clusters =
      fluster.clusters([-180, -85, 180, 85], currentZoom);

  print('Number of clusters at zoom $currentZoom: ${clusters.length}');
}

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);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  fluster: ^1.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:fluster/fluster.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
79
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
89
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:fluster/fluster.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
Dev dependencies
mockito ^4.1.0
pedantic ^1.7.0
test ^1.6.0