[高德地圖API]學習筆記(一)常見問題記錄

Q1 用鼠標工具繪製覆蓋物,如何獲取覆蓋物的路徑?(官方記錄問題)

參考鏈接:https://lbs.amap.com/faq/js-api/map-js-api/cover/43364

代碼示例

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <style>
        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
    <title>橢圓的繪製和編輯</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申請的key值&plugin=AMap.MouseTool"></script>
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
</head>
<body>
<div id="container"></div>
<div class="input-card" style="width: 200px">
    <h4 style="margin-bottom: 10px; font-weight: 600">利用mouseTool繪製覆蓋物</h4>
    <button class="btn" onclick="drawPolyline()" style="margin-bottom: 5px">繪製線段</button>
    <button class="btn" onclick="drawPolygon()" style="margin-bottom: 5px">繪製多邊形</button>
    <button class="btn" onclick="drawRectangle()" style="margin-bottom: 5px">繪製矩形</button>
    <button class="btn" onclick="drawCircle()" style="margin-bottom: 5px">繪製圓形</button>
</div>
<script type="text/javascript">
    var map = new AMap.Map("container", {
        center: [107.943579, 30.131735],
        zoom: 14
    });

    var mouseTool = new AMap.MouseTool(map)

    function drawPolyline() {
        mouseTool.polyline({
            strokeColor: "#3366FF",
            strokeOpacity: 1,
            strokeWeight: 6,
            // 線樣式還支持 'dashed'
            strokeStyle: "solid",
            // strokeStyle是dashed時有效
            // strokeDasharray: [10, 5],
        })
    }

    function drawPolygon() {
        mouseTool.polygon({
            strokeColor: "#FF33FF",
            strokeOpacity: 1,
            strokeWeight: 6,
            strokeOpacity: 0.2,
            fillColor: '#1791fc',
            fillOpacity: 0.4,
            // 線樣式還支持 'dashed'
            strokeStyle: "solid",
            // strokeStyle是dashed時有效
            // strokeDasharray: [30,10],
        })
    }

    function drawRectangle() {
        mouseTool.rectangle({
            strokeColor: 'red',
            strokeOpacity: 0.5,
            strokeWeight: 6,
            fillColor: 'blue',
            fillOpacity: 0.5,
            // strokeStyle還支持 solid
            strokeStyle: 'solid',
            // strokeDasharray: [30,10],
        })
    }

    function drawCircle() {
        mouseTool.circle({
            strokeColor: "#FF33FF",
            strokeOpacity: 1,
            strokeWeight: 6,
            strokeOpacity: 0.2,
            fillColor: '#1791fc',
            fillOpacity: 0.4,
            strokeStyle: 'solid',
            // 線樣式還支持 'dashed'
            // strokeDasharray: [30,10],
        })
    }

    mouseTool.on('draw', function (event) {
        // event.obj 爲繪製出來的覆蓋物對象
        log.info('覆蓋物對象繪製完成')
        alert('是否提交')
    })

    AMap.event.addListener( mouseTool,'draw',function(e){
        console.log(e.obj.getPath());//獲取路徑/範圍
        console.log(e.obj.getArea());
    });

</script>
</body>
</html>

Q2 編輯多邊形後如何獲取多邊形的路徑?(官方記錄問題)

參考鏈接:https://lbs.amap.com/faq/js-api/map-js-api/cover/43418

代碼示例:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>編輯多邊形</title>
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
    <script src="http://webapi.amap.com/maps?v=1.3&key=您申請的key值&plugin=AMap.PolyEditor,AMap.CircleEditor"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>

<body>
<div id="container"></div>
<div class="button-group">
 <input type="button" class="button" value="開始編輯多邊形" onClick="editor.startEditPolygon()"/>
    <input type="button" class="button" value="結束編輯多邊形" onClick="editor.closeEditPolygon()"/>
</div>

<script>
    var editorTool, map = new AMap.Map("container", {
        resizeEnable: true,
        center: [116.403322, 39.900255],//地圖中心點
        zoom: 13 //地圖顯示的縮放級別
    });

    //在地圖上繪製折線
    var editor={};
    editor._polygon=(function(){

        var arr = [ //構建多邊形經緯度座標數組
        [116.403322,39.920255],
        [116.410703,39.897555],
        [116.402292,39.892353],
        [116.389846,39.891365]
        ]

        return new AMap.Polygon({
            map: map,
            path: arr,
            strokeColor: "#0000ff",
            strokeOpacity: 1,
            strokeWeight: 3,
            fillColor: "#f5deb3",
            fillOpacity: 0.35
        });
    })();

    map.setFitView();
    editor._polygonEditor= new AMap.PolyEditor(map, editor._polygon);
    editor.startEditPolygon=function(){
        editor._polygonEditor.open();
    }
    editor.closeEditPolygon=function(){
        editor._polygonEditor.close();
        var a =  editor._polygon.getPath();
        console.log(a);
    }
</script>
</body>
</html>

Q3 清除當前所繪製的圖形?

參考鏈接:https://lbs.amap.com/api/javascript-api/example/mouse-operate-map/mouse-draw-overlayers

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章