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;
 }
}
asked Jan 5, 2015 at 21:41

1 Answer 1

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.

answered Jan 7, 2015 at 11:06
2
  • 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? Commented Jan 7, 2015 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. Commented Jan 8, 2015 at 8:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.