从地图

时间:2018-03-09 15:11:49

标签: javascript jquery d3.js cartodb

我可以在第一次点击时将卡通图层添加到我的地图中,如下所示。但是,我不确定如何在第二次单击时或以类似切换的方式删除图层?

    $("#toggle").button(); 
    $("#toggle").on('click', firstClick)

    function firstClick() {
        $("#toggle").off('click').on('click', secondClick)
        var $this = $(this);
        $this.text('Heat');

        var cluster = cartodb.createLayer(map, {
                user_name : 'user_name',
                type : 'cartodb',
                sublayers : [{
                    sql : 'select * from lon_oa_residents_geocode_cluster',
                    cartocss : '#layer {marker-width: 7; marker-fill: ramp([cluster_no], (#bef4bf,  #005c02, #89ef8b, #64f567, #00f204, #009c03, #005c02,#000000, #004b01, #003801), quantiles); marker-fill-opacity: 1; marker-allow-overlap: true; marker-line-opacity: 0; }',
                    interactivity : 'cartodb_id'
                }]
            }).addTo(map); 
    }

    function secondClick() {
        $("#toggle").off('click').on('click', firstClick)
        var $this = $(this);
        $this.text('Map');      
        //insert code here to remove the cluster layer
    }

2 个答案:

答案 0 :(得分:1)

<强> layer.hide()

隐藏地图中的图层。

<强> layer.show()

如果之前已添加,则显示地图中的图层。

<强> layer.toggle() 切换图层的可见性并返回指示新状态的布尔值(如果显示图层,则为true;如果隐藏图层,则为false)

unpack

答案 1 :(得分:0)

我的解决方案如下。定义子图层很重要:

function firstClick() {
        //alert("First Clicked");
        $("#toggle").off('click').on('click', secondClick)
        var $this = $(this);
        $this.text('Density');

        var cluster = cartodb.createLayer(map, {
                user_name : 'user_name',
                type : 'cartodb',
                sublayers : [{
                    sql : 'select * from lon_oa_residents_geocode_cluster',
                    cartocss : '#layer {marker-width: 7; marker-fill: ramp([cluster_no], (#bef4bf,  #005c02, #89ef8b, #64f567, #00f204, #009c03, #005c02,#000000, #004b01, #003801), quantiles); marker-fill-opacity: 1; marker-allow-overlap: true; marker-line-opacity: 0; }',
                    interactivity : 'cartodb_id'
                }]
            }).addTo(map, 0).done(function (layer) {
                layer0 = layer; 
            });

        }

function secondClick() {
        $("#toggle").off('click').on('click', firstClick)           
            layer0.hide();
        }