I have a map with 5 different sublayers and am having an issue with the infowindows between sublayers. If I click on a sublayer point to open an infowindow and then click on a different sublayer to open another infowindow that first infowindow does not close (I'm assuming it's because it's not part of the same sublayer?). Clicking on individual points of the same sublayer does have the desired behavior of having the first infowindow close when the second one pops up. Here is the code I'm using:

var layerSource = { user_name: 'myuser', type: 'cartodb', sublayers: [{ sql: "SELECT * FROM cartodataset WHERE site_type = 'LUST CLEANUP SITE'", cartocss: $("#site-styles").text() }, { sql: "SELECT * FROM cartodataset WHERE site_type = 'CLEANUP PROGRAM SITE'", cartocss: $("#site-styles").text() }, { sql: "SELECT * FROM cartodataset WHERE site_type = 'MILITARY UST SITE'", cartocss: $("#site-styles").text() }, { sql: "SELECT * FROM cartodataset where site_type = 'MILITARY CLEANUP SITE'", cartocss: $("#site-styles").text() }, { sql: "SELECT * FROM cartodataset where site_type = 'MILITARY PRIVATIZED SITE'", cartocss: $("#site-styles").text() }] }

 var sublayers = [];

 cartodb.createLayer(map, layerSource)
       .done(function(layer) {
           for (var i = 0; i < layer.getSubLayerCount(); i++) {
              sublayers[i] = layer.getSubLayer(i);
              cartodb.vis.Vis.addInfowindow(map, layer.getSubLayer(i), ['site_name','address','site_type','status']);

This same problem occurs when you add multiple viz.json layers to the same map, but this is the first time I've seen it with only one layer source object. I tried a few solutions that I have used for other similar carto problems and none of them seemed to work. I did end up getting it to work by using only one sublayer instead of 5.

sublayers: [{
        cartocss: $("#site-styles").text()

This means you will have to update your layer-control buttons to update the SQL call instead of toggling individual layers. It's a little bit more work but I don't forsee any issues with this solution.

As for why you were having the issue I can only assume it has something to do with having multiple sublayers with the same table source. I don't know why that is, and I couldn't find anything in the carto docs to hint towards why that might be the case.

EDIT For context the original suggested answer was to add the infowindow to the parent layer instead of all the individual sublayers.

  • That only produces an infowindow for the first sublayer. – bazini627 Jun 2 '17 at 3:08
  • Can you set up a plunkr with your issue? Also try having an empty first layer for the sublayers array sublayers: [{}, {sql: '...} Something to do with the first layer being reserved for base layer. – Jags Jun 2 '17 at 3:20
  • Adding the empty first layer seemed to break the functionality of the layer chooser I'm using. Here is the plunker embed.plnkr.co/9kn7AD – bazini627 Jun 2 '17 at 13:40
  • Works like a charm and I changed the selector. Thanks again for the help! bl.ocks.org/bazini627/c3bf5c99efd0f0575adc93bbd715e7c6 – bazini627 Jun 6 '17 at 17:40
  • No worries, if you think the answer is correct could you please mark it as accepted. – Jags Jun 6 '17 at 23:59

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.