How to use MarkerCluster with GoogleMap in an Angular app ?

Stephane Delecroix
Sep 7, 2018 · 1 min read

You use GoogleMap in a Typescript Angular App and you’d like to display marker cluster.

The wrong way:

Add script in your index.html (or angular-cli.json)

<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">

And use it in your component like this :

declare var MarkerClusterer: any;

Why is it wrong ? Because you will load this .js file even if you don’t need it. You don’t leverage lazy loading.

The right way:

In your console

npm i @google/markerclusterer -S

And in your component, in the method where you handle markers and clusters, just use it as it is : a class. Instanciate it.

import * as MarkerClusterer from "@google/markerclusterer"new MarkerClusterer(map, opt_markers, opt_options)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade