代码
var map = new BMap.Map('map');
map.centerAndZoom(new BMap.Point(116.450484, 39.921283), 17);
map.enableScrollWheelZoom(true);
map.setDisplayOptions({
poiText: false, // 隐藏poi标注
poiIcon: false, // 隐藏poi图标
});
map.addEventListener('click', function (e) {
alert('点击位置经纬度:' + e.latlng.lng + ',' + e.latlng.lat);
});
// 自定义canvas
function getTextureCanvas() {
var textureCanvas = document.createElement('canvas');
textureCanvas.width = textureCanvas.height = 200;
var ctx = textureCanvas.getContext('2d');
ctx.fillStyle = '#79a913';
ctx.strokeStyle = 'white';
ctx.lineWidth = 6;
ctx.lineCap = 'square';
ctx.fillRect(0, 0, 200, 200);
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.lineTo(150, 150);
ctx.lineTo(50, 150);
ctx.lineTo(50, 50);
ctx.stroke();
return textureCanvas;
}
// 添加canvas叠加层
var canvasSource = getTextureCanvas();
var pStart = new BMap.Point(116.447717, 39.919173);
var pEnd = new BMap.Point(116.453125, 39.923475);
var bounds = new BMap.Bounds(
new BMap.Point(pStart.lng, pEnd.lat),
new BMap.Point(pEnd.lng, pStart.lat),
);
var canvasOverlay = new BMap.GroundOverlay(bounds, {
type: 'canvas',
url: canvasSource,
opacity: 0.9,
});
map.addOverlay(canvasOverlay);
// 添加文本标注
var opts = {
position: new BMap.Point(116.4503, 39.9213),
offset: new BMap.Size(-28, -20),
};
var label = new BMap.Label('日坛公园', opts);
label.setStyle({
color: '#fff',
borderRadius: '5px',
borderColor: '#fff',
backgroundColor: '#79a913',
fontSize: '16px',
height: '30px',
lineHeight: '30px',
});
map.addOverlay(label);
预览