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 ...
随机推荐
- 记一次VM虚拟机Ubuntu无法联网问题
突然ubuntu获取不到ipv4地址,手动设置静态ip也ping不通本机, 在网上试了一堆的方法也不行,就怀疑是vm设置问题了.因为 作业环境我的VM需要经常性的改变桥接的网卡,所以检查了一 下这里, ...
- UP_GetRecordByPage
CREATE PROCEDURE [dbo].[UP_GetRecordByPage] @tblName varchar(255), -- 表名 @fldName varchar(255), -- 主 ...
- The threat to world
The threat to world 对世界贸易的威胁"> The rules-based system is in grave(严重的) danger 基于规则的体系岌岌可危 DO ...
- vue 学习笔记(一)
对于 vue 官网给的教程由浅及深,非常容易上手.我之前有过 react 项目开发经验,对 webpack 打包,脚手架这一类的东西并不陌生.所以也是我上手比较快的原因吧.简单将我在学习 vue 中遇 ...
- ubuntu安装QGIS
参考官网https://qgis.org/en/site/forusers/alldownloads.html#debian-ubuntu 但是官网写的太繁琐分散,没有按每个OS集中写cli安装完整过 ...
- 11-类中的__call__函数
__call__是一个很神奇的特性,只要某个类型中有__call__方法,,我们可以把这个类型的对象当作函数来使用. 举例: >>>class Reader(): def __ini ...
- 日常记Bug
前记:后端写代码应该对数据的交互更加掌握,不要被编码.数据模型细节坑住 Unicode编码.Django数据迁移偶尔产生的不稳定 处理细项工资记录模型: class TeachRoll(models. ...
- echarts常用方法,item小坑(二)
在echarts折线图使用过程中,我们会遇到折线拐点symbol的问题.这个问题是在版本3.8.5引用时发现的.折线图在支持legend显示的情况下需要考虑. 问题描述如下:如果采用echarts提供 ...
- ES9新内容概括
本文主要介绍ES2018 新出的一些特性 1.异步迭代 允许 async/await 与 for-of 一起使用,以串行的方式运行异步操作,达到异步函数的迭代效果. async function pr ...
- DBlink 创建 删除 脚本
--配置SQLSERVER数据库的DBLINK --删除dblink Exec sp_droplinkedsrvlogin test,Null Exec sp_dropserver test --创建 ...