How do I create a leaflet layer that can be toggled in the layer control from a cartodb viz.json? I have read that the cartodb.createlayer does not create a leaflet layer. It seems you can add the cartodb table, but of course that takes away the whole point of using cartodb for easy visualization. This seems like a basic function, I am a novice in terms of coding so maybe I am missing something.

  • please break this up into 3 separate questions – Mapperz Jan 31 '15 at 18:23
  • jsfiddle.net/q5uy3bh2/2 – malcolm Jan 31 '15 at 19:35
  • one hack I did was to change the opacity of the tile layer, revealing the cartodb layer but keeping layer control available – malcolm Feb 1 '15 at 18:49

Putting the layer control inside the cartodb callback like this seems to work, but the legend remains. Also not sure how this would apply to multiple cartodb layers, but this essentially answers my question. Setting the index value is key to getting the cartodb layer to draw above the tile layer without losing the leaflet layer control:

 var cdb_url = 'http://ovrdc.cartodb.com/api/v2/viz/52a89830-a8c1-11e4-b072->0e0c41326911/viz.json';
 cartodb.createLayer(map, cdb_url)
   .on('done', function(crashes) {
        var overlayMaps = {
          "Vehicle Crashes": crashes
        L.control.layers(baseMaps, overlayMaps).addTo(map);
   .on('error', function(err) {
     alert("some error occurred: " + err);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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