In-class Excercises: CartoDB.js
Part 1: Add a done
handler
- Start with the code from last time. If you don't have it handy, you can work with this jsbin.
- Add a
done
handler tocreateVis()
. - Here's a start, building from the last part:
$(document).ready(function () { cartodb.createVis('map', /* Your CartoDB visualization's CartoDB.js URL */, { option1: option1-value, option2: option2-value, option3: option3-value }) .done(function (vis, layers) { // layers[1] contains the visualization's layers }); });
- Use
console.log()
to convince yourself that thedone
callback is called. - Hide the layer containing your visualization's layers. All of the methods you can call on layers are in the documentation.
Part 2: Get and set a sublayer's SQL
- Start with the code from Part 1.
- In the
done
callback, usegetSubLayer()
to get a visualization layer from your visualization. - Once you do, use
console.log()
to print out the results ofgetSQL()
on the layer. - Use
setSQL()
on the layer to filter the data.
Part 3: Set a sublayer's SQL in reaction to a button click
- Start with the code from Part 2.
- Add a button to your page.
- In the
done
callback, usegetSubLayer()
to get a visualization layer from your visualization. Store this in a variable that is declared outside of thedone
callback. - When the button is clicked, use
setSQL()
on the layer to filter the data.
Part 4: Using Leaflet plugins through CartoDB.js
- Start with the code from Part 3.
- Add the code for the Mapzen Search Leaflet plugin to your page. This is step 1 in the plugin's documentation.
- Skip step two in the plugin's documentation—CartoDB takes care of this for you.
- In the
done
callback usevis.getNativeMap()
to get the Leaflet map that underlies the visualization and initialize the plugin. This is step three in the plugin's documentation.