block by mpmckenna8 2725ea439744db4a643e

CartoDB SQL API simple exameple

Full Screen

So getting the map layers from CartoDB is nice and easy as shown in the last bl.ock but at first I was having trouble understanding how to access all the data I generated the map with throught the layers part of the API, and that’s because you can’t. Well, you can indirectly by the user clicking on a object with a pop up, and for each click a API call is made and the appropriate information to fill in the popup of the given feature will load into the popup.

So what if I wanted to display a list of all the parks in the layer I had, I don’t want to do a request mimicking clicks on each feature cause that would suck. CartoDB has an SQL API to make queries of data from your databases, and you can use all sorts of Postgis spatial query features to get at that data and have it returned to you in a nice json format so you can pop it into your webpage nice and quick and easy. Plus you get a cartoDB id which might make it easy to call events from the main layer. Because there are features in the source database (parks in this example) features which share the same unit_name attribute the sql query uses the distinct keyword so I can make a list of all the park names which fills along the left side of the page. Check out the code and let me know if there are any stupidities or if it helped you somehow.

I decided to make these simple bl.ocks because my first attempt at using the CartoDB api was with a little project which I started with Bootleaf so the CartoDB related stuff was dispersed over a few files and wa kind of a jenky implementation. Please check it out and give me some feedback or let me know if you’re having trouble getting it to work. It should basically be a more complicated version of this map but with campsites too, and a nice search box.

index.html