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 ...
随机推荐
- Springboot中Jackson的操作
有一段时间没写博客了,虽然是菜鸟一枚但毕竟总要有东西记录学习的,我相信有志者事竟成.今天在工作中使用Jackson转换了一个javabean,传到测试服上之后发现日期少了一天,使用的是@JsonFor ...
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group t1,customer t2
### SQL: select t1.gid,t1.gname,t1.gvalue,t1.gtype, t1.gaddress,t1.gmembers, t1.gcode,t1.gphone, t2. ...
- 移动质量(MQ)测试系列
移动质量(MQ)测试 向移动开发者提供专业.稳定.全面.高价值的自动化测试平台. 发现 APP 中的各类隐患,包括 APP 崩溃.各类兼容性.功能性.性能问题等等. MQ 的 5 大功能(兼容性测试. ...
- laravel——基础增删改查
一.控制器代码 <?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class CurdContro ...
- HTML5外包团队 更新一下2019最新案例
本项目控件均为动态加载,3D部分使用Unity3D,其它基于ReactJS,NodeJS,部分使用cocos2D,由于项目涉密,只能发部分截图,欢迎联系索取更多案例,企鹅号 372900288 祝大家 ...
- IDEA 介绍
转载:https://blog.csdn.net/kanchaishaonian/article/details/81107210 前言:IntelliJ IDEA 如果说IntelliJ IDEA是 ...
- 金蝶K3常用数据表
金蝶K3WISE常用数据表 K3Wise 14.2 清空密码update t_User set FSID=') F ", ,P T #8 *P!D &D 80!N &@ &l ...
- 使用pyinstaller打包多个py文件为一个EXE文件
1. 安装pyinstaller. pip install pyinstaller !!!!64位win7上打包后始终不能用,提示找不到ldap模块,换了32位win7就好了.!!!!(代码中涉及ld ...
- vue中data中引用本地图片报错404
首先说明vue-cli中assets和static两个文件的区别 1.assets在项目编译的过程中会被webpack处理理解为模块依赖,如果执行npm run dev或npm run build命令 ...
- 【期望dp】绵羊跳弹簧
[期望dp] 绵羊跳弹簧 >>>>题目 [题目] T 组数据.对于每一组数据,有n+1 个格子从0 到n 标号,绵羊从0 号结点开始,每次若在 x 位置掷骰子,令掷出的数为nu ...