代码
var map = new BMap.Map('map');
map.centerAndZoom(new BMap.Point(116.28, 40.049), 18);
map.enableScrollWheelZoom(true);
var opts = {
position: new BMap.Point(116.2787, 40.0492), // 指定文本标注所在的地理位置
offset: new BMap.Size(30, -30), // 设置文本偏移量
};
// 创建文本标注对象
var label = new BMap.Label('欢迎使用百度地图JSAPI', opts);
// 自定义文本标注样式
label.setStyle({
color: '#fff',
backgroundColor: 'rgba(27, 142, 236, 0.9)',
borderRadius: '8px',
borderColor: 'transparent',
padding: '10px 16px',
fontSize: '14px',
height: 'auto',
lineHeight: '1.5',
boxShadow: '0 2px 12px rgba(0, 0, 0, 0.15)',
letterSpacing: '0.5px',
});
map.addOverlay(label);
通过 BMap.Label 中的 setStyle 方法自定义文本标注样式
预览