googlearchive/js-marker-clusterer

removeMarkers doesn't remove all markers

fatshotty opened this issue · 2 comments

Hi,
I try to remove all markers in MarkerCluster, but it doesn't remove all of them.

I think problem could be around this line of code

https://github.com/googlemaps/js-marker-clusterer/blob/gh-pages/src/markerclusterer.js#L510

It loops in markers array incrementing the i index, but in removeMarker_ function it performs a splice that changes the length of the array and, then, its indexes.
That causes not all markers will be removed.

Is it only me?

Thanks in advance

Remo commented

@fatshotty I can't reproduce this, removeMarker_ does indeed use splice, but it gets the index with every call to it by using these lines https://github.com/gmaps-marker-clusterer/gmaps-marker-clusterer/blob/master/src/markerclusterer.js#L526-L535

I've tested this with this code and couldn't find any issues:

var markers = [];
for (var i = 0; i < 100; i++) {
  var dataPhoto = data.photos[i];
  var latLng = new google.maps.LatLng(dataPhoto.latitude,
      dataPhoto.longitude);
  var marker = new google.maps.Marker({
    position: latLng
  });
  markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers, {imagePath: '../images/m'});

markerCluster.removeMarkers(markers);

In case I'm missing something, please open a new issues over here https://github.com/gmaps-marker-clusterer/gmaps-marker-clusterer, this repo isn't maintained anymore. It would also help if you could post an example so that we can easily reproduce your problem. Thanks!

Hi,
I think the problem is related to the array passed to removeMarkers function.

....
var markerCluster = new MarkerClusterer(map, markers, {imagePath: '../images/m'});

....

markerCluster.removeMarkers( markerCluster.getMarkers() );

in this case you can see the issue.

Thanks for your reply, I will open a new issue in new repo

Thanks again,
Fabio