代码
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'},
},
{
type: 'Feature',
geometry: {type: 'Point', coordinates: [116.4, 39.91]},
properties: {text: '文字2'},
},
{
type: 'Feature',
geometry: {type: 'Point', coordinates: [116.42, 39.93]},
properties: {text: '文字3'},
},
{
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: '#333',
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);
BMap.TextLayer 文字图层,适用于大数据量文本上图的场景,默认取 properties.text 当作文本,你也可以设置回调
预览