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, {})
l.show();
$('#map').append(l.render().el);
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'
}).addTo(map);
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;}'
}]
})
.addTo(map)
.on('done', function (vis,layer) {
var l = cdb.vis.Overlay.create('layer_selector', map.vis, {
layer_selector: true
})
l.show();
$('#map').append(l.render().el);
}).on('error', function () {
console.log("some error occurred");
});
}