0

I would like to add multiple sublayers of the same table attached to a single layeraction in cartodb js. I written my webpage based on the following cartodb tutorial: http://docs.cartodb.com/tutorials/toggle_map_view.html. Essentially I would like to attach more than a single query to each button, something where you would populate sublayers[0] and [1] perhaps? Does anyone now how to do this?

var LayerActions = {
  all: function(){
    sublayers[0].setSQL("SELECT * FROM ne_10m_populated_places_simple");
    return true;
  },
  capitals: function(){
    sublayers[0].setSQL("SELECT * FROM ne_10m_populated_places_simple WHERE featurecla = 'Admin-0 capital'");
    return true;
  },
  megacities: function(){
    sublayers[0].setSQL("SELECT * FROM ne_10m_populated_places_simple WHERE megacity = 1");
    return true;
  }
}
1

You could create a sublayer inside an action. In this example, clicking 'capitals' button render megacities as well, in red:

var LayerActions = { 
capitals: function(){ 
    sublayers[0].setSQL("SELECT * FROM ne_10m_populated_places_simple WHERE featurecla = 'Admin-0 capital'"); 
    theLayer.createSubLayer({
      sql: "SELECT * FROM ne_10m_populated_places_simple WHERE megacity = 1",
      cartocss: '#table_name {marker-fill: #F00000;}'
    });
    return true; 
}, 

Being theLayer a global variable you set with createLayer layer result.

  • Based on the text above, I added the createlayer statement to the function, referring to the global variable called "layer" and it did not show in the map, do you know why? can an sublayer be created inside on this action? – SiennaAvrilPearl Jan 7 '15 at 21:50
  • Yes, it can. Here's the working example: gist.github.com/juanignaciosl/902887c060856db3236b . It's my modification of the html at the tutorial you pointed. You can see my example code there. – juanignaciosl Jan 8 '15 at 8:24

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.