你可以通过 BMap.Rectangle 在地图上绘制矩形,矩形通过西南角(左下角)和东北角(右上角)两个坐标点确定区域范围。
// 创建矩形var sw = new BMap.Point(116.38, 39.9);var ne = new BMap.Point(116.42, 39.93);var bounds = new BMap.Bounds(sw, ne);var rectangle = new BMap.Rectangle(bounds, {strokeWeight: 2,});map.addOverlay(rectangle);
创建 Rectangle 时,可以通过第二个参数设置矩形的属性,常用属性如下:
strokeColor:设置矩形边框颜色,如 "blue"、"#ff0000" 等。
strokeWeight:设置矩形边框宽度,数值类型,默认值为 2。
strokeOpacity:设置矩形边框透明度,取值范围 0.0 ~ 1.0。
strokeStyle:设置矩形边框样式,可选 "solid"(实线)或 "dashed"(虚线)。
fillColor:设置矩形填充颜色,如 "#b9ddfa"。
fillOpacity:设置矩形填充透明度,取值范围 0.0 ~ 1.0。
enableEditing:是否启用矩形的编辑功能,默认 false。编辑效果见 编辑矩形示例
var rectangle = new BMap.Rectangle(bounds, {strokeColor: "blue",strokeWeight: 4,strokeOpacity: 0.8,strokeStyle: "solid",fillColor: "#b9ddfa",fillOpacity: 0.5,enableEditing: false,});
矩形添加到地图后,可以通过实例方法动态更新属性,常用方法如下:
// 修改矩形区域rectangle.setBounds(new BMap.Bounds(new BMap.Point(116.35, 39.88),new BMap.Point(116.45, 39.95)));// 修改样式rectangle.setStrokeColor("red");rectangle.setStrokeWeight(6);rectangle.setStrokeOpacity(0.5);rectangle.setStrokeStyle("dashed");rectangle.setFillColor("#ffcc00");rectangle.setFillOpacity(0.3);// 动态切换编辑状态rectangle.enableEditing();rectangle.disableEditing();// 获取矩形区域var bounds = rectangle.getBounds();
上一篇
下一篇