js-markerclusterer icon indicating copy to clipboard operation
js-markerclusterer copied to clipboard

Hiding markers is still displaying clusters with size 0

Open yanngit opened this issue 2 years ago • 10 comments

Hi, I'm currently building an interactive map with markers. So my code is adding a map with various markers, then adding a clustering and everything is working fine. Issue is appearing when I'm starting to hide some markers, by using marker.setVisible(false) and then rendering again my clusterer, with markerClusterer.render(). I can see on my map some cluster with size 0 appearing where I've hidden some markers.

Environment details

1.Using script api <script src="https://unpkg.com/@googlemaps/markerclusterer/dist/index.min.js"></script> 2. Inside a vuejs app

Steps to reproduce

  1. Create a map
  2. Add various markers
  3. Add a clustrer
  4. Render it to see everything is fine
  5. Hide markers dynamically, with a button click for example
  6. Render again the clusterer

You should see like me clusters of size 0

Code example

Rapid fiddle here : https://jsfiddle.net/2vzayxc4/3/ Click on hide button under the map, and zoom out to see it happening.

Thanks for the help, it's driving me crazy ;)

yanngit avatar Mar 23 '22 00:03 yanngit

@yanngit Thank you for opening this issue. 🙏 Please check out these other resources that might be applicable:

This is an automated message, feel free to ignore.

jpoehnelt avatar Mar 23 '22 00:03 jpoehnelt

I just noticed this as well. I also tried various methods from here such as removing each marker, resetting or using the clearMarkers.

Still returning the size 0.

bensinca avatar Apr 11 '22 19:04 bensinca

There are two separate issues going on here.

  1. The default Algorithm is naive about marker visibility. The changed attribute is based upon a shallow diff of the markers array.
  2. The default Render still renders with 0.

jpoehnelt avatar Apr 12 '22 15:04 jpoehnelt

In the previous markerclustererplus there is an option ignoreHidden that would ignore those markers with visible equal false

Can that be added to this version?

thexs-dev avatar Apr 13 '22 16:04 thexs-dev

The workaround is to remove the marker from the MarkerClusterer when removing from the map or setting visible to false.

There are other related cases that could maybe be addressed:

  • is marker.getMap() the same as that of the clusterer?
  • is marker.getMap() null?
  • marker visibility is false?

jpoehnelt avatar Apr 13 '22 17:04 jpoehnelt

So far the only workaround, in my case, is to use the clearMarkers then addMarkers methods.

bensinca avatar May 18 '22 11:05 bensinca

I worked around this issue by adding the renderer and setting the visibility of a cluster marker like this:

visible: cluster.count > 0,

LeLunZ avatar Oct 03 '22 13:10 LeLunZ

I encountered the same problem. If you use clearMarker first and then addMarker, the count will be 0.

coderwyd avatar Mar 06 '23 09:03 coderwyd

I encountered the same problem. If you use clearMarker first and then addMarker, the count will be 0.

I should have solved the problem by using cluster.markers.length instead of cluster.count.

coderwyd avatar Mar 06 '23 09:03 coderwyd