How to show/hide a MarkerCluster in google maps v3?

Gabi Purcaru picture Gabi Purcaru · Oct 30, 2010 · Viewed 14.5k times · Source

I need to have different markers for different mapTypes, and I'm pushing them to a MarkerClusterer.

I "hide" the markers with:

cluster.set("map", null);
cluster.resetViewport();
cluster.redraw();

And "show" them with:

cluster.set("map", MAP);
cluster.resetViewport();
cluster.redraw();

The problem is that MarkerClusterer seems to not like set("map", null); it throws the error TypeError: Object #<a MarkerClusterer> has no method 'remove'. How can I show/hide them the proper way?

Answer

Micros picture Micros · Mar 22, 2012

In the Javascript API v3 it is sufficient to say:

clusterer.setMap(null);

If you set your map back to the existing map object, the clusters will reappear.

clusterer.setMap( this.map );

Also, I would suggest not to name your Clusterer 'cluster', like in your example. The MarkerClusterer contains Cluster objects, which are the actual clustered markers and not the ClusterER itself.