1draw.html 2.4 KB
<!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:1200px;
      height: 1200px;
    }
  </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=2.0&key=87c481b056bda4517971a8e8220dd75a&plugin=AMap.PolygonEditor"></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="createPolygon()" style="margin-bottom: 5px">绘制区域</button>
<!--     <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: [102.231407, 23.393544],
      zoom: 16.8
    });

    var path1 = [[102.19828,23.408202],[102.184279,23.386124],[102.204195,23.368205],[102.268182,23.401235]]

    var polygon = null;
    if (path1.length) {
      var polygon = new AMap.Polygon({
        path: path1
      })

      map.add([polygon]);
      map.setFitView();

      var polyEditor = new AMap.PolygonEditor(map);
      polyEditor.addAdsorbPolygons([polygon]);

    }









    polyEditor.on('add', function (data) {


      if (polygon) {
        map.remove(polygon);
      }
     // console.log(data);
      console.log('数据1----',data['target']['_opts']['path']);

      polygon = data.target;
      polyEditor.addAdsorbPolygons(polygon);
      polygon.on('dblclick', () => {
        polyEditor.setTarget(polygon);
        polyEditor.open();
      })
    })



    polyEditor.on('end', function (data) {
      console.log('数据2----',data['target']['_opts']['path']);
    });


 


    polygon.on('dblclick', () => {
      polyEditor.setTarget(polygon);
      polyEditor.open();
    })

    function createPolygon() {
      if (polygon) {
        map.remove(polygon);
      }

      polyEditor.close();
      polyEditor.setTarget();
      polyEditor.open();
    }
    /*     polyEditor.setTarget(polygon2);
        polyEditor.open(); */
  </script>
</body>




</html>