How to Customize Your Cluster Markers on Flutter Google Maps
Tags: flutter, tutorial, googlemaps, dart
Published: 2020-07-08 16:19:18

If you’ve been following our blog and read our recent posts about [Flutter](https://flutter.dev/) and the [Google Maps package](https://pub.dev/packages/google_maps_flutter), I’m sure you now know how to cluster markers, but how do you customize or make them dynamic?
<br/>
This is a follow-up article of [How to Cluster Markers on Google Maps using Flutter](/blog/how-to-cluster-markers-on-google-maps-using-flutter/) so I will not get into the details about clustering the markers. Instead, **I will tell you how I was able to customize the cluster markers by adding a dynamic child counter that updates depending on the zoom level.**
<br/>
This was missing in the previous article, and since some of you asked for it here I am! 😎 Without further wait, let’s get into it! 💪
<br/>
So, how do you customize the markers? Unfortunately, the official Google Maps package [doesn’t support clusters](https://github.com/flutter/flutter/issues/26863), and the [markers aren’t event widgets](https://github.com/flutter/flutter/issues/24213) so we must resort to drawing on the [canvas](https://api.flutter.dev/flutter/dart-ui/Canvas-class.html)… wait! Isn’t that scary? 😱
<br/>
Flutter makes it easy so it’s nothing to be afraid of. The following code contains the solution I found:

```dart
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

Future<BitmapDescriptor> getClusterMarker(
  int clusterSize,
  Color clusterColor,
  Color textColor,
  int width,
) async {
  final PictureRecorder pictureRecorder = PictureRecorder();
  final Canvas canvas = Canvas(pictureRecorder);
  final Paint paint = Paint()..color = clusterColor;
  final TextPainter textPainter = TextPainter(
    textDirection: TextDirection.ltr,
  );

  final double radius = width / 2;

  canvas.drawCircle(
    Offset(radius, radius),
    radius,
    paint,
  );

  textPainter.text = TextSpan(
    text: clusterSize.toString(),
    style: TextStyle(
      fontSize: radius - 5,
      fontWeight: FontWeight.bold,
      color: textColor,
    ),
  );

  textPainter.layout();
  textPainter.paint(
    canvas,
    Offset(
      radius - textPainter.width / 2,
      radius - textPainter.height / 2,
    ),
  );

  final image = await pictureRecorder.endRecording().toImage(
    radius.toInt() * 2,
    radius.toInt() * 2,
  );

  final data = await image.toByteData(format: ImageByteFormat.png);

  return BitmapDescriptor.fromBytes(data.buffer.asUint8List());
}
```

First, we set up the [PictureRecorder](https://api.flutter.dev/flutter/dart-ui/PictureRecorder-class.html), [Canvas](https://api.flutter.dev/flutter/dart-ui/Canvas-class.html), [Paint](https://api.flutter.dev/flutter/dart-ui/Paint-class.html), and [TextPainter](https://api.flutter.dev/flutter/painting/TextPainter-class.html) with proper colors and text direction, then we simply get the radius and draw a circle on the canvas.
<br/>
Next is the text, we want it to be the cluster size counter text and we add some style to it. In the end, we simply end the picture recorder and convert it to an image and then to a bitmap descriptor needed for the marker icon.
<br/>
And that’s it! Pretty simple isn’t it? To end this article, I will tell you what more has changed from the latest [How to Cluster Markers on Google Maps using Flutter](https://coletiv.com/blog/how-to-cluster-markers-on-google-maps-using-flutter/) post and we’re finished 🙌.
<br/>
We just need to update the way we’re generating the markers like this:

```dart
return Future.wait(clusterManager.clusters(
  [-180, -85, 180, 85], currentZoom.toInt(),
).map((mapMarker) async {
  if (mapMarker.isCluster) {
    mapMarker.icon = await _getClusterMarker(
      mapMarker.pointsSize,
      Colors.blue,
      Colors.white,
      80,
    );
  }

  return mapMarker.toMarker();
}).toList());
```

We’re checking if the map marker is a cluster and if it is we generate the cluster icon with the updated point size to have the correct counter text inside the circle marker. We also tell it which colors we want to use and the width. Just keep in mind that this is now an async operation.
<br/>
There are a few more small changes that you can see on our [flutter google maps clusters](https://github.com/coletiv/flutter_google_maps_clusters) repo. Go on and check it out!

## Thank you for reading!

Thank you so much for reading, it means a lot to us! Also **don’t forget to follow [Coletiv](/) on [Twitter](https://twitter.com/coletivstudio) and [LinkedIn](https://www.linkedin.com/company/coletiv/)** as we keep posting more and more interesting articles on multiple technologies.

In case you don’t know, Coletiv is a software development studio from Porto specialized in Elixir, Web, and App (iOS & Android) development. But we do all kinds of stuff. We take care of UX/UI design, software development, and even security for you.

So, **[let’s craft something together?](https://coletiv.com/contact)**