0

I'm applying a Carto named map layer to an existing Leaflet map similar to:

cartodb.createLayer(map,{
    type: 'namedmap',
    user_name: **username**,
    options: {
        named_map: { 
            name: **name**
        }
    }
},{
    https: true
}).on('done',function(layer){
    layer.setAuthToken(**token**);
    layer.addTo(map);
}).on('error', function(err){
    console.log('CartoDB Layer Error: '+err);
});

Our named map templates each cover a city and our UI allows users to change the city without leaving the page, which means I need to update the source being used by this layer.

With a normal Leaflet tileLayer this was as easy as:

layer.setUrl(**URL**)

But is there an equivalent with a Carto layer?

I'd really prefer not completely destroy/recreate the layer from the ground up due to the challenges of keeping its state (hidden/shown), ensuring it stays in the same order in the Leaflet controls, etc.

1 Answer 1

1

You can't setSQL with named maps. However, there are other ways :)

Basically, what you've to do is to create several SQL queries as named map (instead of changing sql in runtime). So, you can maintain data privacy.

I've just created an example for you. It's the layer selector example, but using named maps.

You can check it in this link.

1
  • Right, we're already working with named maps. The problem with your approach is that you're loading all possible layers up front, but our application will one day have hundreds. It'll be impractical to handle that many layers--even if they're hidden--when we only need one of them to show at a time. That's why I'm looking for a way to simply change the data source of the single layer. Basically "resetting" it to a new named map source. Leaflet tileLayers have this capability with a simple setURL() call, so I expected CartoDB to have a similar option.
    – Rikaelus
    Jan 20, 2015 at 6:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.