如何在传单上取消绑定工具提示

时间:2018-01-22 19:48:48

标签: javascript leaflet

我想在地图缩放到缩放级别以上时编写取消绑定工具提示的代码,问题是以下代码不起作用我尝试过closeTooltip()和unbindTooltip()。

map.on('zoomend', function() {
        var zoom = map.getZoom();
        if( map.hasLayer(zipcodeslayer) ) {
            zipcodeslayer.eachLayer( function (layer){
                console.log(layer);
                if ( zoom >= 14 ) {
                    tooltiptext = layer.feature.properties.ZCTA5CE10;
                    layer.bindTooltip(tooltiptext , { sticky: true ,permanent: true, interactive: false , direction: 'center'});
                } else {
                    //if () {
                    console.log('remove tooltip');
                    layer.closeTooltip();;
                    //}
                }
            });
        }
    })

1 个答案:

答案 0 :(得分:1)

哦,现在通过以下代码工作:

map.on('zoomend', function() {
        var zoom = map.getZoom();
        if( map.hasLayer(zipcodeslayer) ) {
            zipcodeslayer.eachLayer( function (layer){
                //console.log(layer);
                if ( zoom >= 12 && (!layer.getTooltip()) ) {
                    tooltiptext = '<b>'+layer.feature.properties.ZCTA5CE10+'</b>';
                    layer.bindTooltip(tooltiptext , { sticky: true ,permanent: true, interactive: false , direction: 'center', className: 'zipcodes'});
                } else if ( zoom < 12 && (layer.getTooltip()) ) {
                    //console.log('remove tooltip');
                    layer.unbindTooltip();
                }
            });
        }
    });