Marinko Dragojević
Marinko Dragojević

Reputation: 61

Mapbox - clustering only markers on the same coordinates

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?

Upvotes: 1

Views: 1951

Answers (1)

Marinko Dragojević
Marinko Dragojević

Reputation: 61

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);
        }




    }); 
}

Upvotes: 1

Related Questions