question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Toggling layer visibility with a checkbox and updateTriggers

See original GitHub issue

Description

I am facing issue with implementing a checkbox to turn on or off a layer in pure js using updatetriggers. I have looked at the documentation here and here.

I have this layer with the visible property controlled by a function “enabled”. I added the updateTriggers for the visible property.

    const myIconlayer = new IconLayer({
        id: 'icon-layer',
        data:educationData.features,
        pickable: true,
        iconAtlas: './schooltr.png',
        iconMapping: ICON_MAPPING,
        getIcon: d => 'marker',
        
        visible: enabled,
        updateTriggers: {
          visible: [true, false],
        },

        sizeScale: 6,
        getPosition: d => d.geometry.coordinates,
        getSize: d => 5,
        getColor: d => [Math.sqrt(1000), 140, 0],
      });

      deckgl.setProps({
        layers: [hexagonLayer,myIconlayer],
        viewState: Object.assign({}, currentViewState)
      });

I am updating the visibility with the following enabled function that reads a checkbox element id.

    function enabled() {
      // Get the checkbox
      var checkBox = document.getElementById("Education");

      // If the checkbox is checked, display return true
      if (checkBox.checked == true){
        console.log("checkbox true")
        return true    
      } else {
        console.log("checkbox false")
        return false
      }
    }

However nothing happens on the map, it doesnt seem to change the visibility of the layer. However when i do it manually (replacing with false and true the visible prop) it works.

Here is an example of the working app. https://covmob-website.herokuapp.com/ image

What am I missing to make the checkbox turn on and off the visibility of the layers?

Environment (please complete the following information):

  • Framework Version: deck.gl latest
  • Browser Version: Chrome Version 81.0.4044.129
  • OS: W10 64bit

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

1reaction
alitarrafcommented, Aug 26, 2020

Hi, this is how I worked it out. Also checkout the source code for this example https://deck.gl/gallery/hexagon-layer. renderlayer function and also you can have a look at http://sets-cloud.com/covrex/

You have the renderlayer() function:

// here you are creating the option for the layer to be visible or not depending on wether the checkbox is checked or not.
    let optionsEducation = {
        visible: document.getElementById('Education').checked
      };

//here you are defining your layers parameters
const educationLayer = new IconLayer({
        id: 'education-layer',
        data:educationData.features,
        pickable: true,
        iconAtlas: './img/school.png',
        iconMapping: ICON_MAPPING,
        getIcon: d => 'marker',
        sizeScale: 5,
        getPosition: d => d.geometry.coordinates,
        getSize: d => 5,
        onHover: updateTooltipPOI,
        ...optionsEducation // <<<<<<<--------notice here we are using the optionsEducation variable in the layer
      });

// here you are setting the deckgl prop to render your layer
 deckgl.setProps({
        layers: [educationLayer],
        viewState: Object.assign({}, currentViewState)
      });
1reaction
Pessimistresscommented, May 6, 2020

You are correct that layer props cannot be changed after creation. Even updateTriggers cannot be changed. You must create a new instance of the layer if you want to update a prop. See this example.

You can learn more about the reactive programming model in https://deck.gl/#/documentation/developer-guide/using-layers

Read more comments on GitHub >

github_iconTop Results From Across the Web

Layer Class - deck.gl
The Layer class is the base class of all deck.gl layers, and it provides a ... Under most circumstances, using visible prop to...
Read more >
How to easily toggle layers visibility with a checkbox control ...
One of them is the ability to hide an object from the viewport, but have it show up in the render. There are...
Read more >
Toggle Layer Visibility With a Checkbox Control in After Effects
Great tutorials! But I think “layer visibility” (your title) is the eye icon on the left of a layer, which can't be controlled...
Read more >
toggle visibility of a Layer in Openlayers 3(!) with a Checkbox
You need to call setVisibility(true/false) on the layer. setVisibility. All you are doing in your code is changing the value of static ...
Read more >
Solved: Toggle Feature Layer with a checkbox - Esri Community
Solved: What is the simplest way to use a checkbox input to toggle the visibility of a featureLayer?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found