arcgis api for js开发(四):绘制多边形

arcgis api for js开发(四):绘制多边形
以下分3版本与4版本来写:
1.基于arcgis api for js 3.29
核心代码如下:

 map.on("load", initToolbar);
        var queryType="";//创建类型
        //初始化画图工具
        function initToolbar() {
            tb = new Draw(map);
            tb.on("draw-end", addGraphic);
            //jquery方法,点击激活工具
            $("#polygonQuery").click(function (evt) {
                map.graphics.clear();
                queryType="polygon";
                    map.disableMapNavigation();
                    tb.activate(queryType);//tb.activate('polygon');
            });
            $("#pointQuery").click(function (evt) {
                map.graphics.clear();
                queryType="point";
                map.disableMapNavigation();
                tb.activate(queryType);//tb.activate('polygon');
            });
            //原生js点击事件
                // on(dom.byId("pointQuery"), "click", function(evt) {
            //     console.log(evt);
            //     map.disableMapNavigation();
            //     tb.activate('polygon');//tb.activate('polygon');
            // });
        }
        //添加到地图
        function addGraphic(evt) {
            //deactivate the toolbar and clear existing graphics
            tb.deactivate();
            map.enableMapNavigation();
            // figure out which symbol to use
            var symbol;
            switch (queryType) {
                case "polygon":symbol=new SimpleFillSymbol();
                    map.graphics.add(new Graphic(evt.geometry, symbol));//将绘制的多边形添加至地图
                    break;
                case "point":symbol=new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_SQUARE,
                    new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                        new Color([255,0,0]), 0),
                    new Color([0,255,0]));
                    map.graphics.add(new Graphic(evt.geometry, symbol));
                    break;
            }//绘制所的到的要素为evt.geometry
        }

官方实例地址:https://developers.arcgis.com/javascript/3/jssamples/graphics_add.html
2.基于arcgis api for js 4.11
核心代码:

 let sketchLayer = new GraphicsLayer();
            let bufferLayer = new GraphicsLayer();
           mainView.map.addMany([bufferLayer, sketchLayer]);
            //创建样式
            let sketchGeometry = null;
            const sketchViewModel = new SketchViewModel({
                layer: sketchLayer,
                view: this.mainView,
                pointSymbol: {
                    type: "simple-marker",
                    style: "circle",
                    size: 10,
                    color: [255, 255, 255, 0.8],
                    outline: {
                        color: [211, 132, 80, 0.7],
                        size: 10
                    }
                },
                polylineSymbol: {
                    type: "simple-line",
                    color: [211, 132, 80, 0.7],
                    width: 6
                },
                polygonSymbol: {
                    //三维样式
                    // type: "polygon-3d",
                    // symbolLayers: [
                    //     {
                    //         type: "fill",
                    //         material: {
                    //             color: [255, 255, 255, 0.8]
                    //         },
                    //         outline: {
                    //             color: [211, 132, 80, 0.7],
                    //             size: "10px"
                    //         }
                    //     }
                    // ]
                    type:"simple-fill",
                    color:[211, 132, 80,0.5],//红绿蓝和透明度

                    style: "solid",
                    outline:{
                        color:"black",
                        width:1,
                    },
                }
            });
            //绑定事件
            // document.getElementById("test1-tool").onclick = geometryButtonsClickHandler;
            // document.getElementById("polyline").onclick = geometryButtonsClickHandler;
            // document.getElementById("polygon").onclick = geometryButtonsClickHandler;
            $("#point").click(function () {
                geometryButtonsClickHandler("point");
            });
            $("#polyline").click(function () {
                geometryButtonsClickHandler("polyline");
            });
            $("#polygon").click(function () {
                geometryButtonsClickHandler("polygon");
                // $("#spatialQuery").click(function () {
                    //thisM.spatialquery(sketchGeometry);
                // });
            });
            $("#circleQuery").click(function () {
                geometryButtonsClickHandler("circle");
                //thisM.spatialquery(sketchGeometry);
            });
            $("#rectangleQuery").click(function () {
                geometryButtonsClickHandler("rectangle");
                //thisM.spatialquery(sketchGeometry);
            });
            //创建函数
            function geometryButtonsClickHandler(event) {
                sketchLayer.removeAll();
                const geometryType = event;//event.target.value;
                sketchViewModel.create(geometryType);
                sketchViewModel.on(["create"], function(event) {
                    // update the filter every time the user finishes drawing the filtergeometry
                    if (event.state == "complete") {
                        sketchGeometry = event.graphic.geometry;
                       // thisM.spatialquery(sketchGeometry)绘制完成后执行函数
                    }
                });
                // console.log(sketchGeometry);
            }
            //点击按钮开始生成缓冲区
            let buffer = 0;
            //绑定事件
            $("#bufferbtnDiv").click(function () {
                $("#BufferDiv").fadeIn();
            });
            $("#newBuffer").click(function () {
                    $("#BufferDiv").fadeOut();
                var num=$("#bufferValue").val();
                createBuffer(num);
                });
            function createBuffer(num) {
                // var num=document.getElementById("bufferValue").value;//获取输入的值
                buffer = parseInt(num);
                //
                // // 生成缓冲区
                    // add a polygon graphic for the buffer
                    if (sketchGeometry) {
                        if (buffer > 0) {
                            var newBufferGeometry = geometryEngine.geodesicBuffer(
                                    sketchGeometry,
                                buffer,
                                "meters"
                            );
                            bufferGeometry = newBufferGeometry;
                            if (bufferLayer.graphics.length === 0) {
                                bufferLayer.add(
                                    new Graphic({
                                        geometry: bufferGeometry,
                                        symbol: {
                                            type:"simple-fill",
                                            color:[255, 132, 80,0.5],//红绿蓝和透明度
                                            style: "solid",
                                            outline:{
                                                color:"blue",
                                                width:1,
                                            },
                                        }
                                    })
                                );
                            }
                            thisM.spatialquery(newBufferGeometry);
                            bufferLayer.graphics.getItemAt(0).geometry = bufferGeometry;
                        } else {
                            bufferLayer.removeAll();
                        }
                    }
            }

官方实例:https://developers.arcgis.com/javascript/latest/sample-code/layers-scenelayer-feature-masking/index.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值