I'm loading a CartoDB visualization and I want all layers to be switched off as the default setting, so users have to switch them on using the layer selector loaded with the visualization.

I'm reallly new on leaflet and cartodbjs so not sure where to begin to dive in the code. I think is related with this question but it doesn't have any answer.

I have a sample code on gist (demo).

3 Answers 3


find attached some example code. You can access the layers/sublayers through the javascript API layer[0] is the basemap and layer1 is the main layer with your two sublayers.

Anyway I recommend you creating this visualization with CreateLayer as when hiding the sublayers the toggle layers controls won't update.


  • Hi @matallo, you code works but the layer selector is not updated with the change of the status of the layer. I thought that the code should interact with it but I haven't found a method on the overlay to do that.
    – Jorge Sanz
    Aug 4, 2014 at 9:40
  • Ohh, you mentioned it, sorry :-) I'll use CreateLayer as you say to have more control over the process.
    – Jorge Sanz
    Aug 4, 2014 at 9:49

Wladimir find a better solution interacting with the overlays


          //get the data layers
          var overlays = layers[1].getSubLayers();
          overlays.forEach(function(overlay) {

          //get the layer selector control
          var control = vis.getOverlay("layer_selector");
          //redraw layer selector to update the states

Here you can see a code that hides the layers when the map starts and updates the control layer selection. https://gist.github.com/bolollo/28f1447492232da0b7af

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.