arcgis api for js 4.x学习笔记(2)绘制面(Draw)及面积计算(geodesicArea)

<button id="btnDrawPolygon">面</button>
<button id="clearPolygon">清除</button>
//本文对应Api 4.6版本 
require([
        'dojo/on',
        'dojo/dom',
        'esri/views/2d/draw/Draw',
        'esri/Graphic',
        'esri/geometry/geometryEngine',
        'esri/geometry/Point',
        'esri/geometry/Polyline',
        'esri/geometry/Polygon',
        'esri/symbols/TextSymbol',
        'esri/layers/GraphicsLayer',
        "esri/geometry/SpatialReference",
        'dojo/domReady!'
],function(on,dom,Draw,Graphic,GeometryEngine,Point,Polyline,Polygon,TextSymbol,
GraphicsLayer,SpatialReference) {
    var draw;
    on(dom.byId("btnDrawPolygon"),"click",function(){
        draw = new Draw({
           view: view//view定义见https://blog.csdn.net/wangchaohpu/article/details/107487687
        });
        var action = draw.create("polygon");
        //添加一个点的时候触发
        action.on("vertex-add", function (evt) {
            createPolygonGraphic(evt.vertices);
        });
        // Fires when the pointer moves over the view
        action.on("cursor-update", function (evt) {
            createPolygonGraphic(evt.vertices);
        });
        // 按ctrl+c撤销一个点的时候触发
        action.on("vertex-remove", function (evt) {
            createPolygonGraphic(evt.vertices);
        });
        //双击完成的时候触发
        action.on("draw-complete", function (evt) {
            createPolygonGraphic(evt.vertices);
        });
    })
    function createPolygonGraphic(vertices){
                view.graphics.removeAll();
                var polygon = new Polygon({
                    rings: vertices,
                    spatialReference: view.spatialReference,//坐标系必须跟当前view的坐标系一致,否则添加不上
                })
                var graphic = new Graphic({
                    geometry: polygon,
                    symbol: {
                        type: "simple-fill", // autocasts as SimpleFillSymbol
                        color: [3, 255, 240, 0.1],
                        style: "solid",
                        outline: {  // autocasts as SimpleLineSymbol
                            color: [255,116,3],
                            width: 2
                        }
                    }
                });
                view.graphics.add(graphic);
                var polygon = new Polygon({
                    rings: vertices,
                    // spatialReference: view.spatialReference
                    spatialReference: new SpatialReference(4326)
                })
                //WGS84 (wkid:4326)及墨卡托坐标系用geodesicArea计算面积,其他坐标系用planarArea计算面积
                // var area = GeometryEngine.planarArea(polygon);
                var area = GeometryEngine.geodesicArea(polygon,'square-meters');

                //将面积结果标注在图上
                var center = polygon.centroid;
                var pointcenter = new Point({
                    longitude:center.longitude,
                    latitude:center.latitude,
                    spatialReference: view.spatialReference//坐标系必须跟当前view的坐标系一致,否则添加不上
                })
                var unit="平方米";
                var textSymbol = new TextSymbol({
                    type: "text",
                    color: "red",
                    haloColor: "black",
                    haloSize: "5px",
                    xoffset: '3pt',
                    yoffset: '3pt',
                    text:Math.abs(Math.round(area*100)/100)+unit,
                    font: {
                        size: 12,
                        family: "sans-serif",
                        weight: "bolder"
                    }
                })
                var textGraphics=new Graphic({
                    geometry:pointcenter,
                    symbol: textSymbol
                });
                view.graphics.add(textGraphics);


            }

})

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值