-1

当用户单击点(Cartodb 层)时,我只想绘制一个圆圈(Google.Maps.Circle),也许最后绘制的圆圈可以自动删除。这是我的代码:

//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']);
         });

非常感谢!

4

1 回答 1

0

很难理解您的问题是什么……当用户点击地图时,您是否需要帮助删除现有圈子?我假设是这样的。应该是一个简单的修复 - 只需声明一个 'circle' 变量并添加一个 if 语句:

 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);
         }; 
      });

无论如何,不​​确定这是否正是您想要的,但希望它有所帮助。

于 2015-10-10T00:01:38.713 回答