p{
text-align:center;
}
blockquote > p > span{
text-align:center;
font-size: 18px; color: #ff0000;
}

-->

Arcgis for js开发之直线、圆、箭头、多边形、集结地等绘制方法


将ARCGIS for Js API中绘制各种图形的方法进行封装,方便调用。用时只需要传入参数既可。(在js文件中进行封装定义);

1、新建js文件,新建空对象用于函数的定义

  1. if (!this["gisTool"]) { gisTool= {}; }
  2. if (!this["gisTool.Map"]) { gisTool.Map = {}; }

定义一个空对象,用于存储各类方法:

  1. //地图交互事件
  2. gisTool.Map.MapTool = {

在mapTool对中进行新建函数,方便不同功能中的调用。

一、获取当前点击的地图坐标

  1. //地图拾取点坐标
  2. getMapPoint: function (callBack) {
  3. map.setMapCursor("crosshair");
  4. var mapHandler = dojo.connect(map, "onClick", function (event) {
  5.  
  6. clearLayer(map, "PointLayer");
  7.  
  8. try {
  9. map.setMapCursor("default");
  10.  
  11. callBack(event.mapPoint);
  12.  
  13. dojo.disconnect(mapHandler);//事件值执行一次
  14. } catch (err) { }
  15.  
  16. });
  17. },

二、绘制圆形(传入对应的参数既可)

  1. drawCircle: function (x, y, R, symbol, graphicLayer, isFly, callBack) {
  2. var ptStart = Geometry.drawPoint(parseFloat(x), parseFloat(y), { wkid: 4832});
  3. var circleGeometry = new esri.geometry.Circle(ptStart, {
  4. "radius": R,
  5. });
  6. if (isFly) {
  7. CenterAt(map, circleGeometry);
  8. }
  9. var graphic = new esri.Graphic(circleGeometry, symbol);
  10. if (callBack != null) {
  11. callBack(circleGeometry);
  12. }
  13. graphicLayer.add(graphic);
  14. },

三、图形定位(单击进行坐标或者geometry定位)

  1. flayCirle: function (map, geometry) {
  2. var extent = geometry.getExtent();
  3. if (geometry.type == "point") {
  4. extent = new esri.geometry.Extent(geometry.x - 0.0000001, geometry.y - 0.0000001, geometry.x - 0 + 0.0000001, geometry.y - 0 + 0.0000001, map.spatialReference);
  5. extent = extent.expand(1.5);
  6. }
  7. if (extent != null) {
  8. var point = new esri.geometry.Point(extent.xmin + (extent.xmax - extent.xmin) / 2, extent.ymin + (extent.ymax - extent.ymin) / 2, map.spatialReference);
  9. var newExtent = new esri.geometry.Extent(point.x, point.y, point.x, point.y, point.spatialReference);
  10. //如果当前视图包含要缩放视图
  11. if (Extent(map.extent, extent)) {
  12. // extent = extent.expand(2);
  13. map.setExtent(extent);
  14. } else {
  15. var firstEx = UnionExtent(newExtent, map.extent);
  16. map.setExtent(firstEx, true);
  17. setTimeout(function () {
  18. map.centerAt(point)
  19. }, 700);
  20. setTimeout(function () {
  21. extent = extent.expand(1.5);
  22. map.setExtent(extent);
  23. }, 1400);
  24. }
  25. }
  26. },

四、绘制多线段PloygonLine

  1. drawPolyLine: function (callback) {
  2. var toolbar = new esri.toolbars.Draw(map, { showTooltips: true });
  3. toolbar.activate(esri.toolbars.Draw.POLYLINE);
  4. dojo.connect(toolbar, "onDrawEnd", function (geometry) {
  5. callback(geometry);
  6. toolbar.deactivate();
  7. });
  8. },

五、将绘制的多线段添加到地图上

  1. addPolyLine: function (points,symbol,graphicLayerName) {
  2. var map = map;
  3. require(["esri/geometry/Polygon", "esri/SpatialReference"], function (Polygon, SpatialReference) {
  4. var polygonLine = new Polygon(new esri.SpatialReference({ wkid: wkid }));
  5. polygonLine.addRing(points);
  6. var graphicsLayer = GrapchicLayer(map, graphicLayerName);
  7. var graphic = new esri.Graphic(polygonLine, symbol);
  8. graphicsLayer.add(graphic);
  9. });
  10. },

六、绘制多边形

  1. drawPolygon:function(callback){
  2. var toolbar = new esri.toolbars.Draw(map, {
  3. showTooltips: true
  4. });
  5. toolbar.activate(esri.toolbars.Draw.POLYGON);
  6. dojo.connect(toolbar, "onDrawEnd", function (geometry) {
  7. callback(geometry);
  8. toolbar.deactivate();
  9. });
  10. },

七、绘制箭头的方法

  1. drawArrow: function (callback) {
  2. var toolbar = new esri.toolbars.Draw(map, { showTooltips: true });
  3. toolbar.activate(esri.toolbars.Draw.ARROW);
  4. dojo.connect(toolbar, "onDrawEnd", function (geometry) {
  5. callback(geometry);
  6. toolbar.deactivate();
  7. });
  8. },

八、将多边形添加到地图上

  1. addPolygon: function (points, symbol, graphicLayerName) {
  2. var map = map;
  3. require(["esri/geometry/Polygon", "esri/SpatialReference"], function (Polygon, SpatialReference) {
  4. var polygon = new Polygon(new esri.SpatialReference({ wkid: 4832}));
  5. polygon.addRing(points);
  6. var graphic = new esri.Graphic(polygon, symbol);
  7. var graphicsLayer = GraphicLayer(map, graphicLayerName);
  8. graphicsLayer.add(graphic);
  9. });
  10. },

九、画线的方法

  1. drawLine: function (callback) {
  2. var toolbar = new esri.toolbars.Draw(map, { showTooltips: true });
  3. toolbar.activate(esri.toolbars.Draw.LINE);
  4. dojo.connect(toolbar, "onDrawEnd", function (geometry) {
  5. callback(geometry);
  6. toolbar.deactivate();
  7. });
  8. },

十、绘制集结地(需要英语第三方的API)

  1. drawGathering_Place: function (pnts,symbol,graphicLayerName) {
  2. this.t = 0.4;
  3. var mid = P.PlotUtils.mid(pnts[0], pnts[1]);
  4. var d = P.PlotUtils.distance(pnts[0], mid) / 0.9;
  5. var pnt = P.PlotUtils.getThirdPoint(pnts[0], mid, P.Constants.HALF_PI, d, true);
  6. pnts = [pnts[0], pnt, pnts[1]];
  7.  
  8. var mid = P.PlotUtils.mid(pnts[0], pnts[2]);
  9. pnts.push(mid, pnts[0], pnts[1]);
  10.  
  11. var normals = [];
  12. for (var i = 0; i < pnts.length - 2; i++) {
  13. var pnt1 = pnts[i];
  14. var pnt2 = pnts[i + 1];
  15. var pnt3 = pnts[i + 2];
  16. var normalPoints = P.PlotUtils.getBisectorNormals(this.t, pnt1, pnt2, pnt3);
  17. normals = normals.concat(normalPoints);
  18. }
  19. var count = normals.length;
  20. normals = [normals[count - 1]].concat(normals.slice(0, count - 1));
  21. var pList = [];
  22. for (i = 0; i < pnts.length - 2; i++) {
  23. pnt1 = pnts[i];
  24. pnt2 = pnts[i + 1];
  25. pList.push(pnt1);
  26. for (var t = 0; t <= P.Constants.FITTING_COUNT; t++) {
  27. var pnt = P.PlotUtils.getCubicValue(t / P.Constants.FITTING_COUNT, pnt1, normals[i * 2], normals[i * 2 + 1], pnt2);
  28. pList.push(pnt);
  29. }
  30. pList.push(pnt2);
  31. }
  32. gisTool.MapTool.addPolygon(pList, symbol,graphicLayerName);
  33. },

十一、绘制自由线

  1. drawFreehandLine: function (callback) {
  2. var toolbars = new esri.toolbars.Draw(map, { showTooltips: true });
  3. toolbars.activate(esri.toolbars.Draw.FREEHAND_POLYLINE);
  4. dojo.connect(toolbars, "onDrawEnd", function (geometry) {
  5. callback(geometry);
  6. toolbars.deactivate();
  7. });
  8. },

十二、手绘多边形

  1. drawFreePolygon: function (callback) {
  2. var toolbars = new esri.toolbars.Draw(map, { showTooltips: true });
  3. toolbars.activate(esri.toolbars.Draw.FREEHAND_POLYGON);
  4. dojo.connect(toolbars, "onDrawEnd", function (geometry) {
  5. callback(geometry);
  6. toolbars.deactivate();
  7. });
  8. },

十三、绘制文字

  1. drawText: function (symbol,graphicLayerName) {
  2. var toolbar = new esri.toolbars.Draw(map, { showTooltips: true });
  3. toolbar.activate(esri.toolbars.Draw.POINT);
  4. map.setMapCursor("crosshair");
  5. toolbar.on("draw-complete", function (evt) {
  6. var point = evt.geometry;
  7. var graphicLayer = GraphicLayer(map, graphicLayerName);
  8. var graphic = esri.Graphic(point, symbol);
  9. graphicLayer.add(graphic);
  10. map.setMapCursor("default");
  11. toolbar.deactivate();
  12. });
  13. },

十四、删除图层上的单一要素的方法

  1. deleteOneGraphic: function (graphicLayerName) {
  2. var map = map, oneEvent;
  3. map.setMapCursor("crosshair");
  4. var graphicLayer = new GraphicLayer(map, graphicLayerName);
  5. if (graphicLayer) {
  6. oneEvent = dojo.connect(graphicLayer, "onClick", function (evt) {
  7. map.setMapCursor("default");
  8. graphicLayer.remove(evt.graphic);
  9. dojo.disconnect(oneEvent);
  10. });
  11. }
  12. },

十五、缩放到指定范围

  1. zoomToGeometry: function (xMin, xMax, yMin, yMax) {
  2. var wkid = Robin.Setting.GlobalSetting.wkid;
  3. //起点、终点
  4. var extent = new esri.geometry.Extent(xMin, yMin, xMax, yMax, new esri.SpatialReference({ wkid: wkid }));
  5. Extent(map, extent.expand(3));
  6. }

关于传入的参数进行说明:

graphicLayerName:为指定的需要操作的图层名称,根据具体的图层名称方便后期的管理。

symbol:线或者面的填充样式,一般的定义样式如下:

  1. var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID,
  2. new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
  3. new dojo.Color([255, 0, 0, 0.6]), 4),
  4. new dojo.Color([0, 255,0 , 0.6]));

传入的point的值是callback中返回的geometry中解析出的: var point = geometry.rings[0];


说明:总结仅供参考,有意见保留。


Arcgis for js开发之直线、圆、箭头、多边形、集结地等绘制方法的更多相关文章

  1. arcgis for js开发之路径分析

    arcgis for js开发之路径分析 //方法封装 function routeplan(x1, x2, y1, y2, barrierPathArray, isDraw, callback) { ...

  2. 3、ObjectARX开发创建直线、圆、圆弧和修改对象属性

    一.本节课程 Arx二次开发创建直线.圆.圆弧和修改对象属性 二.本节要讲解的知识点 1.如何应用C++ ARX二次开发创建直线. 2.如何应用C++ ARX二次开发创建圆. 3.如何应用C++ AR ...

  3. arcgis for js学习之Draw类

    arcgis for js学习之Draw类 <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...

  4. Arcgis Javascript API 开发笔记

    JS API3.4的要求 à(1)  IE9或以上版本 否则dijit1.8.3不匹配 1.如何发布ArcgisJavascript API应用 0.准备工作: (1).有web应用: (2).有js ...

  5. JS开发HTML5游戏《神奇的六边形》(四)

    近期出现一款魔性的消除类HTML5游戏<神奇的六边形>,今天我们一起来看看如何通过开源免费的青瓷引擎(www.zuoyouxi.com)来实现这款游戏. (点击图片可进入游戏体验) 因内容 ...

  6. Arcgis for Js之加载wms服务

    概述:本节讲述Arcgis for Js加载ArcgisServer和GeoServer发布的wms服务. 1.定义resourceInfo var resourceInfo = { extent: ...

  7. 手把手教你学node之搭建node.js开发环境

    搭建node.js开发环境 本文只针对在Linux或者Mac下面.至于使用 Windows 并坚持玩新技术的同学,我坚信他们一定有着过人的.甚至是不可告人的兼容性 bug 处理能力,所以这部分同学麻烦 ...

  8. 一个关于原生 js 开发一款插件的前端教程

    教程链接: http://www.codeasily.net/course/plugin_course/ 写的不是很好,前面比较松后面比较急,请大家见谅,本人也没多少年前端经验,拿以前写过的教程网站, ...

  9. WebAppBuilder独立于Portal之arcgis for js应用框架研究

    1.前言 最近在做项目过程中,用到了WAB,先做一下总结和归类.Webappbuilder(简称WAB)是运行在portal或者online的一款webGIS开发应用程序,其代码开源并且具有优秀的设计 ...

随机推荐

  1. [Swift]LeetCode30. 与所有单词相关联的字串 | Substring with Concatenation of All Words

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  2. [Swift]LeetCode304. 二维区域和检索 - 矩阵不可变 | Range Sum Query 2D - Immutable

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  3. [Swift]LeetCode387. 字符串中的第一个唯一字符 | First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  4. [Swift]LeetCode441. 排列硬币 | Arranging Coins

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  5. [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

  6. PHP的Memcached简单实现

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.也可动态缓存一些实 ...

  7. .NET Core实战项目之CMS 第九章 设计篇-白话架构设计

    前面两篇文章给大家介绍了我们实战的CMS系统的数据库设计,源码也已经上传到服务器上了.今天我们就好聊聊架构设计,在开始之前先给大家分享一下这几天我一直在听的<从零开始学架构>里面关于架构设 ...

  8. metasploit无法连接postgresql

    注:倒数两条可以不做. 问题地址:https://askubuntu.com/questions/50621/cannot-connect-to-postgresql-on-port-5432 设置好 ...

  9. 【netty】(1)---BIO NIO AIO演变

    BIO NIO AIO演变 Netty是一个提供异步事件驱动的网络应用框架,用以快速开发高性能.高可靠的网络服务器和客户端程序.Netty简化了网络程序的开发,是很多框架和公司都在使用的技术. Net ...

  10. 如何在.NET Core控制台程序中使用依赖注入

    背景介绍 依赖注入(Dependency Injection), 是面向对象编程中的一种设计原则,可以用来减低代码之间的耦合度.在.NET Core MVC中 我们可以在Startup.cs文件的Co ...