-1

I would like to draw only one circle(Google.Maps.Circle) when users click on point (Cartodb layer), maybe the last drawn circle could be deleted automatically. This is my code:

//CARTODB layer
        var cartoLayer = cartodb.createLayer(map, {
        user_name: 'username',
        type: 'namedmap',
        named_map: {
        name: "namemap",
        layers: [{
        layer_name: "t",
        interactivity: "cartodb_id, name, coordinateuncertaintyinmeters, class"
        }]
        }
        });
        cartoLayer.addTo(map)
        .done(function(layer) {
          layer.getSubLayer(0).setInteraction(true);
          layer.setZIndex(9);

        // on mouseover
            layer.getSubLayer(0).on('featureOver', function(e, pos, pixel, data) {
        // print data to console log
            console.log("Event #" + data.cartodb_id + ", Name " + data.name + ", Clase: " + data.class+ ",Incertidumbre(m.) " + data.coordinateuncertaintyinmeters );
          });

        layer.on('featureClick', function(e, latlng, pos, data) {

                //DRAW A CIRCLE WHEN CLICK ON ONE POINT

                        circle = L.circle(latlng, data.coordinateuncertaintyinmeters, {
                        color: 'red',
                        fillColor: '#f03',
                        fillOpacity: 0.5
                    }).addTo(map); 
          });
        // show infowindows on click
        cdb.vis.Vis.addInfowindow(map, layer.getSubLayer(0), ['cartodb_id','name', 'coordinateuncertaintyinmeters', 'class']);
         });

Thanks so much!

1 Answers1

0

It's hard to understand what your question is... do you need help deleting an existing circle when a user clicks on the map? I am assuming that's the case. Should be a simple fix - just declare a 'circle' var and add an if statement:

 layer.on('featureClick', function(e, latlng, pos, data) {

      //DECLARE VAR HERE
      var circle = L.circle(latlng, data.coordinateuncertaintyinmeters, {color: 'red', fillColor: '#f03', fillOpacity: 0.5});

      //ADD THIS IF STATEMENT
      if (circle){
         map.removeLayer(circle);
         }
      else{
         circle.addTo(map);
         }; 
      });

Well anyway, not sure if that's exactly what you want, but hope it helps.

skwidbreth
  • 7,888
  • 11
  • 58
  • 105