1

This gis.stackexchange question asks how a layer can be displayed after initializing a named map.

I don't have enough reputation to ask a follow-up comment to javisantana's response. It reads in part:

Why didn't you use cartodb.js to get that layergroup? if you are going to use 
cartodb.js it's better to use it to get that layergroup

My question is: how do you use cartodb.js to do that? I can do it just fine using the instructions on the Maps API page and can also successfully create a leaflet tileLayer in a fashion similar to that which javisantana's suggests. However, I would love to be able to use cartodb.js and then be able to do a bit more than just display a static tileLayer (e.g. replace the named map's template variables at client run time).

It seems like it might be possible to do this calling cartodb.createLayer, but my attempts to provide layermetadata have not been successful. The documentation on the cartodb.js page is a little unclear:

cartodb.createLayer(map, { layermetadata })
Layer metadata is always in the form: { type: 'LAYER_TYPE_NAME', options: {...} }
1

Not sure why you need the layergroupid, I guess you are trying to create a layer and then change the values.

creating a layer is a simple as this:

cartodb.createLayer(map, { user_name: 'examples', type: 'cartodb', sublayers: [{ sql: 'select * from country_boundaries', cartocss: '#layer { polygon-fill: #F00; polygon-opacity: 0.3; line-color: #F00; }' }] })

full example here: https://github.com/CartoDB/cartodb.js/blob/develop/examples/leaflet_multilayer.html

If you have a named map and you want to change the template params the way to do is:

cartodb.createLayer(map, {
          type: 'namedmap',
          user_name: 'documentation',
          options: {
            named_map: {
              name: 'tpl_b47ff8ec_e7eb_11e3_894f_0e230854a1cb'
            }
          } })

In order to change template params you should use setParam:

cartodb.createLayer(map, {....}).done(function(layer) { layer.setParam('test', 1); })

see the doc at: http://docs.cartodb.com/cartodb-platform/cartodb-js.html#layersetparamskey-value

  • Thank you. The latter part that pertained to the named map is what I was looking for. I know the documentation is a work in progress, but it does leave a bit to be desired. Is there anywhere I can find, for instance, a thorough explanation of all of the options that can be passed as the {layermetadata} json object when calling createLayer? For example, how would I have known to pass "named_map : {name: 'mynamedmap'}"? – susie derkins Dec 30 '14 at 20:30
  • I was confused and thought I needed the layergroupid, though i see from your response that I didn't. I have since edited my answer to remove the layergroupid aspect in case others are interested more generally – susie derkins Dec 30 '14 at 21:09
  • @susiederkins we are working on "layermetadata" doc lately. Thanks for your feedback – javisantana Jan 1 '15 at 9:18

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.