0

Can you hide/show the sublayers of a layer using Carto's createVis method? I know I can easily hide/show the layers using createVis, but what about the sublayers in a vis layer? I've been trying out different methods, but haven't been successful. Is it possible and if so is it better to just use createLayer instead?

Full script:

var torqueToggle = document.getElementById('torque-toggle'),
    lineToggle = document.getElementById('line-toggle'),
    visjson = "https://powereng.carto.com/api/v2/viz/7e67597e-8e69-4663-83f8-90dc27c02ed9/viz.json",
    options = {
      center: [43.60000432144408, -116.26367568969728],
      zoom: 13,
      shareable: false,
      cartodb_logo: false,
      fullscreen: true,
      mobile_layout: true
    }

cartodb.createVis('map', visjson, options)
    .done(function(vis, layers) {
      layers[2].hide();
      layers[1].setZIndex(9999999999999999);
      console.log(layers);
      torqueToggle.onclick = function() {
        if (layers[2].hidden == false) {
          layers[2].hide();
        } else {
          layers[2].show();
        }
      }
      lineToggle.onclick = function() {
        if (layers[1].getSubLayers(0).hidden == false) {
          layers[1].getSubLayers(0).hide()
        } else {
          layers[1].getSubLayers(0).show()
        }
      }

    })
    .error(function(err) {
      console.log(err);
    });

and i've tried

layers[1].getSubLayers(0).hide()

and

layers[1].layers[0].hide()

JSFiddle

  • If you are powereng user, I would suggest using CARTO's Enterprise support next time. Disclaimer: I work at CARTO. – ramiroaznar Apr 30 '18 at 15:57
  • I appreciate the help. Turns out it was the torque layer preventing me from toggling the sublayers for some reason. I deleted the heat map and everything was working as intended. – Hysteresis May 1 '18 at 17:49
0

BUILDER vizjsons are not compatible with neither cartodb.js v3.15 or CARTO.js v4 as explained on CARTO's new documentation.

Because cartodb.js v3.15 is not maintained, I will suggest using the brand new CARTO.js. You will need to create an application from the ground up. Thus loading your two layers setting the SQL data source and CartoCSS style, and finally, using show()/hide() layer methods to toggle your layers. Finally, notice that heatmaps (made by Torque.js) are not still supported in CARTO.js.

| improve this answer | |
0

Okay, so I figured out my issue. It was the torque layer/heat map preventing me from accessing the sublayers for some reason. Once I deleted that heat map layer in the Carto builder, I was able to hide and show the sub-layers.

| improve this answer | |
  • Happy to hear that removing the heatmap did the trick. But AFAIK, you are going to encounter more problems mixing BUILDER vizjsons and cartodb.js v3... – ramiroaznar May 2 '18 at 16:55

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.