JS API 编辑多边形后如何获取多边形的路径?

使用 PolyEditor编辑折线、多边形、圆形等覆盖物,都可以通过多边形覆盖物自身的方法,获取到位置/范围/路径。

比如结束多边形编辑时,返回该多边形的经纬度数组,核心代码如下:

editor.closeEditPolygon=function(){
  editor._polygonEditor.close();
  var a =  editor._polygon.getPath();
  console.log(a);
}


完整代码举例如下:

<!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>


如以上内容仍未解决您的问题,请创建工单
返回顶部 示例中心 常见问题 智能客服 公众号
二维码