1

It seems that fitbounds() is not possible with named maps

cartodb.createLayer(map,layerSource)
    .addTo(map)
    .done(function(layer) {
        beams.addLayer(layer);
        map.fitBounds(beams, {maxZoom: 6, paddingTopLeft: [50, 70], paddingBottomRight: [50, 50]});
        map.spin(false);
    });

Any idea how I could use it ?

2
  • fitBounds is a leaflet method and has nothing to do with createLayer. What error do you get? Feb 13, 2015 at 8:27
  • I get : Uncaught TypeError: undefined is not a functioncartodb.js:8 (anonymous function)cartodb.js:8 e.LayerGroup.e.Class.extend.eachLayercartodb.js:8 e.FeatureGroup.e.LayerGroup.extend.getBoundscartodb.js:7 e.Map.e.Class.extend.fitBoundsmain.js:861 (anonymous function)cartodb.js:6 a.Events.triggercartodb.js:20 ccartodb.js:20 (anonymous function)cartodb.js:20 (anonymous function)cartodb.js:6 (anonymous function)
    – Franckl
    Feb 13, 2015 at 15:12

1 Answer 1

2

Unless beams is a length-two array of coordinate pairs, fitBounds won't work. The function definition says:

fitBounds( <LatLngBounds> bounds, <fitBounds options> options? )

Something like this should work:

cartodb.createLayer(map,layerSource)
    .addTo(map)
    .done(function(layer) {
        var beams = [[34,-122],[14,-100]];

        map.fitBounds(
            beams, 
            {maxZoom: 6, 
             paddingTopLeft: [50, 70], 
             paddingBottomRight: [50, 50]
            });

        map.spin(false);
});

It looks like your errors might also come from trying to add a layer to an already-added layer. After createLayer, you chain on .addTo(map)... this adds the layer to the map, so there's no need to use the Leaflet addLayer method.

3

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.