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 ...
随机推荐
- Ubuntu16.04 使用sudo cat EOF 编辑文件,提示Permission denied错误的解决办法
一.执行命令报错 在Ubuntu16.04下,使用如下命令,修改hosts主机文件,居然提示权限错误: catty@node186:~$ sudo cat <<EOF > /etc/ ...
- Ant Design of React 框架使用总结1
一. 为什么要用UI 框架 统一了样式交互动画 . Ui框架会对样式,交互动画进行统一,保证了系统风格完整统一,不像拼凑起来的. 兼容性 ,不是去兼容IE 6 7 8那些低版本浏览器,而是对主流的标 ...
- Go语言学习之13 日志管理平台开发
主要内容: 1. ElasticSearch介绍与使用2. kibana介绍与使用 1. ElasticSearch安装 详见上节内容2. kibana安装 (1) 下载ES,下载地址:https:/ ...
- git上传
#1.Git 全局设置: git config --global user.name "skbarcode" git config --global user.email &quo ...
- LeetCode--026--删除排序数组中的重复项(java)
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...
- PYTHON -- 基础3
1.数据类型 1.int 类型 用于计算 1.1 bit_length() 转换为2进制的最小位置 有效的 i = 5 print(i.bit_length()) i 二进制 i.bit_length ...
- SpringBoot之配置文件加载位置
1.SpringBoot启动会扫描application.properties或者application.yml文件作为springboot的配置文件.默认创建项目生成application.prop ...
- vue中的keep-alive
本文转载于:https://blog.csdn.net/xum222222/article/details/80322532 转载仅供个人日后学习 https://www.cnblogs.com/ji ...
- 关于node
nodejs npm常用命令 npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. ...
- xibai的PCI卡在英文系统上安装报错
通过购买时自带的光驱,在里面直接找相对应的型号的驱动,直接安装,即可 不要通过电脑管理,然后更新驱动的这种方式,会报错