基于arcgis js 3.x实现在前端页面动态拖拽圆的功能

参考文献:https://www.cnblogs.com/deliciousExtra/p/5503929.html

思路:

  1. 监听mouse-down事件,获取拖拽点;
  2. 监听mouse-drag事件, 获取实时拖拽点并计算拖拽点与圆心的距离(半径),重新生成新圆,新的半径样式,新的半径文本样式;
  3. 监听mouse-up事件;
    <script type="text/javascript">
            require([
                "esri/map",
                "esri/layers/ArcGISTiledMapServiceLayer",
                "esri/layers/GraphicsLayer",
                "esri/geometry/Point",
                "esri/geometry/Polyline",
                "esri/geometry/Circle",
                "esri/SpatialReference",
                "esri/geometry/geometryEngine",
                "esri/symbols/SimpleMarkerSymbol",
                "esri/symbols/PictureMarkerSymbol",
                "esri/symbols/SimpleLineSymbol",
                "esri/symbols/SimpleFillSymbol",
                "esri/symbols/TextSymbol",
                "esri/symbols/Font",
                "esri/graphic",
                "dojo/_base/Color"
            ],
                function (Map, ArcGISTiledMapServiceLayer,  GraphicsLayer, Point, Polyline, Circle, SpatialReference, geometryEngine,
                    SimpleMarkerSymbol, PictureMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, TextSymbol, Font,Graphic,Color) {
    
                    var map = new Map("map", {
                        center: [116.38, 39.90], //long, lat
                        zoom: 12
                    });
                    var basemap = new ArcGISTiledMapServiceLayer("http://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetGray/MapServer");
                    map.addLayer(basemap);
                    var glayer = new GraphicsLayer();
                    map.addLayer(glayer);
                    /**********样式************/
                    var fillsybol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
                        new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                            new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.25])
                    );
                    var polylinesybol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT,
                        new Color([0, 0, 255, 0.25]), 1.5);
                    var dragButtonSymbol = new PictureMarkerSymbol({
                        "url": require.toUrl("./imgs/左右.png"),
                        "height": 12,
                        "width": 12
                    });
                    /**********样式************/
                    //初始圆
                    var circle = new Circle(new Point(12957457.175868401, 4852952.74397267, new SpatialReference({ wkid: 3857 })), { radius: 12000 });
                    var graphic = new Graphic(circle, fillsybol);          
                    //拖拽点
                    var dragPoint = createDragBtnPoint(circle, circle.center);
                    var dragGraphic = new Graphic(dragPoint, dragButtonSymbol);
                    //标注距离要素
                    var labelGraphic = new Graphic(dragPoint, createDistanceSymbol(12000));
                    glayer.add(graphic);
                    glayer.add(dragGraphic);
                    glayer.add(labelGraphic);
                    //拖拽点与中心点生成的线
                    var polyline = new Polyline(new SpatialReference({ wkid: 3857 }));
                    polyline.addPath([circle.center, dragPoint]);
                    var lineGraphic = new Graphic(polyline, polylinesybol);
                    glayer.add(lineGraphic);
                    //鼠标点击事件
                    glayer.on("mouse-down", function (evt) {
                        var graphic1 = evt.graphic; 
                        if (graphic1.symbol.type == "picturemarkersymbol") {
                            dragGraphic = graphic1;
                            dragGraphic.isMouseDown = true;
                            //禁止地图移动
                            map.disableMapNavigation();
                            map.setMapCursor("pointer");
                        }
                    });
                    //拖拽事件
                    map.on("mouse-drag", function (evt) {
                        if (dragGraphic != null && dragGraphic.isMouseDown) {
                            dragPoint = evt.mapPoint;
                            if (circle.center.y != dragPoint.y) {
                                dragPoint.setY(circle.center.y);
                            }
                            var radius = calDistance(dragPoint, circle.center)
                            console.log(radius)
                            glayer.remove(graphic);
                            console.log(circle.center)
                            var newcircle = new Circle(circle.center, { radius: radius });
                            graphic = new Graphic(newcircle, fillsybol);
                            dragGraphic.setGeometry(dragPoint);
                            glayer.add(graphic);
                            labelGraphic.setGeometry(dragPoint).setSymbol(createDistanceSymbol(radius))
                            polyline.removePath(0);
                            //添加新的线
                            polyline.addPath([circle.center, dragPoint]);
                            glayer.refresh();
                        }
                    });
                    //拖拽结束
                    map.on("mouse-up", function (evt) {
                        map.enableMapNavigation();
                        dragGraphic.isMouseDown = false;
                        map.setMapCursor("default");
                    });
    
                    function calDistance(point1, point2) {
                        return geometryEngine.distance(point1, point2, "meters");
                    }
                    function createDragBtnPoint(geometry, center) {
                        var extent = geometry.getExtent();
                        var xmax = extent.xmax;
                        return new Point([xmax, center.y], center.spatialReference)
                    }
                    function createDistanceSymbol (distance) {
                        var distance = distance.toFixed(0) + "m";
                        var fontColor = new Color("#696969");
                        var holoColor = new Color("#fff");
                        var font = new Font("10pt", Font.STYLE_ITALIC, Font.VARIANT_NORMAL, Font.WEIGHT_BOLD, "Courier");
                        var textSymbol = new TextSymbol(distance, font, fontColor);
                        textSymbol.setOffset(10, 20).setHaloColor(holoColor).setHaloSize(2);
                        textSymbol.setAlign(TextSymbol.ALIGN_MIDDLE);
                        return textSymbol;
                    }
    
                });
        </script>

    效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值