andy shon
andy shon

Reputation: 17

Markercluster in Mapbox

I am following markercluster examples from Mapbox library, but can't solve my problem. If you guys take a look at my working example here, you will notice this line of code:

L.mapbox.featureLayer(markerLayer).on('ready', function(e) {

What I initally thought was I could put markers inside of markercluster featureLayer, but I guess it was a wrong approach. Any solutions? Thanks.

Example following here

Upvotes: 0

Views: 566

Answers (1)

ghybs
ghybs

Reputation: 53185

The mapbox example you refer to makes an AJAX call to retrieve the GeoJSON data, hence it needs to attach an on "ready" listener.

In your case your GeoJSON data is defined in your scripts, so the "ready" event will not be triggered (besides, you should use L.mapbox.featureLayer with your GeoJSON object directly, not a Feature Layer).

You can simply use the eachLayer method to iterate through all created markers within the Feature Layer, and add them into your Marker Cluster Group.

var clusterGroup = new L.MarkerClusterGroup();

var markerLayer = L.mapbox.featureLayer(markers).eachLayer(function(layer) {
  clusterGroup.addLayer(layer);
});

map.addLayer(clusterGroup);

Updated Plunker: http://plnkr.co/edit/fN6xYcn1Lg532eLe39IS?p=preview

Upvotes: 2

Related Questions