I have create a visualisation in cartodb with 4 layers. The resulting viz.json is added to a leaflet map as follows:

cartodb.createLayer(cartomap, [url], {https:true})
                .on('done', function(layer) {
                // add individual layers

I want to accomplish that all 4 layers in the viz.json can be toggled separatly in the layer control (switching both the markers/features and the legend)

The layer in on('done') is a valid leaflet layer and can be added to the Layer Control. However, it contains all sublayers which can then not be toggled individually.

The layer also contains an array with all the "sub" layers in there, but these are not valid Leaflet layers (onAdd function is missing), so these can not be added to the layerControl individually. The layer.getSubLayer(i) also returns a not valid Leaflet layer.

1 Answer 1


Sublayers are not leaflet layers as you say. CartoDB sends all the layers merged in a single tile so you can't manage them using leaflet controls.

You have two solutions here:

  • create your own layer selector (or extend leaflet one) in order to use show/hide for each sublayer

  • use createLayer 4 times creating a single leaflet layer for each sublayer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.