工具欄樣式
var geoc = new BMap.Geocoder();
var styleOptions = {
strokeColor: "red", //邊線顏色。
fillColor: "", //填充顏色。當參數為空時,圓形將沒有填充效果。
strokeWeight: 3, //邊的寬度,以像素為單位。
strokeOpacity: 0.8, //邊線透明度,取值范圍0 - 1。
fillOpacity: 0.6, //填充的透明度,取值范圍0 - 1。
strokeStyle: 'solid' //邊線的樣式,solid或dashed。
}
創建工具欄
var drawingManager = new BMapLib.DrawingManager(map, {
isOpen: false, //是否開啟繪制模式
enableDrawingTool: true, //是否顯示工具欄
drawingToolOptions: {
anchor: BMAP_ANCHOR_BOTTOM_RIGHT, //位置
offset: new BMap.Size(-25, 15), //偏離值
scale: 0.7,
drawingModes: [
BMAP_DRAWING_MARKER,//marker
BMAP_DRAWING_POLYLINE,//線
BMAP_DRAWING_POLYGON//多邊形
]
},
enableCalculate: true,
polylineOptions: styleOptions, //線的樣式
polygonOptions: styleOptions //多邊形的樣式
});
添加監聽事件
drawingManager.addEventListener('polylinecomplete', polylinecomplete);
//e.getPath獲取坐標。length共有多少坐標,用for循環遍歷坐標
function polylinecomplete(e,overlay) {
console.log(e.getPath().length);
var points_poly=[];
for(var i=0;i<e.getPath().length;i++){
points_poly.push({"lng":e.getPath()[i].lng,"lat":e.getPath()[i].lat});
}
console.log(JSON.stringify(points_poly))
}