I want to, on right-click on a feature in a layer, have an action execute. I need to know 1 thing: What layer was right-clicked. Does anybody know how I can get this functionality? The API doesn't officially support it.

  • 1
    ...which API??? – DPSSpatial Jun 19 '14 at 18:24
  • CartoDB, sorry. – Aviator45003 Jun 19 '14 at 18:38
  • I would select an answer, but I am no longer working on that project and don't have access to that code. – Aviator45003 Jan 20 '15 at 18:40

A bit weird, but did you try to catch the secondary click event using Leaflet (assuming you're not using Google Maps), and then open a popup or raise a featureClick event in your layer?

Almost pseudo-code here, but...

    // Do something to get Lat, Lng of the point
    var lat = ...;
    var lng = ...;

    var sql = cartodb.SQL({user: 'your_user'});

    // Perform this test with all your layers. Assume that 'layer' is the layer that matches your table1
    sql.execute("select cartodb_id from table1 where st_contains(the_geom, CDB_LatLng(" + lat + "," + lng + "))")

    .done(function(data) {
        if (data.rows && data.rows.length == 1) {
           // You clicked inside that layer, and got 1 point as result.

           // OPTION 1: Open a Leaflet popup
           var popup =  L.popup().setContent('<p>Hello world!<br />This is a nice popup.</p>'); 

           // OPTION 2: Fake a normal click on the layer. 
           layer.trigger('featureClick', null, [lat, lng], null, { cartodb_id: data.rows[0].cartodb_id }, 0);


I am not familiar with CartoDB, but have you tried checking the sender of the event or the event arguments to see if there. You might be able to use a mouse down event, check for the right mouse button, then check the sender to see who it was that prompted the event.


This feature may be added at some point.


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.