代码
var map = new BMap.Map('map', {
backgroundColor: [0, 0, 0, 100],
});
map.centerAndZoom(new BMap.Point(116.404, 39.915), 14);
// 定义掩模区域(模拟一个区域边界)
var maskPoints = [
new BMap.Point(116.35, 39.94),
new BMap.Point(116.44, 39.94),
new BMap.Point(116.46, 39.92),
new BMap.Point(116.45, 39.89),
new BMap.Point(116.4, 39.87),
new BMap.Point(116.35, 39.88),
new BMap.Point(116.33, 39.91),
];
var currentMask = null;
var currentMode = 'inside';
/**
* 添加掩模覆盖物
*/
function addMask() {
if (currentMask) {
map.removeOverlay(currentMask);
}
currentMask = new BMap.MapMask(maskPoints, {
showRegion: currentMode,
isBuildingMask: true,
isPoiMask: true,
isMapMask: true,
});
map.addOverlay(currentMask);
}
/**
* 切换掩模显示模式
*/
function showMask(mode) {
currentMode = mode;
addMask();
}
/**
* 移除掩模
*/
function removeMask() {
if (currentMask) {
map.removeOverlay(currentMask);
currentMask = null;
}
}
// 添加区域边界多边形,方便观察掩模范围
var polygon = new BMap.Polygon(maskPoints, {
fillOpacity: 0,
});
map.addOverlay(polygon);
// 初始化时添加掩模
addMask();
预览