//str为label的文字内容,fontsize为文字的大小
const getLenPx = (str, fontSize) => {
const str_leng = str.replace(/[^\x00-\xff]/gi, 'aa').length;
return (str_leng * fontSize) / 2;
};
在marker中设置label:
const marker = {
id: id,
iconPath: iconUrl,
latitude: latitude,
longitude: longitude,
width: width,
height: height,
label: {
content: content,
color: 'rgba(51, 51, 51, 1)',
fontSize: 12,
anchorX: -(0.5 * getLenPx(content, 12)),
textAlign: 'center',
anchorY: 0,
},
}