直接上代碼
<!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.PolyEditor"></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: 120px"> <button class="btn" onclick="polyEditor.open()" style="margin-bottom: 5px">開始編輯</button> <button class="btn" onclick="polyEditor.close()">結束編輯</button> </div> <script type="text/javascript"> var map = new AMap.Map("container", { center: [116.400274, 39.905812], zoom: 14 }); var path = [ [116.403322, 39.920255], [116.410703, 39.897555], [116.402292, 39.892353], [116.389846, 39.891365] ] var polygon = new AMap.Polygon({ path: path, strokeColor: "#FF33FF", strokeWeight: 6, strokeOpacity: 0.2, fillOpacity: 0.4, fillColor: '#1791fc', zIndex: 50, }) map.add(polygon) // 縮放地圖到合適的視野級別 map.setFitView([ polygon ]) var polyEditor = new AMap.PolyEditor(map, polygon) polyEditor.on('addnode', function(event) { log.info('觸發事件:addnode') }) polyEditor.on('adjust', function(event) { log.info('觸發事件:adjust') }) polyEditor.on('removenode', function(event) { log.info('觸發事件:removenode') }) polyEditor.on('end', function(event) { log.info('觸發事件: end') //獲取 結束后觸發 console.info("end",event.target.w.path); // event.target 即為編輯后的多邊形對象 }) </script> </body> </html>