2

Is there a good way to change leaflet basemaps from cartodb.js or leaflet.js? I am able to grab the map object and basemap layer from the layers property in the done() promise.

cartodb.createVis(map, layerUrl, mapOptions)
 .done(function(vis, layers) {
 var map = vis.getNativeMap();
 var basemap = layers[0];
 })
 .error(function(err) {
 console.log("An error occurred: " + err);
 });

Right now I'm thinking I need to be able to grab tileLayer from the basemap object or the map object, then use leaflet's setUrl method as per http://leafletjs.com/reference.html#tilelayer-seturl, but so far no luck there.

til
8946 silver badges12 bronze badges
asked Apr 16, 2014 at 20:59

1 Answer 1

1

Yeah, really close, you'll need to set it on the basemap, which is actually layer[0], when you are using createVis at least. So here is how it would work,

cartodb.createVis('map', layerUrl)
 .done(function(vis, layers) {
 var basemap = layers[0];
 basemap.setUrl('http://{s}.tile.osm.org/{z}/{x}/{y}.png');
 })
 .error(function(err) {
 console.log("An error occurred: " + err);
 });

Of course, if you want to reference basemap outside the done promise, make sure it has a different scope. Otherwise you should be set.

If your visualization in CartoDB didn't have a basemap, you would need to add one to your layers instead of just updating layer[0].

answered Apr 29, 2014 at 20:14

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.