arcgis api for js 4.4 绘图小工具
目前在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 绘图小工具的更多相关文章
- arcgis api for js入门开发系列二十打印地图的那些事
前面我写过关于利用arcgis api for js打印地图的,但是打印地图服务都是基于arcgis server发布的,arcgis api加载在线地图,比如天地图.百度地图.高德地图等,底图都是打 ...
- arcgis api for js入门开发系列四地图查询(含源代码)
备注:由于实现本篇功能的需求,修改了地图数据的dlsearch.mxd,然后更新了地图服务,需要的在文章最后有提供最新的mxd以及源代码下载的 上一篇实现了demo的地图工具栏,本篇新增地图查询功能, ...
- arcgis api for js入门开发系列一arcgis api离线部署
在我的GIS之家QQ群里,很多都是arcgis api for js开发的新手,他们一般都是GIS专业的学生,或者从计算机专业刚刚转向来的giser,他们难免会遇到各种webgis开发的简单问题,由于 ...
- arcgis api for js入门开发系列十一地图统计图
上一篇实现了demo的叠加SHP图层,本篇新增地图统计图,截图如下: 地图统计图实现的思路如下:利用拓展arcgis api的js文件(MapChartGraphic.js以及MapChartGrap ...
- 转:arcgis api for js入门开发系列四地图查询
原文地址:arcgis api for js入门开发系列四地图查询 arcgis for js的地图查询方式,一般来说,总共有三种查询方式:FindTask.IdentifyTask.QueryTas ...
- Arcgis API for JS——打印控件乱码
在通过Arcgis API for JS编写打印控件进行地图下载时,总发现地图字体乱码,如下图: 解决方法: 在装有ArcGIS Server,要调用服务的电脑或服务器上找到下图文件夹
- ArcGIS API for js Legend(图例)
1.说明 有关怎么把ArcGIS API for js部署到IIS上,请参考我上面的写的博客https://www.cnblogs.com/net064/p/10302660.html 2.运行效果 ...
- arcgis api for js 地图查询
arcgis api for js入门开发系列四地图查询(含源代码) 上一篇实现了demo的地图工具栏,本篇新增地图查询功能,包括属性查询和空间查询两大块,截图如下: 属性查询效果图: 空间查询效 ...
- arcgis api for js 4.X 出现跨域问题
arcgis api for js 4.X 出现跨域问题 XMLHttpRequest cannot load http://localhost/4.3/4.3/esri/workers/mutabl ...
随机推荐
- 【python 3】 函数 初识
函数初识 1.函数的定义.调用.返回值 函数的定义.调用.返回值 def demo(): ## 定义函数 (def + 空格 + 函数名 + () + 冒号) ## 如下为函数体 return a # ...
- 北京AI外包团队 祝大家2019事业有事,大吉大利!
未来已来,以人工智能为核心的科技力量,在重新塑造着我们生活的环境.这种重新塑造的现象如此之明显,力量如此强大,以至于越来越多的人在讨论,我们面临着新一轮的工业革命.而且现在我们面临的这次新的科技力量, ...
- vue 学习笔记(一)
对于 vue 官网给的教程由浅及深,非常容易上手.我之前有过 react 项目开发经验,对 webpack 打包,脚手架这一类的东西并不陌生.所以也是我上手比较快的原因吧.简单将我在学习 vue 中遇 ...
- js斐波那契数列
斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...... 这个数列从第3项开始,每一项都等于前两项之和. 1.递归算法: function ...
- 正向代理or反向代理
正向代理 我访问不了某网站比如www.google.com,但是我能访问一个代理服务器 这个代理服务器呢,它能访问那个我不能访问的网站,于是我先连上代理服务器,告诉它我需要那个无法访问网站的内容,代理 ...
- C++ 命名管道 与Winform跨进程通信
以下是.NET命名管道解决方案中几个主要的类. NamedPipeNative:这个类和kernal32.dll联系实现命名管道的通信,其中包含一些常用方法和常量. NamedPipeWrapper ...
- Previous operation has not finished;run 'cleanup' if it was interrupted;Please execute the 'Cleanup' command.
今天更新文件夹时svn报错如下 提示说让clean up,但是clean up又提示fail,让继续clean up,这样就陷入死循环了…… 搜了多种解决办法后找到原因:当时正在打开着svn的某个文件 ...
- 苹果手机的SB系列(2)为什么不能重命名?
为什么没有重命名? 在手机端不能重命名,在WINOWS端文件是只读的,连他TM的只读属性都无法改,不能重命名,你让我怎么备份? 我怎么知道哪些照片上次备份过了?又重头来过?还是要用苹果的MAC?这种态 ...
- 用VS2017编写C语言的Hello World
1.新建项目 2.选择新建空项目 3.在源文件处右键单击,选择添加-新建项 4.选择“c++文件”,将名称后缀改成.c即可用C语言编写程序 5.编写代码: #include <stdio.h&g ...
- ES6 中 Promise
在说Promise之前我们先简单说一下什么是同步异步? 同步(Sync):所谓同步,就是发出一个功能调用时,在没有得到结果之前,该调用就不返回或继续执行后续操作. 异步(Async):异步与同步相对, ...