我想在地图缩放到缩放级别以上时编写取消绑定工具提示的代码,问题是以下代码不起作用我尝试过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();;
//}
}
});
}
})
答案 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();
}
});
}
});