Question

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?

Was it helpful?

Solution

You should store the marker object in a var and then unset the map as following:

var markerCluster = new MarkerClusterer(map, markers);
/// ... later on
markerCluster.setMap(null);

after you've done this, you could init a new MarkerClusterer with new markers

Update

since you are using google maps ui plugin here's some additional code. I've added a click even on a button with class reset_markercluster ofcourse this is just to show how to use it to call the map

var _map, _markerCluster;

$(function() {
  $('#map_canvas').gmap().bind('init', function(event, map) { 
    _map = map; // at this point you can call _map whenever you need to call the map

    // build up your markers here ...

    _markerCluster = new MarkerClusterer(_map, markers);  // you could also use map instead of _map here cause it's still present in this function
  });

  $("button.reset_markercluster").click(function(e) {
    e.preventDefault();
    _markerCluster.setMap(null);  // remove's the previous added markerCluster

    // rebuild you markers here ...

    _markerCluster = new MarkerClusterer(_map, newMarkers);

  });
});

OTHER TIPS

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.

It's best to use the clearMarkers() method from your markerCluster object:

http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/reference.html updated reference: https://googlemaps.github.io/js-markerclustererplus/classes/markerclusterer.html#clearmarkers

marker-clusterer-plus has a removeMarkers method:

markerCluster.removeMarkers(markers);

You can manage like this.

  1. Create blank cluster variable

    var _markerCluster;

  2. Store cluster object on our variable.

    _markerCluster = new MarkerClusterer(map, markers, { imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m', });

  1. When you remove marker then you have need to write bellow code.
    try{
         _markerCluster.clearMarkers();
    }catch(e){
    
    }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top