百度地圖API功能演示

風靈使發表於2018-09-05

draw.html

<!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%;margin:0;font-family:"微軟雅黑";}
        #allmap {width: 100%; height:500px; overflow: hidden;}
        #result {width:100%;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;
        }
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=O*************k"></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>百度地圖API功能演示</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>
<div id="result">
    <input type="button" value="獲取繪製的覆蓋物個數" onclick="alert(overlays.length)"/>
    <input type="button" value="清除所有覆蓋物" onclick="clearAll()"/>
    <input type="button" value="獲取多邊形的頂點" onclick="getPoint()"/><br/>
    <input type="button" value="開啟線、面編輯功能" onclick="Editing('enable')"/>
    <input type="button" value="關閉線、面編輯功能" onclick="Editing('disable')"/>
    <input type="button" value="顯示原有多邊形" onclick="showPolygon(this)"/><br/>
    <input type="button" value="畫多邊形" onclick="draw(BMAP_DRAWING_POLYGON)"/>
    <input type="button" value="畫矩形" onclick="draw(BMAP_DRAWING_RECTANGLE)"/>
    <input type="button" value="畫線" onclick="draw(BMAP_DRAWING_POLYLINE)"/>
    <!-- <input type="button"  value="畫點" onclick="draw(BMAP_DRAWING_MARKER)" />-->
    <span>右鍵獲取任意點的經緯度</span>
</div>
<div id="resultShape">
</div>
<div id="shape">
    座標為
</div>
<script type="text/javascript">
    function $(id){
        return document.getElementById(id);
    }
    // 百度地圖API功能    
    var map = new BMap.Map('map');
    var poi = new BMap.Point(113.948913,22.530844);
    map.centerAndZoom(poi, 16);
    map.enableScrollWheelZoom();
    var overlays = [];
    var overlaycomplete = function(e){
        overlays.push(e.overlay);
    };
    var styleOptions = {
        strokeColor:"red",    //邊線顏色。    
        fillColor:"red",      //填充顏色。當引數為空時,圓形將沒有填充效果。    
        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_TOP_RIGHT, //位置    
            offset: new BMap.Size(5, 5), //偏離值    
        },
        circleOptions: styleOptions, //圓的樣式    
        polylineOptions: styleOptions, //線的樣式    
        polygonOptions: styleOptions, //多邊形的樣式    
        rectangleOptions: styleOptions //矩形的樣式    
    });
    //新增滑鼠繪製工具監聽事件,用於獲取繪製結果
    drawingManager.addEventListener('overlaycomplete', overlaycomplete);
    map.addEventListener("rightclick",function(e){
        if(confirm(e.point.lng + "," + e.point.lat)){
            $("shape").innerHTML=$("shape").innerHTML+" <br/>("+e.point.lng+","+e.point.lat+")";
        }
    });
    function draw(type){
        drawingManager.open();
        drawingManager.setDrawingMode(type);
    }
    function clearAll() {
        for(var i = 0; i < overlays.length; i++){
            map.removeOverlay(overlays[i]);
        }
        overlays.length = 0
    }
    function getPoint(){
        $("resultShape").innerHTML='';
        for(var i = 0; i < overlays.length; i++){
            var overlay=overlays[i].getPath();
            $("resultShape").innerHTML=$("resultShape").innerHTML+overlay.length+'邊形:<br/>';
            for(var j = 0; j < overlay.length; j++){
                var grid =overlay[j];
                $("resultShape").innerHTML=$("resultShape").innerHTML+(j+1)+"個點:("+grid.lng+","+grid.lat+");<br/>";
            }
        }
    }
    function Editing(state){
        for(var i = 0; i < overlays.length; i++){
            state=='enable'?overlays[i].enableEditing():overlays[i].disableEditing();
        }
    }
    function  showPolygon(btn){
        var polygon = new BMap.Polygon([
            new BMap.Point(113.941853,22.530777),
            new BMap.Point(113.940487,22.527789),
            new BMap.Point(113.94788,22.527597),
            new BMap.Point(113.947925,22.530618)
        ], styleOptions);  //建立多邊形
        map.addOverlay(polygon);   //增加多邊形
        // overlays.push(polygon); //是否把該影象加入到編輯和刪除行列
        btn.setAttribute('disabled','false');
        showText();
    }
    function showText(){
        var opts = {
            position : new BMap.Point(113.941853,22.530777),    // 指定文字標註所在的地理位置
            offset   : new BMap.Size(30, 30)    //設定文字偏移量
        }
        var label = new BMap.Label("不可編輯", opts);  // 建立文字標註物件
        label.setStyle({
            color : "black",
            fontSize : "16px",
            fillColor:"red",      //填充顏色。當引數為空時,圓形將沒有填充效果。
        });
        map.addOverlay(label);
    }
</script>
</body>
</html>

效果如圖:

這裡寫圖片描述

相關文章