I’m working on a web application where I need to exclude labels from rendering if their values are null or empty. In my labelingInfo, the issue arises when labels have no valid value—the background color for the symbol still displays, which results in empty boxes being rendered.
I am attempting to resolve this by dynamically hiding the background when the labels are null or empty. Here is my current implementation:
const textLayer = {
id,
type: 'symbol',
source: sourceId,
layout: {
...(this.textSymbolLineLayoutProperties(line,
textAnchor, labelingInfo)), // to be used for line labels
'text-field': text || textExpression,
'text-rotate': angle ? -angle : 0,
visibility: this.asset.visibility ? 'visible' : 'none',
'text-size': ptToPixel(size),
'text-font': ['Roboto Black', 'Open Sans Regular', 'Arial Unicode MS Regular'],
...(!line ? {
'text-variable-anchor': [textAnchor],
'text-offset': offset,
...(stackLabel ? {
'text-max-width': (labelingInfo.stackRowLength / 2),
} : {}),
} : {}),
'text-justify': 'auto',
...(backgroundColor ? {
// 'icon-image': background,
'icon-image': [
'case',
['==', ['get', ['to-string',
['get', objectIdField?.name]], ['literal', { ...textExpression[2][1] }]], null],
null, // Use the icon if FieldName has a value
background, // Hide the icon otherwise
],
'icon-text-fit': 'both',
'icon-text-fit-padding': Array(4).fill(borderLineSize + 5),
'icon-allow-overlap': !!['none', 'dynamicNeverRemove'].includes(deconflictionStrategy),
} : {}),
'text-allow-overlap': !!['none', 'dynamicNeverRemove'].includes(deconflictionStrategy),
},
paint: {
'text-color': this.rgbaColor(color),
'text-halo-color': this.rgbaColor(haloColor),
'text-halo-width': haloSize || 0,
'text-opacity': opacity,
'text-translate': [ptToPixel(xoffset), ptToPixel(yoffset)],
'icon-translate': [ptToPixel(xoffset), ptToPixel(yoffset)],
// 'background-color': this.rgbaColor(haloColor),
// 'background-color': [],
},
metadata: {
expression,
text: true,
},
...(filter ? { filter } : {}),
};
this.mapAdapter.addLayer(textLayer, this.asset.uuid);