百度地圖繪製多邊型帶編輯功能

來源:互聯網
上載者:User

百度地圖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>

本文出自 “墨亦白時白亦墨” 部落格,請務必保留此出處http://mobai.blog.51cto.com/7300044/1297901

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.