0

Here's my code:

        function main() {
            // create leaflet map and define some properties
            var map=    L.map(
                "map",
                {
                    zoomControl: true,
                    center: [40,-95],
                    zoom: 4
                }
            );

            // add a base layer to map
            var Hydda_Full = L.tileLayer('http://{s}.tile.openstreetmap.se/hydda/full/{z}/{x}/{y}.png', {
                attribution: 'Tiles courtesy of <a href="http://openstreetmap.se/" target="_blank">OpenStreetMap Sweden</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
            });
            Hydda_Full.addTo(map);

            // viz.json of map
            var layerURL=           "https://CARTO_USERNAME.carto.com/api/v2/viz/CARTO_ID/viz.json";

            // add CARTO layer with on layer
            cartodb.createLayer(map, layerURL, {infowindow: false, tooltip: false})
                .addTo(map)
                .done(function(layer){
                    layer.setInteraction(true);

                    layer.on("featureClick",function(e, latlng, pos, data){
                            console.log(data);
                        });
                    });
        }

        window.onload = main;

console.log(data) prints an object with only the cartodb_id key and its value. But the dataset on which my Carto map is based has way more columns than cartodb_id.

Why do I only get the cartodb_id column, not the others?

3

Because columns are defined when setting interactivity, different than setInteraction - try this:

.on('done', function(layer) {

   var sublayer = layer.getSubLayer(0); //which sublayer depends on your viz.json

   sublayer.set({ 'interactivity': ['cartodb_id', 'your_column_name'] });

   sublayer.setInteraction(true);

   sublayer.on("featureClick",function(e, latlng, pos, data){
      console.log(data);
   });

})
  • It works, thanks. How can I get a list of all column names so I don't have to copy and paste them all from Carto? – Username Jul 14 '17 at 4:14
  • 1
    var colNames = []; var sublayer = layer.getSubLayer(0); var colQuery = "SELECT column_name FROM information_schema.columns WHERE table_schema = 'your_account_name' AND table_name = 'your_table_name'" $.getJSON('https://your_account_name.carto.com/api/v2/sql/?q=' + colQuery + '&api_key=your_api_key', function(data) { $.each(data.rows, function(key,val) { colNames.push(JSON.stringify(val.column_name)); }); console.log("colNames = " + colNames); }); – Steph Jul 17 '17 at 15:26
  • 1
    @Username be careful with that SQL query, It is useful, but I believe it will also return any deleted column names as well. I've started using SELECT attname::text as column_name FROM pg_attribute WHERE attrelid = 'your_table_name'::regclass AND attnum > 0 AND NOT attisdropped ORDER BY attnum; – Jags Jul 18 '17 at 22:46

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.