ArcGIS api for JavaScript 4+版本绘制工具(Draw)

简介

arcgis api for js 4+版本发布很久了,该版本代码风格符合现阶段主流的编码风格,相比3+版本的二次开发更具美感,同时兼容requireJs了(3+版本是和requireJs直接冲突的版本,要在项目中共同使用的最好方式是把地图应用单独放在iframe里面)(当然,打包方式需要用requireJs的打包而不能用dojo-builder的打包方式),然而估计这esri的团队都注重3d的开发了,所以对于3+版本原有的很多功能都还没有实现。比如3+版本的工具draw,官网给出的答案是会做,但具体做的时间是 comming soon,本人从4.0等到了4.2,这个东西还是没有,于是索性自己写了一个。其中该工具只支持2d的绘图。

使用方式

代码放置位置图
调用方式图

代码如下

`define([
"./Custom",
"../layers/GraphicsLayer",
"../geometry/Point",
"../geometry/Polyline",
"../geometry/Polygon",
"../geometry/Extent",
"../geometry/Circle",
"../Graphic",
"../symbols/SimpleMarkerSymbol",
"../symbols/SimpleLineSymbol",
"../symbols/SimpleFillSymbol",
"dojo/dom"], function (Custom, GraphicsLayer, Point,
Polyline, Polygon, Extent, Circle, Graphic, SimpleMarkerSymbol, SimpleLineSymbol,
SimpleFillSymbol, dom) {

    var types = {
        POLYGON: 'polygon',
        POLYLINE: 'polyline',
        POINT: 'point',
        CIRCLE: 'circle',
        FREE_POLYGON: 'free_polygon',
        FREE_POLYLINE: 'free_polyline',
        FREE_EXTENT: 'free_extent'
    };

    var Draw = Custom.createSubclass({
        declaredClass: "esri.custom._Draw",
        normalizeCtorArgs: function (view, options) {
            options || (options = {});
            this._mapView = view;
            this._clickPoints = []; // 点击绘制时的点集合
            this._tempGraphicLayer = new GraphicsLayer(); // 绘制过程中的要素图层
            this._drawGraphicLayer = new GraphicsLayer(); // 绘制完成的要素图层
            this._mapView.map.add(this._drawGraphicLayer);
            this._mapView.map.add(this._tempGraphicLayer);
            this._defaultSymbol = dojo.mixin({ // 默认样式
                polygon: new SimpleFillSymbol({ // 默认绘制polygon的样式
                    color: [127, 127, 127, 0.9],
                    style: "solid",
                    outline: {
                        color: [0, 124, 247, 1],
                        width: 2
                    }
                }),
                tempPolygon: new SimpleFillSymbol({ // 绘制过程中的polygon样式
                    color: [0, 0, 0, 0],
                    style: "solid",
                    outline: {
                        color: [0, 124, 247, 1],
                        width: 2
                    }
                }),
                polyline: new SimpleLineSymbol({
                    color: [0, 124, 247, 1],
                    width: 2,
                    style: "solid"
                }),
                tempPolyline: new SimpleLineSymbol({
                    color: [0, 124, 247, 1],
                    width: 2,
                    style: "solid"
                })
            }, options.defaultSymbol);
            dojo.query(view.container).addContent('<div id="fn-tooltip-div" style="position:absolute;border: 1px solid #999;padding: 4px;background:#fff0bb;"></div>');
            this.tooltipMsg = dojo.mixin({
                click: '点击开始绘制',
                doubleClick: '双击结束绘制',
                drag: '按下鼠标左键开始绘制',
                dragStart: '移动鼠标进行绘制',
                dragEnd: '松开鼠标左键结束绘制'
            }, options.tooltipMsg);
            this._tooltipNode = dom.byId('fn-tooltip-div');
        },
        _clickGeometryTypes: [types.POLYGON, types.POLYLINE, types.POINT, types.POINT], // 点击绘制的几何类型
        _dragGeometryTypes: [types.FREE_POLYGON, types.FREE_POLYLINE, types.FREE_EXTENT], // 拖拽绘制的几何类型
        properties: {
            start: false,
            end: null,
            pointermove: null,
            drag: null,
            click: null
        },
        _setTooltipMsg: function (msg) {
            if (msg) {
                this._tooltipNode.style.display = "block";
            } else {
                this._tooltipNode.style.display = "none";
            }
            this._tooltipNode.innerHTML = msg;
        },
        registerEvents: function () {
            var me = this;
            this._handlers = [this._mapView.on('click', function (event) { //
                if (dojo.indexOf(me._clickGeometryTypes, me._geometryType) > -1) {
                    if (!me.get('start')) {
                        me.set('start', true);
                        if (me._geometryType !== types.POINT)
                            me._setTooltipMsg(me.tooltipMsg.doubleClick);
                    }
                    //me._drawPoint(event.mapPoint);
                    me._clickPoints.push(event.mapPoint);
                    me._tempGraphicLayer.removeAll();
                    me._drawGraphicLayer.removeAll();
                    event.stopPropagation();
                    var graphic;
                    switch (me._geometryType) {
                        case types.POLYGON:
                            graphic = me._drawPolygon(me._clickPoints); break;
                        case types.POLYLINE:
                            graphic = me._drawPolyline(me._clickPoints); break;
                        case types.POINT:
                            graphic = me._drawPoint(event.mapPoint);
                            me._endDraw(graphic);
                        case types.CIRCLE:
                            graphic = me._drawCircle(me._clickPoints);
                            if (me._clickPoints.length === 2) {
                                me._endDraw(graphic);
                            }
                    }
                    if (graphic) {
                        graphic.mapPoint = event.mapPoint;
                        me.set('click', graphic);
                    } else {
                        me.set('click', { mapPoint: event.mapPoint });
                    }
                }
            }), this._mapView.on('pointer-move', this.debounce(function (event) {
                var graphic = me._dragOrMoveHandler(event);
                if (graphic) {
                    me.set('pointermove', graphic);
                }
            }, 0)), this._mapView.on('double-click', function (event) {
                if (me.get('start')) {
                    event.stopPropagation();
                    me._clickPoints.push(event.mapPoint);
                    me._tempGraphicLayer.removeAll();
                    var graphic = me._generateGraphic();
                    graphic.mapPoint = event.mapPoint;
                    me._endDraw(graphic);
                }
            }), this._mapView.on('pointer-down', function (event) {
                if (dojo.indexOf(me._dragGeometryTypes, me._geometryType) > -1) {
                    if (!me.get('start')) {
                        me.set('start', true);
                        me._setTooltipMsg(me.tooltipMsg.dragStart);
                    }
                }
            }), this._mapView.on('pointer-up', function (event) {
                if (dojo.indexOf(me._dragGeometryTypes, me._geometryType) > -1) {
                    var graphic = me._generateGraphic();
                    me._endDraw(graphic);
                }
            }), this._mapView.on('drag', function (event) {
                var graphic = me._dragOrMoveHandler(event);
                if (graphic) {
                    me.set('drag', graphic);
                }
            })];
        },
        _generateGraphic: function () {
            switch (this._geometryType) {
                case types.POLYGON:
                    return this._drawPolygon(this._clickPoints);
                case types.POLYLINE:
                    return this._drawPolyline(this._clickPoints);
            }
            return null;
        },
        _dragOrMoveHandler: function (event) {
            var node = this._tooltipNode, me = this, graphic;
            node.style.left = (event.x + 15) + 'px';
            node.style.top = (event.y - 25) + 'px';
            if (me.get('start')) {
                var mapPoint = me._mapView.toMap({ x: event.x, y: event.y });
                me._tempGraphicLayer.removeAll();
                switch (me._geometryType) {
                    case types.POLYGON:
                        graphic = me._drawPolygon(me._clickPoints.concat([mapPoint]), me._tempGraphicLayer); break;
                    case types.POLYLINE:
                        graphic = me._drawPolyline(me._clickPoints.concat([mapPoint]), me._tempGraphicLayer); break;
                    case types.Circle:
                        graphic = me._drawCircle(me._clickPoints.concat([mapPoint]), me._tempGraphicLayer); break;
                    case types.FREE_POLYGON:
                        me._freeDraw(event);
                        graphic = me._drawPolygon(me._clickPoints); break;
                    case types.FREE_POLYLINE:
                        me._freeDraw(event);
                        graphic = me._drawPolyline(me._clickPoints); break;
                    case types.FREE_EXTENT:
                        me._freeDraw(event);
                        graphic = me._drawExtent(me._clickPoints); break;
                }
            } else {
                if (me._geometryType.indexOf('free') > -1) {
                    me._setTooltipMsg(me.tooltipMsg.drag);
                } else {
                    me._setTooltipMsg(me.tooltipMsg.click);
                }
            }
            return graphic;
        },
        _freeDraw: function (event) {
            var me = this;
            event.stopPropagation();
            var mapPoint = me._mapView.toMap({ x: event.x, y: event.y });
            me._setTooltipMsg(me.tooltipMsg.dragEnd);
            me._clickPoints.push(mapPoint);
            me._drawGraphicLayer.removeAll();
        },
        _endDraw: function (graphic) {
            this.set('start', false);
            this.set('end', graphic);
            this._drawGraphicLayer.removeAll();
            this._clickPoints = [];
        },
        activate: function (geometryType) {
            this._geometryType = geometryType;
            this.registerEvents();
        },
        deactivate: function () {
            dojo.forEach(this._handlers, function (handler) {
                handler.remove();
            });
            this._tempGraphicLayer.removeAll();
            this._drawGraphicLayer.removeAll();
            this._setTooltipMsg('');
        },
        _drawCircle: function (mapPoints, layer) {
            var _layer = layer || this._drawGraphicLayer;
            if (mapPoints.length === 1) {
                this._drawPoint(mapPoints[0]);
            }
            else if (mapPoints.length === 2) {
                this._drawPolyline(mapPoints, layer);
                var firstPoint = mapPoints[0];
                var secPoint = mapPoints[mapPoints.length - 1];
                var circle = new Circle({
                    spatialReference: this._mapView.spatialReference,
                    center: firstPoint,
                    radius: Math.sqrt(Math.pow(firstPoint.x - secPoint.x, 2) + Math.pow(firstPoint.y - secPoint.y, 2))
                });
                var graphic = new Graphic({
                    geometry: circle,
                    symbol: layer ? this._defaultSymbol.tempPolygon : this._defaultSymbol.polygon
                });
                _layer.add(graphic);
                this._drawPoint(circle.centroid);
                return graphic;
            }
        },
        _drawExtent: function (mapPoints, layer) {
            var _layer = layer || this._drawGraphicLayer;
            if (mapPoints.length >= 2) {
                var firstPoint = mapPoints[0];
                var secPoint = mapPoints[mapPoints.length - 1];
                var extent = new Extent({
                    spatialReference: this._mapView.spatialReference,
                    xmax: Math.max(firstPoint.x, secPoint.x),
                    xmin: Math.min(firstPoint.x, secPoint.x),
                    ymax: Math.max(firstPoint.y, secPoint.y),
                    ymin: Math.min(firstPoint.y, secPoint.y)
                });
                var graphic = new Graphic({
                    geometry: extent,
                    symbol: layer ? this._defaultSymbol.tempPolygon : this._defaultSymbol.polygon
                });
                _layer.add(graphic);
                return graphic;
            }
        },
        _drawPolygon: function (mapPoints, layer) {
            var _layer = layer || this._drawGraphicLayer;
            if (mapPoints.length === 2) {
                this._drawPolyline(mapPoints, layer);
            } else if (mapPoints.length > 2) {
                var polygon = new Polygon({
                    spatialReference: this._mapView.spatialReference,
                    rings: [dojo.map(mapPoints, function (mapPoint) {
                        return [mapPoint.x, mapPoint.y];
                    })]
                });
                var graphic = new Graphic({
                    geometry: polygon,
                    symbol: layer ? this._defaultSymbol.tempPolygon : this._defaultSymbol.polygon
                });
                _layer.add(graphic);
                this._drawPoint(polygon.centroid);
                console.log(polygon.centroid)
                return graphic;
            }
        },
        _drawPolyline: function (mapPoints, layer) {
            var _layer = layer || this._drawGraphicLayer;
            var polyline = new Polyline({
                spatialReference: this._mapView.spatialReference,
                paths: [dojo.map(mapPoints, function (mapPoint) {
                    return [mapPoint.x, mapPoint.y];
                })]
            });
            var graphic = new Graphic({
                geometry: polyline,
                symbol: layer ? this._defaultSymbol.tempPolyline : this._defaultSymbol.polyline
            });
            _layer.add(graphic);
            return graphic;
        },
        _drawPoint: function (mapPoint, layer) {
            var _layer = layer || this._drawGraphicLayer;
            var mp = new Point({
                spatialReference: mapPoint.spatialReference || this._mapView.spatialReference,
                x: mapPoint.x,
                y: mapPoint.y
            });
            var graphic = new Graphic({
                geometry: mp,
                symbol: new SimpleMarkerSymbol()
            });
            _layer.add(graphic);
            return graphic;
        }
    });

    Draw.TYPE = types;

    return Draw;

});

效果图

效果图

小姐

未经过祥测,也许有bug

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,616评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,020评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,078评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,040评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,154评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,265评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,298评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,072评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,491评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,795评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,970评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,654评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,272评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,985评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,223评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,815评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,852评论 2 351

推荐阅读更多精彩内容