How to Cluster Markers on Google Maps using Flutter

 


Marker Clustering on Google Maps Flutter
Map Clusters

Fluster Package Comes to the Rescue

Implementing Fluster

1. Extending the Clusterable class

import 'package:fluster/fluster.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:meta/meta.dart';
class MapMarker extends Clusterable {
final String id;
final LatLng position;
final BitmapDescriptor icon;
MapMarker({
@required this.id,
@required this.position,
@required this.icon,
isCluster = false,
clusterId,
pointsSize,
childMarkerId,
}) : super(
markerId: id,
latitude: position.latitude,
longitude: position.longitude,
isCluster: isCluster,
clusterId: clusterId,
pointsSize: pointsSize,
childMarkerId: childMarkerId,
);
Marker toMarker() => Marker(
markerId: MarkerId(id),
position: LatLng(
position.latitude,
position.longitude,
),
icon: icon,
);
}

2. Create the List of Markers to be Displayed

import 'package:flutter_google_maps_clusters/helpers/map_marker.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
final List<MapMarker> markers = [];final List<LatLng> markerLocations = [
LatLng(41.147125, -8.611249),
LatLng(41.145599, -8.610691),
...
];
for (LatLng markerLocation in _markerLocations) {
markers.add(
MapMarker(
id: markerLocations.indexOf(markerLocation).toString(),
position: markerLocation,
icon: markerImage,
),
);
}

3. Init Fluster

import 'package:fluster/fluster.dart';
import 'package:flutter_google_maps_clusters/helpers/map_marker.dart';
final Fluster<MapMarker> fluster = Fluster<MapMarker>(
minZoom: minZoom, // The min zoom at clusters will show
maxZoom: maxZoom, // The max zoom at clusters will show
radius: 150, // Cluster radius in pixels
extent: 2048, // Tile extent. Radius is calculated with it.
nodeSize: 64, // Size of the KD-tree leaf node.
points: markers, // The list of markers created before
createCluster: ( // Create cluster marker
BaseCluster cluster,
double lng,
double lat,
) => MapMarker(
id: cluster.id.toString(),
position: LatLng(lat, lng),
icon: clusterImage,
isCluster: cluster.isCluster,
clusterId: cluster.id,
pointsSize: cluster.pointsSize,
childMarkerId: cluster.childMarkerId,
),
);

4. Get Cluster Markers

import 'package:fluster/fluster.dart';
import 'package:flutter_google_maps_clusters/helpers/map_marker.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
final List<Marker> googleMarkers = fluster
.clusters([-180, -85, 180, 85], currentZoom)
.map((cluster) => cluster.toMarker())
.toList()

5. Show the Map with Markers

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
@override
Widget build(BuildContext context) {
return Scaffold(
body: GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(41.143029, -8.611274),
zoom: currentZoom,
),
markers: googleMarkers.toSet(),
onMapCreated: (controller) {
// You can init Fluster here
},
onCameraMove: (position) {
// Update the markers for the updated position.zoom
),
),
);
}

A GIF showing clusters working on Google Maps.
Marker Clustering Working on Google Maps

Be sure to check the example project at the Coletiv Github account to see the rest of the code needed to implement clusters on Google Maps.

Thank you for reading!

Comments

Popular posts from this blog

Flutter: Provider Architecture Part 1