-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to cluster custom markers ? #8072
Comments
You're either looking for https://docs.mapbox.com/mapbox-gl-js/example/cluster/, or a more advanced https://docs.mapbox.com/mapbox-gl-js/example/cluster-html/ |
@mighty-nyaina and anyone else in need of help please see my answer here. |
@mourner I don´t think thats the case TBH. You are referring the cases where custom html Clusters are rendered, or clusters with layered points for markers. but the person, and many of us, are looking into clustering custom markers. The problem with this approach is that we need to remove custom markers from the map when they are inside the layered generated cluster, and vice-versa. |
@mighty-nyaina / @luchux Did (any of) you ever find a solution to this ? I'm having the same issue. I'm adding markers not based from a json, but from user entered values, which works, but I have 4 locations within 1 square kilometer, which I want to cluster, but can't seem to get it done with the provided examples. |
@Beee4life I have applied this solution:
In this way you get the "spaces" data, and the clusters calculated when you zoom in-out by the native Mapbox APIs.
Above, you get the spaces features from the map and you can filter those that are not clusters, (those inside clusters will not appear as features in the map). From here on you can traverse the list of your unique features, and check if you need to add or remove those markers to your custom markers list (if they are already rendered, don´t render them, if they are not render them, and those that shouldn´t be rendered and are rendered remove from markers). Keep a list of them in a state, and you will be able to re render and remove on zooming behaviour.
|
Thank, appreciate the feedback... I will try to look into it asap... |
@luchux I have looked but can't wrap my head around it.... I have uploaded my code here to give you a better look at it (if you would like to)... The map is built here and the (added) settings are set here. Lines 51 - 103 of the js file are new/added and aren't needed for the map which is shown here. |
Is there a good solution for this, I also add markers manually to the map and can't find a way to cluster them. |
I think you will need to do this manually, there is no implented way of doing this inside mapbox. Atleast that's my experience with mapbox. My solution to this was to check all the markers inside the camera and check if any of those collided with each other. Those who did was added into different "cluster"-arrays, and when all the collision has been checked, I looped through the array and added new markers between the ones that collided. At the same time i hid the markers that was now inside the cluster group. I hope this can help someone into the right direction. |
Found this video/article/code that explains it perfectly https://www.leighhalliday.com/mapbox-clustering |
In case someone is still looking for a solution/ workaround without the need of an additional package: You can combine adding a clusters layer with adding markers via symbol layer. That is, on loading your map instance, you add your data source as well as a custom image, as described here https://docs.mapbox.com/mapbox-gl-js/example/geojson-markers/:
Then you add your clusters layers as described in https://docs.mapbox.com/mapbox-gl-js/example/cluster/. The important part is the addLayer() function for the unclustered points:
Make sure that the custom name in the icon-image property matches the name provided in the map.addImage() function. The Mapbox API and its filters per layer should now resolve each unclustered data point of your data to the marker image you provided. |
Looking for something similar to this, where I can combine markers and clusters dynamically: https://svelte-maplibre.vercel.app/examples/custom_marker_clusters The examples above seems to use LabelStyling, which doesn't enable the same visual effect. Something I'm missing here? |
Hi!
How can I cluster custom markers ? I did not find anywhere how to cluster marker like Google Maps for example
https://developers.google.com/maps/documentation/javascript/marker-clustering
That is, clustering markers and when we zoom the cluster circle transforms to markers
Thanks in advance for your help
The text was updated successfully, but these errors were encountered: