1
votes

Is it possible to cluster only markers on the same coordinates? When using mapbox marker clustering all markers are grouped depending on map zoom level. What I would like to do is to have all markers independent (non grouped) except markers on the same latitude-longitude coordinates.

Is that possible?

1
That is not clustering (see wikipedia), but duplicate coordinate detection, a much simpler problem. - Has QUIT--Anony-Mousse
Have you even read my post?The task is to GROUP markers with the same coordinates and DON'T GROUP others. - Marinko Dragojević
Yes. You want to group duplicates, and not do clustering, but your question title says "clustering" (see Wikipedia for clustering) - Has QUIT--Anony-Mousse
The obvious (and likely best) solution is to merge duplicate coordinates a single marker prior to adding them to the map. - Has QUIT--Anony-Mousse

1 Answers

1
votes

This is not a perfect solution but it will do the job... I've solved my problem by using two layer groups, one for individual markers and one for clustered markers.

Something like this:

var overlay1 = L.layerGroup().addTo(map);//this one is for single markers
var overlay2 = L.layerGroup().addTo(map);//this one is for clustered

var layers;


//load markers from external source
var featureLayer = L.mapbox.featureLayer()

.loadURL('/my_geojson_script/')

.on('ready', function(e) {

    layers = e.target;

    //go and do the filtering
    doTheThing();



})





function doTheThing()

    overlay1.clearLayers();//remove all single markers
    overlay2.clearLayers();//remove all clustered

    var clusterGroup = new L.MarkerClusterGroup().addTo(overlay2);



    layers.eachLayer(function(layer) {


        //the number of markers on this layer coordinates (info collected from json property. I calculate this in advance)
        var numberOfMarkers = layer.feature.properties.numberOfMarkers;


        //if number of markers is greater than 1 add layer to cluster group         
        if(numberOfMarkers>1){

            clusterGroup.addLayer(layer);
        }
        else{//if number of markers is 1 add layer to individual layer group

            overlay1.addLayer(layer);
        }




    }); 
}