百度地圖API提供的Demo 功能不全,下面是我改寫的個帶編輯功能的多邊形繪製Demo,有需要的同學拿去

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html{width: 100%;height: 100%;overflow: hidden;margin:0;}
#allmap {margin-right: 300px;height: 100%;overflow: hidden;}
#result {border-left:1px dotted #999;height:100%;width:295px;position:absolute;top:0px;right:0px;font-size:12px;}
dl,dt,dd,ul,li{
    margin:0;
    padding:0;
    list-style:none;
}
p{font-size:12px;}
dt{
    font-size:14px;
    font-family:"微軟雅黑";
    font-weight:bold;
    border-bottom:1px dotted #000;
    padding:5px 0 5px 5px;
    margin:5px 0;
}
dd{
    padding:5px 0 0 5px;
}
li{
    line-height:28px;
}
.red{color: red;}
</style>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.min.css" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=5E5EE28a7615536d1ffe2ce2a3667859"></script>
<!--載入滑鼠繪製工具-->
<script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
<!--載入檢索資訊視窗-->
<script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css" />
<title>百度地圖繪製多邊型帶編輯功能</title>
</head>
<body>
<div id="allmap" style="overflow:hidden;zoom:1;position:relative;">  
    <div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;"></div>
    <div id="showPanelBtn" style="position:absolute;font-size:14px;top:50%;margin-top:-95px;right:0px;width:20px;padding:10px 10px;color:#999;cursor:pointer;text-align:center;height:170px;rgba(255,255,255,0.9);-webkit-transition:  all 0.5s ease-in-out;transition: all 0.5s ease-in-out;font-family:`微軟雅黑`;font-weight:bold;">編輯多邊形<br/>&lt;</div>
    <div id="panelWrap" style="width:0px;position:absolute;top:0px;right:0px;height:100%;overflow:auto;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;">
        <div style="width:20px;height:200px;margin:-100px 0 0 -10px;color:#999;position:absolute;opacity:0.5;top:50%;left:50%;" id="showOverlayInfo">此處用於展示覆蓋物資訊</div>
        <div id="panel" style="position:absolute;"></div>
    </div>
</div>
<div id="result">
    <dl>
        <dt>繪製工具功能</dt>
            <ul>
                <li><label><input type="radio" name="openClose1" onclick="bmap.drawingManager.open()"/>開啟</label>  <label><input type="radio" name="openClose1" onclick="bmap.drawingManager.close()" checked="checked"/>關閉</label></li>
            </ul>
        </dd>
        <dt>是否進行線或面積的計算(單位米)</dt>
            <ul>
                <li><label><input type="radio" name="openClose" onclick="bmap.drawingManager.enableCalculate()"/>開啟</label>  <label><input type="radio" name="openClose" onclick="bmap.drawingManager.disableCalculate()"  checked="checked"/>關閉</label></li>
            </ul>
        </dd>
        <dt>繪製功能</dt>
        <dd>
            <ul>
                <li>
                    <label><input type="radio" name="drawmode" onclick="bmap.drawingManager.setDrawingMode(BMAP_DRAWING_MARKER)" checked="checked"/>畫點</label>
                    <input type="checkbox" id="isInfowindow"/> 是否帶資訊視窗
                </li>
                <li>
                    <label><input type="radio" name="drawmode" onclick="bmap.drawingManager.setDrawingMode(BMAP_DRAWING_CIRCLE)"/>畫圓</label>
                </li>
                <li>
                    <label><input type="radio" name="drawmode" onclick="bmap.drawingManager.setDrawingMode(BMAP_DRAWING_POLYLINE)"/>畫線</label>
                </li>
                <li>
                    <label><input type="radio" name="drawmode" onclick="bmap.drawingManager.setDrawingMode(BMAP_DRAWING_POLYGON)"/>畫多邊形</label>
                </li>
                <li>
                    <label><input type="radio" name="drawmode" onclick="bmap.drawingManager.setDrawingMode(BMAP_DRAWING_RECTANGLE)"/>畫矩形</label>
                </li>
            </ul>
        </dd>
        <dt>覆蓋物操作</dt>
        <dd>
            <ul>
                <li>
                    <input type="button" value="獲取繪製的覆蓋物個數" onclick="bmap.getCount()"/>
                    <input type="button" value="清除所有覆蓋物" onclick="bmap.clearAll()"/>
                    <input type="button" value="獲取最後一個覆蓋物資訊" onclick="bmap.getOverLay()"/>
                </li>
            </ul>
        </dd>
    </dl>
</div>
<script type="text/javascript">
/**
 * Author: mobai
 * http://mobai.blog.51cto.com/
 * 轉載請註明出處
 */
function $(id){
    return document.getElementById(id);
}
var bmap = {
    status: false,
    map: ``,
    point: ``,
    overlays: [],
    overlaysCache: [],
    myPolygon: ``,
    myOverlay: [],
    drawingManager: ``,
    styleOptions: {
        strokeColor:"red",      //邊線顏色。
        fillColor:"red",        //填充顏色。當引數為空時,圓形將沒有填充效果。
        strokeWeight: 3,        //邊線的寬度,以畫素為單位。
        strokeOpacity: 0.8,     //邊線透明度,取值範圍0 - 1。
        fillOpacity: 0.3,       //填充的透明度,取值範圍0 - 1。
        strokeStyle: `solid`    //邊線的樣式,solid或dashed。
    },
    /**
     * 例項化
     */
    init: function(){
        if(this.status){
            return;
        }
        this.status = true;
        this.map = new BMap.Map(`map`);
        this.point = new BMap.Point(116.307852,40.057031);
        var map = this.map;
        var styleOptions = this.styleOptions;
        map.centerAndZoom(this.point, 16);
        map.enableScrollWheelZoom();
        //例項化滑鼠繪製工具
        this.drawingManager = new BMapLib.DrawingManager(map, {
            isOpen: false, //是否開啟繪製模式
            enableDrawingTool: true, //是否顯示工具欄
            drawingToolOptions: {
                anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
                offset: new BMap.Size(5, 5), //偏離值
                scale: 0.8 //工具欄縮放比例
            },
            circleOptions: styleOptions, //圓的樣式
            polylineOptions: styleOptions, //線的樣式
            polygonOptions: styleOptions, //多邊形的樣式
            rectangleOptions: styleOptions //矩形的樣式
        });
        //新增滑鼠繪製工具監聽事件,用於獲取繪製結果
        this.drawingManager.addEventListener(`overlaycomplete`, bmap.overlaycomplete);
        /*載入一個已有的多邊形*/
        if (this.myOverlay) {
            this.loadMyOverlay();
        };
        map.addEventListener("rightclick",function(e){
            alert(e.point.lng + "," + e.point.lat);
        });
    },
    loadMyOverlay: function(){
        var map = this.map;
        this.clearAll();
        map.centerAndZoom(this.point, 11);
        myPolygon = new BMap.Polygon(this.myOverlay, this.styleOptions);
        this.myPolygon = myPolygon;
        try{myPolygon.enableEditing();}catch(e){};
        myPolygon.addEventListener("lineupdate",function(e){
            bmap.showLatLon(e.currentTarget.W);
        });
        map.addOverlay(myPolygon);
    },
    showLatLon: function(a){
        var len = a.length;
        var s = ``;
        var arr = [];
        for(var i =0 ; i < len-1; i++){
            arr.push([a[i].lng, a[i].lat]);
            s += `<li>`+ a[i].lng +`,`+ a[i].lat +`<span class="red" title="刪除" onclick="bmap.delPoint(`+i+`)">X</span></li>`;
        }
        this.overlaysCache = arr;
        $("panelWrap").innerHTML = `<ul>`+ s +`</ul>`;
    },
    delPoint: function(i){
        if(this.overlaysCache.length <=3 ){
            alert(`不能再刪除, 請保留3個以上的點.`);
            return;
        }
        this.overlaysCache.splice(i,1);
        var a = this.overlaysCache;
        var newOverlay = [];
        for(var i in a ){
            newOverlay.push( new BMap.Point(a[i][0],a[i][1]) );
        }
        this.myOverlay = newOverlay;
        this.loadMyOverlay();
    },
    /**
     *回撥獲得覆蓋物資訊
     */
    overlaycomplete: function(e){
        bmap.overlays.push(e.overlay);
        e.overlay.enableEditing();
        e.overlay.addEventListener("lineupdate",function(e){
            bmap.showLatLon(e.currentTarget.W);
        });
    },
    /**
     * 清除覆蓋物
     */
    clearAll: function() {
        var map = this.map;
        var overlays = this.overlays;
        for(var i = 0; i < overlays.length; i++){
            map.removeOverlay(overlays[i]);
        }
        this.overlays.length = 0
        map.removeOverlay(this.myPolygon);
        this.myPolygon = ``;
    },
    /**
     *取覆蓋物的經緯度
     */
    getOverLay: function(){
        var box = this.myPolygon ? this.myPolygon : this.overlays[this.overlays.length - 1];
        console.log(box.W);
    },
    getCount: function(){
        var n = 0;
        if (this.myPolygon) {
            n++;
        };
        if (this.overlays) {
            n = n + this.overlays.length;
        };
        console.log(n);
    }
};
//顯示結果皮膚動作
var isPanelShow = false;
$("showPanelBtn").onclick = showPanel;
function showPanel(){
    if (isPanelShow == false) {
        isPanelShow = true;
        $("showPanelBtn").style.right = "230px";
        $("panelWrap").style.width = "230px";
        $("map").style.marginRight = "230px";
        $("showPanelBtn").innerHTML = "編輯多邊形<br/>>";
    } else {
        isPanelShow = false;
        $("showPanelBtn").style.right = "0px";
        $("panelWrap").style.width = "0px";
        $("map").style.marginRight = "0px";
        $("showPanelBtn").innerHTML = "編輯多邊形<br/><";
    }
}
//載入一個已有的多邊形
bmap.myOverlay = [  
    new BMap.Point(116.256515,39.995242),
    new BMap.Point(116.502579,39.951893),
    new BMap.Point(116.256515,39.866882)
];
bmap.init();
</script>
</body>
</html>