I'm trying to make my own layer selector (due to the CartoDB one not appearing on mobile devices) and I'm having issues, mainly due to my lack of experience with Javascript.

The issue seems to be most of the tutorials use SQL to pull out data from a single layer, but I can't figure out how to to just pull out layers from a visualization.

I'm basing my code off of http://jsbin.com/sihayino/4/edit , because it seems to work besides that. It produces buttons at the very least.

There are two issues that confuse me,

A) What does this block of code do?

var subLayerOptions = {
sql: "SELECT * FROM estest_all_3"

B) And how do I get this to turn into displaying layers in the visualization, rather than a

sublayers[0].setSQL("SELECT * FROM estest_all_3");

These are the only things in the file I can't figure out how to replace with my information.

I can provide more code if needed.

1 Answer 1



What does this block of code do?

subLayerOptions is just the author's way of storing a set of layer options/parameters that they later set to the layer. It could be SQL/CartoCSS etc. The key bit is,


It is saying, whatever CartoDB thought the initial state of that layer should be, forget it, use the settings I have in my subLayerOptions object.

How do I get this to turn into displaying layers in the visualization

This is an example of the first steps I would take to make it happen,


Basically, using the layer.show() and layer.hide() functions plus some organization

  • Bonus question: How are the layers called? Lets say the first layer is called "Poverty." The following do not work: layers.getSubLayer("Poverty").hide(); layers.getSubLayer(Poverty).hide(); layers.getSubLayer(poverty).hide(); layers.getSubLayer("poverty").hide(); layers.getSubLayer(0).hide(); layers.getSubLayer(1).hide();
    – user29032
    Commented Apr 27, 2014 at 23:32

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.