update markercluster after removing markers from array

jaget picture jaget · Nov 22, 2011 · Viewed 45.8k times · Source

I am currently using markercluster plugin with jquery ui maps.

I have two arrays one of all markers (called markers) and one of markers that match search criteria (called current_markers). These are fitered from the first array.

I then draw the current_markers on screen.

I am finding however that the markerclusterer library is not updating based on this change.

So how can I update the markerclusterer?

Is it possible to assign the markerclusterer to a variable and call an update function?

Answer

superluminary picture superluminary · Jun 24, 2014

Yes you can.

Creating the map

Assuming you have created your MarkerClusterer object something like this:

var center = new google.maps.LatLng(10, 20);
var map = new google.maps.Map(document.getElementById('map'), { zoom: 6, center: center, mapTypeId: google.maps.MapTypeId.ROADMAP });
var markerClusterer = new MarkerClusterer(map);

Adding markers

You can add multiple markers to it something like this:

var markers = []
var marker = new google.maps.Marker({position: center});
markers.push(marker);
markerClusterer.addMarkers(markers);

Note that here I have added only one.

Removing all markers

You can then clear all the markers using clearMarkers something like this:

markerClusterer.clearMarkers();
markers = [];

Note that for tidiness I have also unset the markers array here.

Docs

Full documentation on all the available methods is available here:

https://googlemaps.github.io/js-marker-clusterer/docs/reference.html

UPDATED Link: https://googlemaps.github.io/js-markerclustererplus/classes/markerclusterer.html#clearmarkers

It's a sensible and relatively complete API.