Using the same theory to create a search box at runtime, (such as here), I wrote this code:

var l = cdb.vis.Overlay.create('layer_selector', map.viz, {})

But this doesn't work as anticipated. The layer selector doesn't fill with any layers. It tells me I have 0 layers.

Can someone tell me how to get a layer selector at runtime, using the cartodb layer selector?

EDIT: I've taken the bits of my code related to this:

function main() {
var map = new L.Map('map', {
    zoomControl: false,
    center: [40.7142, -74.0064],
    zoom: 11
L.tileLayer('http://tile.stamen.com/toner/{z}/{x}/{y}.png', {
    attribution: 'Stamen'

cartodb.createLayer(map, {
    type: 'cartodb',
    options: {
        user_name: '',
        table_name: '',
        extra_params: {
            map_key: ''
    sublayers: [{
        sql: "SELECT * FROM roads",
        cartocss: '#roads{line-color: #FF6600;line-width: 2;line-opacity: 0.7;}'
    .on('done', function (vis,layer) {

    var l = cdb.vis.Overlay.create('layer_selector', map.vis, {
        layer_selector: true

}).on('error', function () {
    console.log("some error occurred");
  • Please, could you share your entire code?
    – Xatpy
    Jan 19, 2015 at 16:55

2 Answers 2


You were doing it basically right, check this full example:


  • This doesn't appear to work when creating a map at runtime Jan 18, 2015 at 22:16

There are examples in the official documentation, here and code here.

I don't use their API, though, so someone else might have a specific example.

Try something like this:

var l = cdb.vis.Overlay.create('layer_selector', map.viz, {layer_selector: true})

I think it's just an option to set, which by default is 'off' for layer_selector. See here. "layer_selector: show layer selector (default: false)"

  • This doesn't answer my question. I want the cartodb layer selector to work. Jan 13, 2015 at 3:23
  • Try my next suggestion above.
    – Alex Leith
    Jan 13, 2015 at 21:27
  • That doesn't work either Jan 13, 2015 at 22:17
  • Set up a jsfiddle.net and we can take a look.
    – Alex Leith
    Jan 14, 2015 at 0:10

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.