代码
const map = new BMap.Map('map', {
displayOptions: {
poi: false,
},
});
map.centerAndZoom(new BMap.Point(116.404, 39.915), 14);
// GeoJSON 格式数据
const data = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {type: 'Point', coordinates: [116.38, 39.92]},
properties: {text: '文字1', color: '#108ee9'},
},
{
type: 'Feature',
geometry: {type: 'Point', coordinates: [116.4, 39.91]},
properties: {text: '文字2', color: '#f50'},
},
{
type: 'Feature',
geometry: {type: 'Point', coordinates: [116.42, 39.93]},
properties: {text: '文字3', color: '#1d7834'},
},
{
type: 'Feature',
geometry: {type: 'Point', coordinates: [116.41, 39.9]},
properties: {text: '文字4'},
},
{
type: 'Feature',
geometry: {type: 'Point', coordinates: [116.39, 39.89]},
properties: {text: '文字5'},
},
],
};
var layer = new BMap.TextLayer({
fontSize: 20,
color(props) {
return props.color;
},
strokeColor: 'rgba(255, 255, 255, 1)',
strokeWeight: 2,
enablePicked: true,
});
layer.addEventListener('click', function (e) {
if (e.value) {
document.getElementById('text').textContent = '选中:' + e.value.properties.text;
}
});
layer.setData(data);
map.addLayer(layer);
通过给 color 属性设置回调的方式可以动态修改颜色
预览