目前在4.x API中还未有官方的绘图工具,而实际项目中又需要这样的绘图工具,所以自己写了一个。  奉上代码。

使用方法:
1.将引入的API模块传入构造函数 var drawTools = new DrawTools(view, Graphic, Point, Polyline, Polygon, Circle);
2.定义好相应的symbol后 调用draw方法开始绘制
drawTools.draw("polygon",polygonSymbol,function(graphic){
  //write your code
})
 /*由于4.X版本目前没有画图工具  因此自定义此画图工具
*1.调用构造函数,并传入相应参数:
*Polyline=>Polyline模块;view=>当前的MapView; Graphic=>Graphic模块;
*Point=>Point模块; Polygon=>Polygon模块; Circle=>模块;
*2.调用该类的的draw方方法,并传入相应参数:
*geometryType=>要绘制的几何类型(string): point|polyline|polygon|rectangle|circle
*symbol=>对应的geometry的symbol
*callback=>绘制完成时执行的回掉函数,回掉函数带有一个参数graphic=>所绘制的graphic
*
*可能出bug的操作:在绘制过程中拖拽地图
*
*degined by yumubai ^-^
*/
DrawTools = function (view, Graphic, Point, Polyline, Polygon, Circle) {
this.isDrawActive = false;
this.pointerDownListener = null;
this.pointerMoveListener = null;
this.pointerUpListener = null;
this.doubleClickListener = null;
this.dragListener = null;
this.curGraphic = null;
this.view = view;
this.Graphic = Graphic;
this.Point = Point;
this.Polyline = Polyline;
this.Polygon = Polygon;
this.Circle = Circle;
this.draging = false;//判断用户是否在拖动地图
}; DrawTools.prototype.draw = function (geometryType,symbol, callback) {
this.geometryType = geometryType;
this.symbol = symbol;
this.setFunc ();
if (this.drawEndCallback) this.drawEndCallback = null;
this.drawEndCallback = callback;
this.activeDraw();
}; DrawTools.prototype.setFunc = function () {
switch (this.geometryType) {
case "point": this.drawFunction = this.drawPoint;
break;
case "polyline": this.drawFunction = this.drawLine;
break;
case "polygon": this.drawFunction = this.drawPolygon;
break;
case "rectangle": this.drawFunction = this.drawRectangle;
break;
case "circle": this.drawFunction = this.drawCircle;
break;
};
} DrawTools.prototype.activeDraw = function () {
this.isDrawActive = true;
this.clearGraphic();
this.deactiveDraw();
try {
this.drawFunction();
} catch (err) { }
}; DrawTools.prototype.deactiveDraw = function () {
this.isDrawActive = false;
if (this.pointerDownListener) this.pointerDownListener.remove(); this.pointerDownListener = null;
if (this.pointerMoveListener) this.pointerMoveListener.remove(); this.pointerMoveListener = null;
if (this.pointerUpListener) this.pointerUpListener.remove(); this.pointerUpListener = null;
if (this.doubleClickListener) this.doubleClickListener.remove(); this.doubleClickListener = null;
if (this.dragListener) this.dragListener.remove(); this.dragListener = null;
this.clearGraphic();
}; DrawTools.prototype.clearGraphic = function () {
if (this.curGraphic) this.view.graphics.remove(this.curGraphic);
if (this.moveLine) this.view.graphics.remove(this.moveLine);
this.curGraphic = null;
this.moveLine = null;
}; DrawTools.prototype.createGraphic = function (graphic) {
this.view.graphics.add(graphic);
this.curGraphic = graphic;
}; DrawTools.prototype.createPoint = function (event) {
return this.view.toMap(event);
}; DrawTools.prototype.endDraw = function (event) {
event.stopPropagation();
var graphic = this.curGraphic.clone();
this.deactiveDraw();
this.drawEndCallback(graphic);
}; DrawTools.prototype.drawPoint = function () {
var self = this;
self.pointerUpListener = self.view.on("pointer-up", function (event) {
if (self.draging) {
self.draging = !self.draging;
return;
};
self.clearGraphic();
event.stopPropagation();
var graphic = new self.Graphic(self.createPoint(event), self.symbol);
self.createGraphic(graphic);
self.endDraw(event);
});
self.dragListener = self.view.on("drag", function (event) {
if (event.action == "start") self.draging = true;
});
}; DrawTools.prototype.drawLine = function () {
var self = this;
self.pointerDownListener = self.view.on("pointer-down", function (event) {
event.stopPropagation();
var line = self.curGraphic;
var point = self.createPoint(event);
if (!line) {
var polyline = new self.Polyline({
spatialReference: self.view.spatialReference,
paths: [[point.x, point.y]]
});
var graphic = new self.Graphic(polyline, self.symbol);
self.createGraphic(graphic);
} else {
var line = self.curGraphic.clone();
self.clearGraphic();
var pathLength = line.geometry.paths[0].length;
line.geometry.insertPoint(0, pathLength, point);
self.createGraphic(line);
};
}); self.pointerMoveListener = self.view.on("pointer-move", function (event) {
if (!self.curGraphic) return;
event.stopPropagation();
var point = self.createPoint(event);
var line = self.curGraphic.clone();
var lastPoint = line.geometry.paths[0][line.geometry.paths[0].length - 1];
if (!lastPoint) return;
if (self.moveLine) {
self.view.graphics.remove(self.moveLine);
self.moveLine = null;
};
self.moveLine = new self.Graphic(new self.Polyline({
paths: [[lastPoint[0], lastPoint[1]], [point.x, point.y]],
spatialReference: self.view.spatialReference
}), self.symbol);
self.view.graphics.add(self.moveLine); }); self.doubleClickListener = self.view.on("double-click", function (event) {
self.endDraw(event);
}); self.dragListener = self.view.on("drag", function (event) {
if (event.action == "start") self.draging = !self.draging;
if (event.action == "end") {
self.draging = !self.draging;
if (!self.curGraphic) return;
var line = self.curGraphic.clone();
self.clearGraphic();
var pathLength = line.geometry.paths[0].length;
if (!pathLength) return;
line.geometry.removePoint(0, pathLength - 1);
self.createGraphic(line);
};
});
}; DrawTools.prototype.drawPolygon = function () {
var self = this;
self.pointerDownListener = self.view.on("pointer-down", function (event) {
event.stopPropagation();
var polygon = self.curGraphic;
var point = self.createPoint(event);
if (!polygon) {
var polygon = new self.Polygon({
spatialReference: self.view.spatialReference,
rings: [[point.x, point.y], [point.x, point.y]]
});
var graphic = new self.Graphic(polygon, self.symbol);
self.createGraphic(graphic);
} else {
var polygon = self.curGraphic.clone();
self.clearGraphic();
var ringLength = polygon.geometry.rings[0].length;
polygon.geometry.insertPoint(0, ringLength - 1, point);
self.createGraphic(polygon);
}
}); self.pointerMoveListener = self.view.on("pointer-move", function (event) {
if (!self.curGraphic) return;
event.stopPropagation();
var polygon = self.curGraphic.clone();
self.clearGraphic();
var ringLength = polygon.geometry.rings[0].length;
var point = self.createPoint(event);
polygon.geometry.setPoint(0, ringLength - 1, point);
self.createGraphic(polygon);
}); self.doubleClickListener = self.view.on("double-click", function (event) {
self.endDraw(event);
}); self.dragListener = self.view.on("drag", function (event) {
if (event.action == "start") self.draging = !self.draging;
if (event.action == "end") {
self.draging = !self.draging;
if (!self.curGraphic) return;
var polygon = self.curGraphic.clone();
self.clearGraphic();
var ringLength = polygon.geometry.rings[0].length;
if (!ringLength) return;
polygon.geometry.removePoint(0, ringLength - 2);
self.createGraphic(polygon);
};
});
}; DrawTools.prototype.drawRectangle = function () {
var self = this;
self.dragListener = self.view.on("drag", function (event) {
event.stopPropagation();
var point = self.createPoint(event);
var rectangle = self.curGraphic;
if (event.action == "start") {
if (!rectangle) {
var rectangle = new self.Polygon({
spatialReference: self.view.spatialReference,
rings: [[point.x, point.y], [point.x, point.y], [point.x, point.y], [point.x, point.y]]
});
var graphic = new self.Graphic(rectangle, self.symbol);
self.createGraphic(graphic);
}
};
if (event.action == "update") {
if (!rectangle) return;
var rectangle = self.curGraphic.clone();
self.clearGraphic();
var point = self.createPoint(event);
var originPoint = rectangle.geometry.rings[0][0];
var pointScreen = self.view.toScreen(new self.Point({ x: point.x, y: point.y, spatialReference: self.view.spatialReference }));
var originPointScreen = self.view.toScreen(new self.Point({ x: originPoint[0], y: originPoint[1], spatialReference: self.view.spatialReference }));
var screenRings = [[originPointScreen.x, originPointScreen.y], [pointScreen.x, originPointScreen.y], [pointScreen.x, pointScreen.y], [originPointScreen.x, pointScreen.y]];
var mapRings = screenRings.map(function (point, index) {
var mapPoint = self.view.toMap({ x: point[0], y: point[1] });
return [mapPoint.x, mapPoint.y];
}); var newRectangle = new self.Polygon({
spatialReference: self.view.spatialReference,
rings: mapRings
});
var graphic = new self.Graphic(newRectangle, self.symbol);
self.createGraphic(graphic);
};
if (event.action == "end") {
self.endDraw(event);
};
});
}; DrawTools.prototype.drawCircle = function () {
var self = this;
self.dragListener = self.view.on("drag", function (event) {
event.stopPropagation();
var circle = self.curGraphic;
var point = self.createPoint(event);
if (event.action == "start") {
if (!circle) {
var circle = new self.Circle({
center: point,
radius: 0,
spatialReference: self.view.spatialReference
});
var graphic = new self.Graphic(circle, self.symbol);
self.createGraphic(graphic);
}
};
if (event.action == "update") {
if (!circle) return;
self.clearGraphic();
var center = circle.geometry.center;
var radius = Math.pow((Math.pow((point.x - center.x), 2) + Math.pow((point.y - center.y), 2)), 0.5);
var newCircle = new self.Circle({
center: center,
radius: radius,
spatialReference: self.view.spatialReference
});
var graphic = new self.Graphic(newCircle, self.symbol);
self.createGraphic(graphic);
};
if (event.action == "end") {
self.endDraw(event);
};
});
};

arcgis api for js 4.4 绘图小工具的更多相关文章

  1. arcgis api for js入门开发系列二十打印地图的那些事

    前面我写过关于利用arcgis api for js打印地图的,但是打印地图服务都是基于arcgis server发布的,arcgis api加载在线地图,比如天地图.百度地图.高德地图等,底图都是打 ...

  2. arcgis api for js入门开发系列四地图查询(含源代码)

    备注:由于实现本篇功能的需求,修改了地图数据的dlsearch.mxd,然后更新了地图服务,需要的在文章最后有提供最新的mxd以及源代码下载的 上一篇实现了demo的地图工具栏,本篇新增地图查询功能, ...

  3. arcgis api for js入门开发系列一arcgis api离线部署

    在我的GIS之家QQ群里,很多都是arcgis api for js开发的新手,他们一般都是GIS专业的学生,或者从计算机专业刚刚转向来的giser,他们难免会遇到各种webgis开发的简单问题,由于 ...

  4. arcgis api for js入门开发系列十一地图统计图

    上一篇实现了demo的叠加SHP图层,本篇新增地图统计图,截图如下: 地图统计图实现的思路如下:利用拓展arcgis api的js文件(MapChartGraphic.js以及MapChartGrap ...

  5. 转:arcgis api for js入门开发系列四地图查询

    原文地址:arcgis api for js入门开发系列四地图查询 arcgis for js的地图查询方式,一般来说,总共有三种查询方式:FindTask.IdentifyTask.QueryTas ...

  6. Arcgis API for JS——打印控件乱码

    在通过Arcgis API for JS编写打印控件进行地图下载时,总发现地图字体乱码,如下图: 解决方法: 在装有ArcGIS Server,要调用服务的电脑或服务器上找到下图文件夹

  7. ArcGIS API for js Legend(图例)

    1.说明 有关怎么把ArcGIS API for js部署到IIS上,请参考我上面的写的博客https://www.cnblogs.com/net064/p/10302660.html 2.运行效果 ...

  8. arcgis api for js 地图查询

      arcgis api for js入门开发系列四地图查询(含源代码) 上一篇实现了demo的地图工具栏,本篇新增地图查询功能,包括属性查询和空间查询两大块,截图如下: 属性查询效果图: 空间查询效 ...

  9. arcgis api for js 4.X 出现跨域问题

    arcgis api for js 4.X 出现跨域问题 XMLHttpRequest cannot load http://localhost/4.3/4.3/esri/workers/mutabl ...

随机推荐

  1. HDU 3533 Escape(大逃亡)

    HDU 3533 Escape(大逃亡) /K (Java/Others)   Problem Description - 题目描述 The students of the HEU are maneu ...

  2. Vivado HLS 工具

    干什么的 Vivado HLS工具可以将C语言高级综合为硬件. 为什么要使用HLS 可以在更高的抽象层次描述功能,而不是在传统的RTL级别 一个潜在的用处是,系统设计划分成硬件部分和软件部分之后,软件 ...

  3. PHP curl Post请求和Get请求~

    //获取的参数 $api_key = '8a82d53a57b06c1d835d129f7e43d49c'; $orderNum = pdo_fetch('select ddlm_order_no f ...

  4. Vue-admin工作整理(八): BUS | | 组件通信

    一.父子组件之间通信 思路:定义一个个人组件,个人组件通常需要在前缀统一命名一下,如:AInput,该组件的作用是将编辑框中的内容获取并通过事件提交出去,然后在目标组件(store)中通过双向交互模式 ...

  5. C# 在类中使用Timer定时器以及延时处理的方法

    我们平时在C#中要用到定时功能时,有自带定时器,一般在定时器里面写函数就行了,现在需要在类里面写了一个定时器,不和界面绑定,一开始的时候感觉没什么思路,然后看了一下界面的设计代码,有了思路,还是很简单 ...

  6. Apache Solr入门教程(转)

    1.为什么选择Apache Solr Apache Solr是一个功能强大的搜索服务器,它支持REST风格API.Solr是基于Lucene的,Lucene 支持强大的匹配能力,如短语,通配符,连接, ...

  7. Jmeter 获取系统时间,和对系统时间进行增减时间

    今天做了一个测试,比如发送短信验证码之后的, 验证90s被验证码有效的问题 那如何测试开发的代码,判断了90s内有效呢1. 验证码获取时间距离现在89秒,验证通过2. 验证码获取时间距离现在90秒,验 ...

  8. 多组图自动无限循环(swiper轮播)

    前两天的一个项目中遇到多组图片无限轮播,当时采用了swiper 但是没有解决让它无限轮播.今天再次尝试了一下发现是自己的样式写错了.今天在这里写一下,为了给自己一个警醒不要犯同样的错误 首先先引入一下 ...

  9. CSS中的单位px、em、rem、%、vw、vh、vm

    px 相对长度单位,像素px 是相对于显示器屏幕分辨率而言的.是我们网页设计常用的单位,也是基本单位. 通过 px 可以设置固定的布局或者元素大小,缺点是没有弹性.用 px 设置字体大小时,比较稳定和 ...

  10. java的抽象方法为什么不能是static、final、private?

    1.java的抽象方法为什么不能用static修饰?类抽象方法? 如上代码,在抽象类中定义static属性是没有问题的,但是定义抽象方法时是不能定义为静态(static)的,否则编译器会报错:The ...