1

I am trying to change the color of my map layer when filter buttons are selected. So, the goal is: the user sees all of the data in one color. Then, when they click on a filter button, only a subset of the data is shown and in a different color.

I am looking for a command to call the previously defined styles when buttons are selected.

I have several map styles set up like this, each with a different polygon-fill (bluestyle, redstyle, etc):

<style type="cartocss/html" id="bluestyle">
        #blue{
          polygon-fill: #11799F;
          polygon-opacity: 0.7;
          line-color: #FFF;
          line-width: 0.5;
          line-opacity: 1;
        }


    </style>

Then, the code below (much of which is based on CartoDB's interactivity tutorial). Note I added "**" around the spot where I need to plug in this style change.

  function createSelector(layer) {
            var condition = "";
            var $options = $(".layer_selector").find("li");
            $options.click(function(e) {
                var $li = $(e.target);
                var selected = $li.attr('data');
                var type = $li.data('type');
                if (type === "cartocss") {
                    $options.removeClass('cartocss_selected');
                    if (selected !== "bluestyle") {
                        $li.addClass('cartocss_selected');                      
                    }
                    condition = $('#'+selected).text();
                    layer.setCartoCSS(condition);
                }  else {
                    $options.removeClass('sql_selected');
                    if (selected !== "") {
                        $li.addClass('sql_selected');
                    }
                    if (selected.indexOf('Button1') !== -1) {
                        **(some code that will change the style to redstyle)** ;
                    } else {

                    }
                    layer.setSQL("SELECT * FROM " + tableName + selected);
                } 
            });
        }

I am new to Javascript.

Your Answer

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

Browse other questions tagged or ask your own question.