Mapbox GL JS 2.14.1中text-halo-width属性报错,其他text属性正常
Mapbox GL JS 2.14.1 text-halo-width 属性报错问题解决
问题场景
使用Mapbox GL JS 2.14.1版本配置symbol类型标签图层时,代码如下:
map.addLayer({ 'id': 'labels-layer', 'type': 'symbol', 'source': 'labels-source', 'layout': { 'text-field': ['get', 'description'], 'text-halo-width': 1, 'text-font': ["Lato Bold"], 'text-size': 14, 'text-max-width': 20, 'text-variable-anchor': ['left', 'bottom', 'top', 'right'], 'text-radial-offset': 0.5, 'text-justify': 'auto' } });
执行后触发报错:
Error: layers.labels-layer.layout.text-halo-width: unknown property "text-halo-width"
移除text-halo-width后报错消失,但其他text-halo-*属性也会触发同类错误,官方文档显示这些属性存在,且其他text开头的布局属性可正常工作。
原因与解决方法
这是操作疏漏,不是文档问题。Mapbox GL JS中,text-halo-width、text-halo-color等文本光晕相关属性属于绘制(paint)属性,而非布局(layout)属性,你错误地将它们放在了layout配置块内,导致识别失败。
修正后的代码需要把光晕属性移到paint配置对象中:
map.addLayer({ 'id': 'labels-layer', 'type': 'symbol', 'source': 'labels-source', 'layout': { 'text-field': ['get', 'description'], 'text-font': ["Lato Bold"], 'text-size': 14, 'text-max-width': 20, 'text-variable-anchor': ['left', 'bottom', 'top', 'right'], 'text-radial-offset': 0.5, 'text-justify': 'auto' }, 'paint': { 'text-halo-width': 1, // 如需设置光晕颜色,也在此处添加 // 'text-halo-color': '#ffffff' } });
所有text-halo-*属性都需要放在paint块中,调整后即可正常生效,不会再触发错误。
内容的提问来源于stack exchange,提问作者Joe Germuska
相关产品推荐
相关产品推荐

