js-markerclusterer
js-markerclusterer copied to clipboard
Hiding markers is still displaying clusters with size 0
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
- Create a map
- Add various markers
- Add a clustrer
- Render it to see everything is fine
- Hide markers dynamically, with a button click for example
- 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 Thank you for opening this issue. 🙏 Please check out these other resources that might be applicable:
- Support Console - If you have a support contract.
- Discord - chat with other developers
-
StackOverflow - use the
google-maps
tag - Google Maps Platform issue trackers
This is an automated message, feel free to ignore.
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.
There are two separate issues going on here.
- The default Algorithm is naive about marker visibility. The changed attribute is based upon a shallow diff of the markers array.
- The default Render still renders with 0.
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?
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?
So far the only workaround, in my case, is to use the clearMarkers
then addMarkers
methods.
I worked around this issue by adding the renderer and setting the visibility of a cluster marker like this:
visible: cluster.count > 0,
I encountered the same problem. If you use clearMarker
first and then addMarker
, the count will be 0.
I encountered the same problem. If you use
clearMarker
first and thenaddMarker
, the count will be 0.
I should have solved the problem by using cluster.markers.length
instead of cluster.count
.