arcgis 4.x 编辑图层

<html>

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
        <title>Sketch update validation - 4.10</title>

        <link rel="stylesheet" href="https://js.arcgis.com/4.10/esri/css/main.css">
        <script src="https://js.arcgis.com/4.10/"></script>
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <style>
            html,
            body,
            #viewDiv {
                padding: 0;
                margin: 0;
                height: 100%;
                width: 100%;
                font-family: verdana;
            }
        </style>

        <script>
            
            var clickCount = 1;
            
            require([
                "esri/widgets/Sketch/SketchViewModel",
                "esri/Graphic",
                "esri/Map",
                "esri/views/MapView",
                "esri/layers/GraphicsLayer",
                "esri/geometry/Polygon",
                "esri/geometry/geometryEngine",
                "esri/geometry/support/webMercatorUtils"
            ], function(SketchViewModel, Graphic, Map, MapView, GraphicsLayer, Polygon, geometryEngine, webMercatorUtils) {
                let sketchViewModel;
                // 多边形
                const graphicsLayer = new GraphicsLayer();

                const map = new Map({
                    basemap: "hybrid",
                    layers: [graphicsLayer]
                });

                const view = new MapView({
                    container: "viewDiv",
                    map: map,
                });

                // 设置图形点击事件
                setUpGraphicClickHandler();

                view.when(function() {
                    // 添加图形
                    var newDevelopmentGraphic = new Graphic({
                        geometry: webMercatorUtils.geographicToWebMercator(new Polygon({   // 用 webMercatorUtils(geometry) 将经纬度转平面坐标 
                            rings: [
                                [-111.30, 52.68],
                                [-98, 49.5],
                                [-93.94, 29.89],
                                [-93.4, 29.9]
                            ],
                            spatialReference: view.spatialReference
                        })),
                        symbol: {
                            type: "simple-fill",
                            style: "solid",
                            color: [0, 170, 255, 0.8],
                            outline: {
                                color: [255, 255, 255],
                                width: 2
                            }
                        },
                        attributes: {
                            newDevelopment: "new store"
                        }
                    });

                    // 将绘制的图形添加到view
                    graphicsLayer.addMany([newDevelopmentGraphic]);
//===============================线(结束)===================================================

//===============================点(开始)=================================================== 
                    var pointGraphic = new Graphic({
                        geometry: webMercatorUtils.geographicToWebMercator({ // 用 webMercatorUtils(geometry) 将经纬度转平面坐标 
                            type: "point", // autocasts as new Point()
                            longitude: -117.1376635742038,
                            latitude: 34.13644900200571,
                            spatialReference: view.spatialReference
                        }),
                        symbol: {
                            type: "simple-marker", 
                            color: [0, 170, 255, 0.8],
                            outline: {
                                color: [255, 255, 255],
                                width: 2
                            }
                        },
                        attributes: {
                            newDevelopment: "new store"
                        }
                    });

                    graphicsLayer.addMany([pointGraphic]);
//===============================点(结束)=================================================== 
//===============================线(开始)====================================================
                    var polylineGraphic = new Graphic({
                        geometry: webMercatorUtils.geographicToWebMercator({
                            type: "polyline",
                            paths: [[
                                [-111.10, 52.8],
                                [-100, 49.8],
                                [-90.94, 29.39]
                            ]],
                            spatialReference: view.spatialReference
                      }),
                        symbol: {
                            type: "simple-line",
                            color: [0, 170, 255, 0.8],
                            width: 2
                        },
                        attributes: {
                            Name: "Keystone Pipeline",
                            Owner: "TransCanada",
                            Length: "3,456 km",
                            newDevelopment: "new store"
                        }
                    });

                    graphicsLayer.addMany([polylineGraphic]);

                    sketchViewModel = new SketchViewModel({
                        view: view,
                        layer: graphicsLayer,
                        updateOnGraphicClick: false,
                        defaultUpdateOptions: { // 设置更新操作的默认选项
                            toggleToolOnClick: false // 仅启用重塑操作
                        }
                    });
                    
                    $("#huizhi").click(function() {
                          sketchViewModel.update([polylineGraphic], {
                            tool: "reshape"
                        });
                    });

                });

                // 设置图形点击事件
                function setUpGraphicClickHandler() {
                    // 鼠标点击事件
                    view.on("click", function(event) {
                        // 如果当前图形正在被编辑,直接退出编辑
                        if(sketchViewModel.state === "active") {
                            return;
                        }

                        view.hitTest(event).then(function(response) {
                            var results = response.results;
                            results.forEach(function(result) {
                                
                                console.log(clickCount);
                                if(clickCount == 2) {
                                    if(result.graphic.layer === sketchViewModel.layer && result.graphic.attributes && result.graphic.attributes.newDevelopment) {
                                        sketchViewModel.update([result.graphic], {
                                            tool: "reshape"
                                        });
                                    }
                                    clickCount = 1;
                                } else {
                                    clickCount++; 
                                }
                            });
                        });
                        
                        
                    });
                }

            });
        </script>
    </head>

    <body>
        <input type="button" value="按钮" id="huizhi" />
        <div id="viewDiv"></div>
    </body>

</html>

转载于:https://my.oschina.net/u/3516665/blog/3062745

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值