JTMapKit.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  1. /**
  2. * 初始化函数
  3. * @param {any} option 一个JSON类型
  4. * selector 地图选择器 对应的DIV
  5. * onMapReady 地图加载完成调用
  6. * onViewReady 地图加载完成后加载底图
  7. * mapType 地图类型 '2d'或者'3d' 默认是'2d'
  8. * center 中心点
  9. * extent 显示范围
  10. * basemap 基础底图
  11. */
  12. function JTMapKit(option) {
  13. this.mapOption = {};
  14. this._init(option || {});
  15. //默认点符号
  16. this.pointSymbol = {
  17. type: "simple-marker",
  18. style: "circle",
  19. color: "red",
  20. size: "12px",
  21. outline: {
  22. color: [255, 255, 0],
  23. width: 1
  24. }
  25. }
  26. //起始点点符号
  27. this.pointStrSymbol = {
  28. type: "simple-marker",
  29. style: "circle",
  30. color: "red",
  31. size: "12px",
  32. outline: {
  33. color: [255, 255, 0],
  34. width: 1
  35. }
  36. }
  37. //终止点点符号
  38. this.pointEndSymbol = {
  39. type: "simple-marker",
  40. style: "circle",
  41. color: "green",
  42. size: "12px",
  43. outline: {
  44. color: [255, 255, 0],
  45. width: 1
  46. }
  47. }
  48. //中间点点符号
  49. this.pointMiddleSymbol = {
  50. type: "simple-marker",
  51. style: "circle",
  52. color: [128, 128, 128],
  53. size: "10px",
  54. outline: {
  55. color: [255, 255, 0],
  56. width: 1
  57. }
  58. }
  59. //默认线符号
  60. this.lineSymbol = {
  61. type: "simple-line",
  62. color: [0, 0, 255],
  63. width: 1,
  64. cap: "round",
  65. join: "round"
  66. }
  67. //默认面符号
  68. this.fillSymbol = {
  69. type: "simple-fill",
  70. color: [4, 90, 141, 0.3],
  71. style: "solid",
  72. outline: {
  73. color: [0, 0, 255],
  74. width: 1
  75. }
  76. }
  77. //默认文字符号
  78. this.textSymbol = {
  79. type: 'text',
  80. color: "#FFFFFF",
  81. haloColor: "#555555",
  82. haloSize: "2px",
  83. text: '',
  84. xoffset: 0,
  85. yoffset: -20,
  86. font: {
  87. size: 10,
  88. family: "Playfair Display",
  89. weight: "bold"
  90. }
  91. };
  92. this.isDrawing = false; //正在绘制中,用于控制多点绘制
  93. }
  94. /**
  95. * 原型中添加的方法
  96. * @param {any} option
  97. */
  98. JTMapKit.prototype.extend = function(option) {
  99. for (key in option) {
  100. JTMapKit.prototype[key] = option[key];
  101. }
  102. }
  103. /**+++++++++++++++初始化相关内容+++++++++++++++++*/
  104. JTMapKit.prototype.extend({
  105. /**
  106. * 默认执行的初始化信息
  107. * @param {any} option
  108. */
  109. _init: function(option) {
  110. this.selector = option.selector || 'map'; //地图选择器 对应的DIV控件
  111. this.serverUrl = option.serverUrl;
  112. this.onMapReady = option.onMapReady; //地图加载完后处理
  113. this.onViewReady = option.onViewReady; //mapView加载完成后添加底图
  114. this.onDrawEventComplete = option.onDrawCompleteEvent; //绘制完成激活事件
  115. this.onMeasureEventComplete = option.onMeasureCompleteEvent; //测量结束事件
  116. this.onViewEventPointMove = option.onViewEventPointMove; //地图鼠标移动事件
  117. this.onQueryTackEvent = option.onQueryTackEvent; //查询结果监听事件
  118. this.onViewEventChange = option.onViewEventChange; //视图变化监听事件
  119. this.onPrintEventProgress = option.onPrintEventProgress; //地图正在输出中
  120. this.onPrintEventComplete = option.onPrintEventComplete; //地图输出完成事件
  121. this.onEventError = option.onEventError; //错误事件接收
  122. this.mapType = option.mapType || '2d'; //地图类型,2d,3d
  123. //小组件
  124. this.uiZoom = null; //放大缩小组件
  125. this.uiScaleBar = null; //比例尺组件
  126. //地图相关参数
  127. this.mapOption.basemap = option.basemap; //底图
  128. this.mapOption.center = option.center; //中心点
  129. this.mapOption.extent = option.extent; //全图
  130. this.subDomains = ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"]; //天地图的URL服务器
  131. this.baseMapVectorURL =
  132. "http://{subDomain}.tianditu.gov.cn/DataServer?T=vec_w&x={col}&y={row}&l={level}&tk=944815e0a333f538646868d896ef7378";
  133. this.baseMapVectorLabelURL =
  134. "http://{subDomain}.tianditu.gov.cn/DataServer?T=cva_w&x={col}&y={row}&l={level}&tk=944815e0a333f538646868d896ef7378";
  135. this.baseMapRasterURL =
  136. "http://{subDomain}.tianditu.gov.cn/DataServer?T=img_w&x={col}&y={row}&l={level}&tk=944815e0a333f538646868d896ef7378";
  137. this.baseMapRasterLabelURL =
  138. "http://{subDomain}.tianditu.gov.cn/DataServer?T=cia_w&x={col}&y={row}&l={level}&tk=944815e0a333f538646868d896ef7378";
  139. //绘图相关
  140. this.draw = undefined;
  141. if (this.mapType.toLocaleLowerCase() === '3d') {
  142. } else {
  143. this._init2dMap();
  144. }
  145. },
  146. /** 初始化二维地图 */
  147. _init2dMap: function() {
  148. var self = this;
  149. require(["esri/Map", "esri/views/MapView", "esri/core/urlUtils"], function(Map, MapView, urlUtils) {
  150. //此项操作是为了设置代理 否则将无法使用TileLayer访问切片服务,导致跨域问题产生
  151. //urlUtils.addProxyRule({
  152. // urlPrefix: "http://218.59.194.82:6080/",
  153. // proxyUrl: "http://218.59.194.74:8088/DotNet/proxy.ashx"
  154. //});
  155. self.map = new Map({
  156. basemap: self.mapOption.basemap
  157. });
  158. self.mapView = new MapView({
  159. container: self.selector,
  160. map: self.map,
  161. });
  162. //清空所有自带组件
  163. self.mapView.ui.components = [];
  164. if (self.onViewReady) {
  165. self.onViewReady(self.mapView);
  166. }
  167. if (!self.loadLayers) {
  168. self.loadLayers = [];
  169. }
  170. self.mapView.when(function() {
  171. //注册地图视图鼠标移动事件
  172. self.mapView.on("pointer-move", function(event) {
  173. if (self.onViewEventPointMove) {
  174. var mapPoint = self.mapView.toMap({
  175. x: event.x,
  176. y: event.y
  177. });
  178. //构建JSON
  179. var result = {
  180. sx: event.x.toFixed(3),
  181. sy: event.y.toFixed(3),
  182. mx: mapPoint.x.toFixed(3),
  183. my: mapPoint.y.toFixed(3)
  184. }
  185. self.onViewEventPointMove(result);
  186. }
  187. });
  188. //注册地图拖动事件
  189. self.mapView.on("drag", function(event) {
  190. if (self.onViewEventChange) {
  191. self.onViewEventChange(self.mapView.extent);
  192. }
  193. });
  194. //注册地图滚轮滚动事件
  195. self.mapView.on("mouse-wheel", function(event) {
  196. //此处使用延时 主要是因为mouse-wheel事件是在滚轮刚开始滚动时就开始
  197. //而不是在滚动结束时开始
  198. window.setTimeout(function() {
  199. if (self.onViewEventChange) {
  200. self.onViewEventChange(self.mapView.extent);
  201. }
  202. }, 1000);
  203. });
  204. //注册地图视窗变化事件
  205. self.mapView.on("resize", function(event) {
  206. if (self.onViewEventChange) {
  207. self.onViewEventChange(self.mapView.extent);
  208. }
  209. });
  210. //地图视图实例化完成
  211. if (self.onMapReady) {
  212. self.onMapReady(self.map, self.mapView);
  213. }
  214. }, function(error) {
  215. console.log("地图加载失败", error);
  216. });
  217. });
  218. },
  219. /**
  220. * 设置范围
  221. * @param {any} extent
  222. */
  223. setExtent: function(extent) {
  224. this.mapView.extent = extent;
  225. },
  226. /**
  227. * 设置放大比例
  228. * @param {any} zoom
  229. */
  230. setZoom: function(zoom) {
  231. this.mapView.zoom = zoom;
  232. },
  233. /**
  234. * 设置地图中心点
  235. * @param {any} center
  236. */
  237. setCenter: function(center) {
  238. this.mapView.center = center;
  239. },
  240. /**
  241. * 设置比例尺
  242. * @param {any} scale
  243. */
  244. setScale: function(scale) {
  245. this.mapView.scale = scale;
  246. },
  247. // 隐藏放大缩小的工具条
  248. hidenZoomToolBar: function() {
  249. this.mapView.ui.remove("zoom");
  250. },
  251. /** 隐藏底部的LOGO */
  252. hidenLogo: function() {
  253. this.mapView.ui.remove("attribution");
  254. },
  255. /** 放大 */
  256. zoomIn: function() {
  257. var self = this;
  258. require(["esri/widgets/Zoom"], function(Zoom) {
  259. var zoom = new Zoom({
  260. view: self.mapView
  261. });
  262. zoom.zoomIn();
  263. });
  264. },
  265. /** 缩小 */
  266. zoomOut: function() {
  267. var self = this;
  268. require(["esri/widgets/Zoom"], function(Zoom) {
  269. var zoom = new Zoom({
  270. view: self.mapView
  271. });
  272. zoom.zoomOut();
  273. });
  274. },
  275. /**
  276. * 定位地图中心位置
  277. * @param {any} longitude 经度
  278. * @param {any} latitude 纬度
  279. */
  280. zoomTo: function(longitude, latitude) {
  281. var self = this;
  282. require(["esri/geometry/SpatialReference",
  283. "esri/geometry/Extent"
  284. ], function(SpatialReference, Extent) {
  285. var spa = new SpatialReference({
  286. wkid: 4326
  287. });
  288. var extent = new Extent({
  289. xmin: parseFloat(longitude) - 0.002,
  290. ymin: parseFloat(latitude) - 0.002,
  291. xmax: parseFloat(longitude) + 0.002,
  292. ymax: parseFloat(latitude) + 0.002,
  293. spatialReference: spa
  294. })
  295. self.mapView.extent = extent;
  296. });
  297. },
  298. });
  299. /**++++++++++++++添加小组件++++++++++++++++++++*/
  300. JTMapKit.prototype.extend({
  301. /**
  302. * 添加放大缩小组件
  303. */
  304. setUIZoomVisible: function(isVisible) {
  305. var self = this;
  306. require(["esri/widgets/Zoom"], function(Zoom) {
  307. if (self.uiZoom == null) {
  308. self.uiZoom = new Zoom({
  309. view: self.mapView,
  310. id: "toolsZoom"
  311. });
  312. }
  313. var widget = self.mapView.ui.find("toolsZoom");
  314. if (isVisible) {
  315. if (widget == undefined) {
  316. self.mapView.ui.add(self.uiZoom, {
  317. position: "top-left",
  318. });
  319. }
  320. } else {
  321. if (widget != undefined) {
  322. self.mapView.ui.remove(self.uiZoom);
  323. }
  324. }
  325. });
  326. },
  327. /**
  328. * 添加放大缩小组件
  329. */
  330. setUIScaleBarVisible: function(isVisible) {
  331. var self = this;
  332. require(["esri/widgets/ScaleBar"], function(ScaleBar) {
  333. if (self.uiScaleBar == null) {
  334. self.uiScaleBar = new ScaleBar({
  335. view: self.mapView,
  336. id: "toolsScaleBar",
  337. unit: "metric",
  338. });
  339. }
  340. var widget = self.mapView.ui.find("toolsScaleBar");
  341. if (isVisible) {
  342. if (widget == undefined) {
  343. self.mapView.ui.add(self.uiScaleBar, {
  344. position: "bottom-left",
  345. });
  346. }
  347. } else {
  348. if (widget != undefined) {
  349. self.mapView.ui.remove(self.uiScaleBar);
  350. }
  351. }
  352. });
  353. },
  354. });
  355. /**+++++++++++++++添加加载图层的相关内容+++++++++*/
  356. JTMapKit.prototype.extend({
  357. /**
  358. * 添加到图层中
  359. * @param {any} id 图层ID 如果时服务图层 则代表子图层的索引Index
  360. * @param {any} title 图层的名称
  361. * @param {any} visible 图层是否可见
  362. * @param {any} type 图层的类型
  363. * @param {any} slayer 所属服务图层
  364. */
  365. _addLayerToLayers: function(id, title, visible, type, slayer) {
  366. var self = this;
  367. self.loadLayers.push({
  368. id: id,
  369. title: title,
  370. visible: visible,
  371. type: type,
  372. slayer: slayer
  373. });
  374. },
  375. /**
  376. * 添加动态服务图层到地图中
  377. * @param {any} option 选项
  378. * @param {any} index 添加到地图中在图层中的顺序
  379. * @param {any} name 图层名称
  380. * @param {any} callback 添加完成时回调
  381. */
  382. addDynamicLayer: function(option, index, name, callback) {
  383. if (!option) {
  384. return
  385. }
  386. var self = this;
  387. require(["esri/layers/MapImageLayer"], function(MapImageLayer) {
  388. // printToConsole(JSON.stringify(option));
  389. var layer = new MapImageLayer(option);
  390. layer.title = name;
  391. //添加图层到视图中执行该代码
  392. layer.on("layerview-create", function() {
  393. //循环所有子图层
  394. layer.allSublayers.find(function(sublayer) {
  395. self._addLayerToLayers(sublayer.id, sublayer.title, sublayer
  396. .visible, "sublayer", layer);
  397. });
  398. if (callback) {
  399. callback(layer.fullExtent);
  400. }
  401. });
  402. layer.on("layerview-create-error", function(event) {
  403. alert(event.error.message);
  404. });
  405. self.map.add(layer, index);
  406. });
  407. },
  408. /**
  409. * 添加发布的要素服务 单独一层
  410. * @param {any} option 地图选项
  411. * @param {any} index 地图图层索引
  412. */
  413. addFeatureLayer: function(option, index, name, callback) {
  414. if (!option) {
  415. return
  416. }
  417. option.outFields ? option.outFields : ["*"];
  418. var self = this;
  419. require(["esri/layers/FeatureLayer"], function(FeatureLayer) {
  420. var layer = new FeatureLayer(option);
  421. layer.title = name;
  422. //添加图层到视图中执行该代码
  423. layer.on("layerview-create", function(event) {
  424. //将该图层添加到图层中
  425. _addLayerToLayers(layer.id, layer.title, layer.visible, "featurelayer",
  426. undefined);
  427. if (callback) {
  428. callback();
  429. }
  430. });
  431. layer.on("layerview-create-error", function(event) {
  432. alert(event.error.message);
  433. });
  434. self.map.add(layer, index);
  435. return layer;
  436. });
  437. },
  438. /**
  439. * 添加发布的切片服务
  440. * @param {any} option 地图选项
  441. * @param {any} index 地图图层索引
  442. */
  443. addTileLayer: function(option, index, name, callback) {
  444. if (!option) {
  445. return
  446. }
  447. var self = this;
  448. require(["esri/layers/TileLayer"], function(TileLayer) {
  449. var layer = new TileLayer(option);
  450. layer.title = name;
  451. //添加图层到视图中执行该代码
  452. layer.on("layerview-create", function() {
  453. //将该图层添加到图层中
  454. self._addLayerToLayers(layer.id, layer.title, layer.visible, "tilelayer",
  455. layer);
  456. if (callback) {
  457. callback();
  458. }
  459. });
  460. layer.on("layerview-create-error", function(event) {
  461. alert(event.error.message);
  462. });
  463. self.map.add(layer, index);
  464. return layer;
  465. });
  466. },
  467. /**
  468. * 添加WMS服务图层
  469. * @param {any} option 地图选项
  470. * @param {any} index 地图图层索引
  471. */
  472. addWebTitleLayer: function(option, index, name, callback) {
  473. if (!option) {
  474. return;
  475. }
  476. var self = this;
  477. require(["esri/layers/WebTileLayer"], function(WebTileLayer) {
  478. var layer = new WebTileLayer(option);
  479. layer.title = name;
  480. //添加图层到视图中执行该代码
  481. layer.on("layerview-create", function() {
  482. //将该图层添加到图层中
  483. self._addLayerToLayers(layer.id, layer.title, layer.visible, "webtilelayer",
  484. undefined);
  485. if (callback) {
  486. callback();
  487. }
  488. });
  489. layer.on("layerview-create-error", function(event) {
  490. alert(event.error.message);
  491. });
  492. self.map.add(layer, index);
  493. return layer;
  494. });
  495. },
  496. /**
  497. * 加载底图
  498. * @param {Object} callback 加载完成后回调
  499. */
  500. loadBaseMap: function(callback) {
  501. var self = this;
  502. //添加天地图的影像图 回调后添加其他图层 主要时为了保证能够正常获取地图视图的坐标系
  503. self.addWebTitleLayer({
  504. urlTemplate: self.baseMapRasterURL,
  505. subDomains: self.subDomains,
  506. }, 0, "天地图影像图", function() {
  507. //添加天地图的标注图
  508. self.addWebTitleLayer({
  509. urlTemplate: self.baseMapRasterLabelURL,
  510. subDomains: self.subDomains,
  511. }, 100, "天地图标注", function() {
  512. callback();
  513. });
  514. });
  515. },
  516. /**
  517. * 切换底图为影像底图
  518. * @param {Object} callback 加载完成回调
  519. */
  520. loadBaseRasterMap: function(callback) {
  521. var self = this;
  522. self.map.layers.removeAt(0);
  523. self.map.layers.removeAt(100);
  524. //添加天地图的影像图 回调后添加其他图层 主要时为了保证能够正常获取地图视图的坐标系
  525. self.addWebTitleLayer({
  526. urlTemplate: self.baseMapRasterURL,
  527. subDomains: self.subDomains,
  528. }, 0, "天地图影像图", function() {
  529. //添加天地图的标注图
  530. self.addWebTitleLayer({
  531. urlTemplate: self.baseMapRasterLabelURL,
  532. subDomains: self.subDomains,
  533. }, 100, "天地图标注", function() {
  534. callback();
  535. });
  536. });
  537. },
  538. /**
  539. * 切换底图为标准底图
  540. * @param {Object} callback 加载完成回调
  541. */
  542. loadBaseVectorMap: function(callback) {
  543. var self = this;
  544. self.map.layers.removeAt(0);
  545. self.map.layers.removeAt(100);
  546. //添加天地图的影像图 回调后添加其他图层 主要时为了保证能够正常获取地图视图的坐标系
  547. self.addWebTitleLayer({
  548. urlTemplate: self.baseMapVectorURL,
  549. subDomains: self.subDomains,
  550. }, 0, "天地图影像图", function() {
  551. //添加天地图的标注图
  552. self.addWebTitleLayer({
  553. urlTemplate: self.baseMapVectorLabelURL,
  554. subDomains: self.subDomains,
  555. }, 100, "天地图标注", function() {
  556. callback();
  557. });
  558. });
  559. },
  560. });
  561. /**+++++++++++++++草图编辑相关+++++++++++++++++++*/
  562. JTMapKit.prototype.extend({
  563. _sketchViewModel: function(callback) {
  564. var self = this;
  565. require([
  566. "esri/layers/GraphicsLayer",
  567. "esri/widgets/Sketch/SketchViewModel"
  568. ],
  569. function(GraphicsLayer, SketchViewModel) {
  570. if (!self.layerSketch) {
  571. self.layerSketch = new GraphicsLayer();
  572. self.map.add(self.layerSketch);
  573. }
  574. if (!self.sketchViewModel) {
  575. self.sketchViewModel = new SketchViewModel({
  576. layer: self.layerSketch,
  577. view: self.mapView
  578. });
  579. self.sketchViewModel.on("create", function(event) {
  580. if (event.state == "start") {
  581. self.layerSketch.removeAll(); //删除全部
  582. } else if (event.state == "complete") {}
  583. });
  584. }
  585. callback();
  586. });
  587. },
  588. //激活绘制面工具
  589. sketchPolygonTools: function() {
  590. var self = this;
  591. this._sketchViewModel(function() {
  592. self.sketchViewModel.create("polygon", {
  593. mode: "click"
  594. });
  595. });
  596. },
  597. //前进
  598. sketchRedo: function() {
  599. var self = this;
  600. this._sketchViewModel(function() {
  601. self.sketchViewModel.redo();
  602. });
  603. },
  604. //回退
  605. sketchUndo: function() {
  606. var self = this;
  607. this._sketchViewModel(function() {
  608. self.sketchViewModel.undo();
  609. });
  610. },
  611. //删除全部
  612. sketchDelete: function() {
  613. var self = this;
  614. this._sketchViewModel(function() {
  615. self.layerSketch.removeAll(); //删除全部
  616. });
  617. },
  618. //获取绘制的要素
  619. sketchGraphic: function(callback) {
  620. var self = this;
  621. if (self.layerSketch) {
  622. if (self.layerSketch.graphics.length > 0) {
  623. self.layerSketch.graphics.filter(function(graphic) {
  624. self.sGraphic = graphic;
  625. require([
  626. "esri/geometry/projection",
  627. "esri/geometry/SpatialReference",
  628. "esri/geometry/support/geodesicUtils"
  629. ],
  630. function(projection, SpatialReference, geodesicUtils) {
  631. //定义坐标系
  632. var outSpatialReference = new SpatialReference({
  633. wkid: 4326
  634. });
  635. //默认没有坐标系 需要设置
  636. self.sGraphic.geometry.spatialReference = self.mapView
  637. .spatialReference;
  638. //必须这样执行 只有projection.load()执行完成才能调用坐标转换
  639. projection.load().then(function() {
  640. var polygon = projection.project(self.sGraphic.geometry,
  641. outSpatialReference);
  642. //通过计算面积判断该Polygon是否为反序
  643. var result = geodesicUtils.geodesicAreas([polygon],
  644. "square-meters");
  645. var area = parseFloat(result[0]);
  646. //如果初次计算的面积小于0 说明坐标串是反序的 将其反过来 重新计算
  647. //if (area > 0) {
  648. // polygon.rings[0].reverse();
  649. //}
  650. var resCoords = "";
  651. for (var idx = 0; idx < polygon.rings[0]
  652. .length; idx++) {
  653. var point = polygon.getPoint(0, idx);
  654. if (resCoords == "") {
  655. resCoords = point.x + "," + point.y;
  656. } else {
  657. resCoords += "," + point.x + "," + point.y;
  658. }
  659. }
  660. callback(resCoords);
  661. });
  662. });
  663. });
  664. } else {
  665. //如果没有绘制要素则返回undefined
  666. callback(undefined);
  667. }
  668. } else {
  669. callback(undefined);
  670. }
  671. },
  672. //添加编辑要素
  673. sketchAddGraphic: function(graphic) {
  674. var self = this;
  675. this._sketchViewModel(function() {
  676. require([
  677. "esri/geometry/projection",
  678. "esri/geometry/SpatialReference",
  679. "esri/Graphic"
  680. ],
  681. function(projection, SpatialReference, Graphic) {
  682. //定义坐标系
  683. var outSpatialReference = new SpatialReference({
  684. wkid: 4326
  685. });
  686. graphic.spatialReference = outSpatialReference;
  687. //由于选择的图斑是WGS84坐标系下的成果 需要改正
  688. projection.load().then(function() {
  689. self.layerSketch.removeAll();
  690. var polygon = projection.project(graphic.geometry, self.mapView
  691. .spatialReference);
  692. var resGraphic = new Graphic({
  693. geometry: polygon,
  694. symbol: self.sketchViewModel.polygonSymbol,
  695. });
  696. self.layerSketch.add(resGraphic);
  697. });
  698. });
  699. });
  700. },
  701. });
  702. /**+++++++++++++++绘制相关+++++++++++++++++++++++*/
  703. JTMapKit.prototype.extend({
  704. _draw: function(type, option) {
  705. var self = this;
  706. self.isDrawing = true;
  707. require([
  708. "esri/views/draw/Draw",
  709. "esri/layers/GraphicsLayer",
  710. "esri/Graphic"
  711. ],
  712. function(Draw, GraphicsLayer, Graphic) {
  713. self.clearGraphic();
  714. if (!self.graphicsLayer) {
  715. self.graphicsLayer = new GraphicsLayer();
  716. self.map.layers.add(self.graphicsLayer);
  717. }
  718. var draw = new Draw({
  719. view: self.mapView
  720. });
  721. var action = draw.create(type);
  722. option.type = type;
  723. //添加点
  724. action.on("vertex-add", function(evt) {
  725. self._drawActionHandler(evt, option, Graphic);
  726. });
  727. //绘制结束
  728. action.on("draw-complete", function(evt) {
  729. self.isDrawing = false;
  730. self._drawActionHandler(evt, option, Graphic);
  731. });
  732. //删除点
  733. action.on("vertex-remove", function(evt) {
  734. self._drawActionHandler(evt, option, Graphic);
  735. });
  736. action.on("cursor-update", function(evt) {
  737. if (type === 'circle') {
  738. self._drawActionHandler(evt, option, Graphic);
  739. } else if (type === 'rectangle') {
  740. self._drawActionHandler(evt, option, Graphic);
  741. }
  742. })
  743. if (type === 'multipoint') {
  744. self.mapView.on('click', function(evt) {
  745. self._addMultipoint(evt, option, Graphic)
  746. })
  747. }
  748. });
  749. },
  750. //绘制处理事件
  751. _drawActionHandler(evt, option, Graphic) {
  752. if (option.type === 'circle') {
  753. this._drawCircleActionHandler(evt, option, Graphic);
  754. return;
  755. } else if (option.type === 'rectangle') {
  756. this._drawRectangleActionHandler(evt, option, Graphic);
  757. return;
  758. }
  759. var geometry;
  760. if (evt.coordinates) { //绘制单个点时获取的是coordinates
  761. var coordinates = evt.coordinates;
  762. geometry = {
  763. type: "point",
  764. x: coordinates[0],
  765. y: coordinates[1],
  766. spatialReference: this.mapView.spatialReference
  767. };
  768. } else if (evt.vertices) {
  769. var vertices = evt.vertices;
  770. var type = option.type;
  771. geometry = {
  772. spatialReference: this.mapView.spatialReference
  773. };
  774. //多点
  775. if (type === 'multipoint') {
  776. this.isDrawing = false;
  777. geometry.points = vertices;
  778. geometry.type = "multipoint";
  779. } else if (type === 'polyline') {
  780. geometry.paths = vertices;
  781. geometry.type = "polyline";
  782. } else {
  783. geometry.rings = vertices;
  784. geometry.type = "polygon";
  785. }
  786. }
  787. var graphic = new Graphic({
  788. geometry: geometry,
  789. symbol: option.symbol
  790. });
  791. this.graphicsLayer.removeAll();
  792. this.graphicsLayer.add(graphic);
  793. //将绘制的要素通过回调方法回调回去
  794. if (this.onDrawEventComplete && !this.isDrawing) {
  795. //将绘制的图元要素返回
  796. this.onDrawEventComplete(geometry);
  797. }
  798. },
  799. //绘制圆
  800. _drawCircleActionHandler(evt, option, Graphic) {
  801. var self = this;
  802. require(["esri/geometry/Circle",
  803. "esri/geometry/Point"
  804. ], function(Circle, Point) {
  805. var vertices = evt.vertices;
  806. if (vertices.length < 2) {
  807. return
  808. }
  809. self.graphicsLayer.removeAll();
  810. var center = new Point({
  811. hasZ: false,
  812. hasM: false,
  813. x: vertices[0][0],
  814. y: vertices[0][1],
  815. spatialReference: self.mapView.spatialReference
  816. });
  817. var radius = center.distance(new Point({
  818. hasZ: false,
  819. hasM: false,
  820. x: vertices[1][0],
  821. y: vertices[1][1],
  822. spatialReference: self.mapView.spatialReference
  823. }));
  824. var graphic = new Graphic({
  825. geometry: new Circle({
  826. hasZ: false,
  827. hasM: false,
  828. center: center,
  829. radius: radius,
  830. spatialReference: self.mapView.spatialReference
  831. }),
  832. symbol: option.symbol
  833. });
  834. self.graphicsLayer.add(graphic);
  835. });
  836. },
  837. //绘制矩形
  838. _drawRectangleActionHandler(evt, option, Graphic) {
  839. var self = this;
  840. require(["esri/geometry/Circle",
  841. "esri/geometry/Polygon"
  842. ], function(Circle, Polygon) {
  843. //获取所有顶点
  844. var vertices = evt.vertices;
  845. if (vertices.length < 2) {
  846. return
  847. }
  848. var rings = [vertices[0],
  849. [vertices[0][0], vertices[1][1]], vertices[1],
  850. [vertices[1][0], vertices[0][1]]
  851. ];
  852. self.graphicsLayer.removeAll();
  853. var graphic = new Graphic({
  854. geometry: new Polygon({
  855. hasZ: false,
  856. hasM: false,
  857. rings: [rings],
  858. spatialReference: self.mapView.spatialReference
  859. }),
  860. symbol: option.symbol
  861. });
  862. self.graphicsLayer.add(graphic);
  863. });
  864. },
  865. //绘制多个点
  866. _addMultipoint: function(evt, option, Graphic) {
  867. if (this.isDrawing) {
  868. var graphic = new Graphic({
  869. geometry: evt.mapPoint,
  870. symbol: option.symbol
  871. });
  872. this.graphicsLayer.add(graphic);
  873. }
  874. },
  875. //绘制单个点
  876. drawPoint: function(option) {
  877. var options = option || {};
  878. if (!options.symbol) {
  879. options.symbol = this.pointSymbol;
  880. }
  881. this._draw('point', options);
  882. },
  883. //绘制多个点
  884. drawMultiPoint: function(option) {
  885. this.isDrawing = true;
  886. var options = option || {};
  887. if (!options.symbol) {
  888. options.symbol = this.pointSymbol;
  889. }
  890. this._draw('multipoint', options);
  891. },
  892. //绘制线
  893. drawPolyline: function(option) {
  894. var options = option || {};
  895. if (!options.symbol) {
  896. options.symbol = this.lineSymbol;
  897. }
  898. this._draw('polyline', options);
  899. },
  900. //绘制多边形
  901. drawPolygon: function(option) {
  902. var options = option || {};
  903. if (!options.symbol) {
  904. options.symbol = this.fillSymbol;
  905. }
  906. this._draw('polygon', options);
  907. },
  908. //绘制矩形
  909. drawRectangle: function(option) {
  910. var options = option || {};
  911. if (!options.symbol) {
  912. options.symbol = this.fillSymbol;
  913. }
  914. this._draw('rectangle', options);
  915. },
  916. //绘制圆
  917. drawCircle: function(option) {
  918. var options = option || {};
  919. if (!options.symbol) {
  920. options.symbol = this.fillSymbol;
  921. }
  922. this._draw('circle', options);
  923. },
  924. //清空绘制图层
  925. clearGraphic: function() {
  926. if (this.graphicsLayer) {
  927. this.graphicsLayer.removeAll();
  928. }
  929. this.queryTaskGraphic = undefined; //初始化查询绘制要素
  930. this.measureGraphic = undefined; //初始化测量绘制要素
  931. if (this.draw) {
  932. this.draw.reset(); //清除全部动作
  933. this.draw = undefined;
  934. }
  935. this.isDrawing = false; //重置测量标志
  936. this.mapView.popup.visible = false; //关闭气泡窗口
  937. }
  938. })
  939. /**+++++++++++++++查询相关+++++++++++++++++++++++*/
  940. JTMapKit.prototype.extend({
  941. _queryTack: function(type, option) {
  942. var self = this;
  943. self.clearGraphic();
  944. self.isDrawing = true;
  945. require([
  946. "esri/views/draw/Draw",
  947. "esri/layers/GraphicsLayer",
  948. "esri/Graphic"
  949. ],
  950. function(Draw, GraphicsLayer, Graphic) {
  951. if (!self.graphicsLayer) {
  952. self.graphicsLayer = new GraphicsLayer();
  953. self.map.layers.add(self.graphicsLayer);
  954. }
  955. if (!self.draw) {
  956. self.draw = draw = new Draw({
  957. view: self.mapView,
  958. });
  959. } else {
  960. self.draw.reset();
  961. }
  962. var action = draw.create(type);
  963. action.mode = "click"; //只有点击绘制
  964. option.type = type;
  965. //添加点
  966. action.on("vertex-add", function(evt) {
  967. self._queryTackActionHandler(evt, option, Graphic);
  968. });
  969. //绘制结束
  970. action.on("draw-complete", function(evt) {
  971. self.isDrawing = false;
  972. self._queryTackActionHandler(evt, option, Graphic);
  973. });
  974. action.on("cursor-update", function(evt) {
  975. if (type === 'rectangle') {
  976. self._queryTackRectangleActionHandler(evt, option, Graphic);
  977. }
  978. })
  979. });
  980. },
  981. //查询绘制处理事件
  982. _queryTackActionHandler(evt, option, Graphic) {
  983. var self = this;
  984. //矩形查询绘制事件
  985. if (option.type === 'rectangle') {
  986. this._queryTackRectangleActionHandler(evt, option, Graphic);
  987. return;
  988. }
  989. var geometry, clickPoint;
  990. if (evt.coordinates) { //绘制单个点时获取的是coordinates
  991. var coordinates = evt.coordinates;
  992. geometry = {
  993. type: "point",
  994. x: coordinates[0],
  995. y: coordinates[1],
  996. spatialReference: this.mapView.spatialReference
  997. };
  998. } else if (evt.vertices) {
  999. var vertices = evt.vertices;
  1000. //创建点击点 以便实现效果
  1001. clickPoint = {
  1002. type: "point",
  1003. x: vertices[vertices.length - 1][0],
  1004. y: vertices[vertices.length - 1][1],
  1005. spatialReference: this.mapView.spatialReference
  1006. };
  1007. var type = option.type;
  1008. geometry = {
  1009. spatialReference: this.mapView.spatialReference
  1010. };
  1011. if (type === 'polyline') {
  1012. geometry.paths = vertices;
  1013. geometry.type = "polyline";
  1014. } else {
  1015. geometry.rings = vertices;
  1016. geometry.type = "polygon";
  1017. }
  1018. }
  1019. //如果不存在则创建 否则更新
  1020. if (!this.queryTaskGraphic) {
  1021. this.queryTaskGraphic = new Graphic({
  1022. geometry: geometry,
  1023. symbol: option.symbol
  1024. });
  1025. this.graphicsLayer.add(this.queryTaskGraphic);
  1026. } else {
  1027. this.queryTaskGraphic.geometry = geometry;
  1028. }
  1029. //追加点击点
  1030. var graphicPoint = new Graphic({
  1031. geometry: clickPoint,
  1032. symbol: this.pointSymbol
  1033. });
  1034. this.graphicsLayer.add(graphicPoint);
  1035. var mGeometry = this.queryTaskGraphic.geometry; //获取绘制的要素
  1036. /* 判断查询方式 */
  1037. if (option.type === 'point') {
  1038. self._queryTaskFromGeometry(self.queryTaskGraphic.geometry);
  1039. self.queryTaskGraphic = undefined;
  1040. }else if(option.type === 'polygon'){
  1041. //绘制的是查询面 且点数大于等于3
  1042. if (mGeometry.type == 'polygon' && mGeometry.rings[0].length >= 3) {
  1043. this._showQueryResultPopup(clickPoint, function() {
  1044. self.mapView.popup.visible = false;
  1045. self._queryTaskFromGeometry(self.queryTaskGraphic.geometry);
  1046. self.queryTaskGraphic = undefined;
  1047. // console.log($('.esri-popup__main-container').parent().html());
  1048. });
  1049. }
  1050. }
  1051. },
  1052. /**
  1053. * 显示测量结果
  1054. * @param {ESRIPoint} location 测量结果内容
  1055. * @param {function} callback 点击查询回调
  1056. */
  1057. _showQueryResultPopup: function(location, callback) {
  1058. /* 创建查询按钮 */
  1059. var queryButton = document.createElement("button");
  1060. queryButton.className =
  1061. "mui-btn mui-btn-primary mui-icon app-icon app-icon-map-areaquery wc-btn-child wc-btn-query";
  1062. var btnLabel = document.createTextNode("查询案件");
  1063. queryButton.appendChild(btnLabel);
  1064. queryButton.onclick = function(event) {
  1065. if (callback) callback();
  1066. }
  1067. /* 显示弹窗试试 */
  1068. var popup = this.mapView.popup;
  1069. /* 显示位置 */
  1070. popup.location = location;
  1071. /* 设置标题 */
  1072. popup.title = "区域案件查询";
  1073. /* 设置显示内容 */
  1074. popup.viewModel.content = queryButton;
  1075. /* 控制窗口的停靠按钮是否显示 */
  1076. popup.dockOptions = {
  1077. buttonEnabled: false,
  1078. }
  1079. /* 控制是否显示关闭按钮 */
  1080. popup.visibleElements = {
  1081. closeButton: true,
  1082. featureNavigation: false
  1083. }
  1084. /* 定义一个action试试 */
  1085. // var queryAction = {
  1086. // title: '查询',
  1087. // id: 'actionQuery',
  1088. // image: "https://developers.arcgis.com/javascript/latest//sample-code/popup-actions/live/Measure_Distance16.png"
  1089. // };
  1090. // popup.actions.push(queryAction);
  1091. popup.on("trigger-action", function(event) {
  1092. console.log("ActionId = " + event.action.id);
  1093. });
  1094. /* 关闭自动缩放按钮 */
  1095. popup.viewModel.includeDefaultActions = false;
  1096. /* 控制是否显示 */
  1097. popup.visible = true;
  1098. },
  1099. //查询绘制矩形
  1100. _queryTackRectangleActionHandler(evt, option, Graphic) {
  1101. var self = this;
  1102. require(["esri/geometry/Circle",
  1103. "esri/geometry/Polygon"
  1104. ], function(Circle, Polygon) {
  1105. //获取所有顶点
  1106. var vertices = evt.vertices;
  1107. if (vertices.length < 2) {
  1108. return
  1109. }
  1110. var rings = [vertices[0],
  1111. [vertices[0][0], vertices[1][1]], vertices[1],
  1112. [vertices[1][0], vertices[0][1]]
  1113. ];
  1114. self.graphicsLayer.removeAll();
  1115. var graphic = new Graphic({
  1116. geometry: new Polygon({
  1117. hasZ: false,
  1118. hasM: false,
  1119. rings: [rings],
  1120. spatialReference: self.mapView.spatialReference
  1121. }),
  1122. symbol: option.symbol
  1123. });
  1124. self.graphicsLayer.removeAll();
  1125. self.graphicsLayer.add(graphic);
  1126. //执行查询
  1127. if (!self.isDrawing) {
  1128. self._queryTaskFromGeometry(graphic.geometry);
  1129. }
  1130. });
  1131. },
  1132. //查询
  1133. _queryTaskFromGeometry: function(geometry) {
  1134. var self = this;
  1135. require([
  1136. "esri/tasks/support/Query",
  1137. "esri/tasks/QueryTask",
  1138. "esri/geometry/projection",
  1139. "esri/geometry/SpatialReference"
  1140. ],
  1141. function(Query, QueryTask, projection, SpatialReference) {
  1142. var qTask = new QueryTask({
  1143. url: self.queryURL
  1144. });
  1145. var params = new Query({
  1146. returnGeometry: true,
  1147. outFields: ["*"],
  1148. distance: 2,
  1149. geometry: geometry,
  1150. spatialRelationship: "intersects",
  1151. where: self.queryFilter,
  1152. });
  1153. //定义坐标系
  1154. var outSpatialReference = new SpatialReference({
  1155. wkid: 4326
  1156. });
  1157. qTask.execute(params).then(function(response) {
  1158. //通过回调返回查询到的图元
  1159. if (self.onQueryTackEvent) {
  1160. self.graphicsLayer.removeAll(); //删除绘制的要素
  1161. //变换投影
  1162. projection.load().then(function() {
  1163. //将最终的要素图形转换为经纬度返回
  1164. response.features.forEach(function(graphic, index) {
  1165. graphic.geometry.spatialReference = self.mapView
  1166. .spatialReference;
  1167. var geo = projection.project(graphic.geometry,
  1168. outSpatialReference);
  1169. graphic.geometry = geo;
  1170. });
  1171. self.onQueryTackEvent(response.features);
  1172. });
  1173. }
  1174. });
  1175. }
  1176. );
  1177. },
  1178. //点击查询
  1179. queryTackPoint: function(option) {
  1180. var options = option || {};
  1181. this.isDrawing = true;
  1182. if (!options.symbol) {
  1183. options.symbol = this.pointSymbol;
  1184. }
  1185. if (option.url) {
  1186. this.queryURL = option.url;
  1187. }
  1188. if (option.filter) {
  1189. this.queryFilter = option.filter;
  1190. }
  1191. this._queryTack('point', options);
  1192. },
  1193. //多边形查询
  1194. queryTackPolygon: function(option) {
  1195. var options = option || {};
  1196. this.isDrawing = true;
  1197. if (!options.symbol) {
  1198. options.symbol = this.fillSymbol;
  1199. }
  1200. if (option.url) {
  1201. this.queryURL = option.url;
  1202. }
  1203. if (option.filter) {
  1204. this.queryFilter = option.filter;
  1205. }
  1206. this._queryTack('polygon', options);
  1207. },
  1208. //矩形查询
  1209. queryTackRectangle: function(option) {
  1210. var options = option || {};
  1211. this.isDrawing = true;
  1212. if (!options.symbol) {
  1213. options.symbol = this.fillSymbol;
  1214. }
  1215. if (option.url) {
  1216. this.queryURL = option.url;
  1217. }
  1218. if (option.filter) {
  1219. this.queryFilter = option.filter;
  1220. }
  1221. this._queryTack('rectangle', options);
  1222. }
  1223. })
  1224. /**+++++++++++++++测量相关+++++++++++++++++++++++*/
  1225. JTMapKit.prototype.extend({
  1226. /**
  1227. * @param {string} type 测量类型 point/polyline/polygon
  1228. * @param {JSON} option 配置项
  1229. */
  1230. _measure: function(type, option) {
  1231. var self = this;
  1232. self.clearGraphic();
  1233. self.isDrawing = true;
  1234. require([
  1235. "esri/views/draw/Draw",
  1236. "esri/layers/GraphicsLayer",
  1237. "esri/Graphic"
  1238. ],
  1239. function(Draw, GraphicsLayer, Graphic) {
  1240. /* 判断绘制图层是否存在 如果不存在 则创建 并加入到地图中 */
  1241. if (!self.graphicsLayer) {
  1242. self.graphicsLayer = new GraphicsLayer();
  1243. self.map.layers.add(self.graphicsLayer);
  1244. }
  1245. /* 判断绘制工具是否已经实例化 如果未实例化则创建 否则重置 */
  1246. if (!self.draw) {
  1247. self.draw = new Draw({
  1248. view: self.mapView
  1249. });
  1250. } else {
  1251. self.draw.reset();
  1252. }
  1253. /* 未绘制工具创建动作 因为reset会清除已添加的动作 */
  1254. action = self.draw.create(type, {
  1255. mode: 'click'
  1256. });
  1257. option.type = type;
  1258. //添加点
  1259. action.on("vertex-add", function(evt) {
  1260. self._measureActionHandler(evt, option, Graphic);
  1261. });
  1262. //绘制结束
  1263. action.on("draw-complete", function(evt) {
  1264. self.isDrawing = false;
  1265. self._measureActionHandler(evt, option, Graphic);
  1266. });
  1267. //删除点
  1268. action.on("vertex-remove", function(evt) {
  1269. self._measureActionHandler(evt, option, Graphic);
  1270. });
  1271. });
  1272. },
  1273. //绘制处理事件
  1274. _measureActionHandler(evt, option, Graphic) {
  1275. var geometry, clickPoint;
  1276. if (evt.coordinates) { //绘制单个点时获取的是coordinates
  1277. var coordinates = evt.coordinates;
  1278. clickPoint = geometry = {
  1279. type: "point",
  1280. x: coordinates[0],
  1281. y: coordinates[1],
  1282. spatialReference: this.mapView.spatialReference
  1283. };
  1284. } else if (evt.vertices) {
  1285. var vertices = evt.vertices;
  1286. var type = option.type;
  1287. //创建点击点 以便实现效果
  1288. clickPoint = {
  1289. type: "point",
  1290. x: vertices[vertices.length - 1][0],
  1291. y: vertices[vertices.length - 1][1],
  1292. spatialReference: this.mapView.spatialReference
  1293. };
  1294. geometry = {
  1295. spatialReference: this.mapView.spatialReference
  1296. };
  1297. if (type === 'polyline') {
  1298. geometry.paths = vertices;
  1299. geometry.type = "polyline";
  1300. } else {
  1301. geometry.rings = vertices;
  1302. geometry.type = "polygon";
  1303. }
  1304. }
  1305. if (!this.measureGraphic) {
  1306. this.measureGraphic = new Graphic({
  1307. geometry: geometry,
  1308. symbol: option.symbol
  1309. });
  1310. this.graphicsLayer.add(this.measureGraphic);
  1311. } else {
  1312. this.measureGraphic.geometry = geometry;
  1313. }
  1314. //追加点击点
  1315. var graphicPoint = new Graphic({
  1316. geometry: clickPoint,
  1317. symbol: this.pointSymbol
  1318. });
  1319. this.graphicsLayer.add(graphicPoint);
  1320. /* 判断是否需要计算并返回结果 */
  1321. var self = this;
  1322. var mGeometry = this.measureGraphic.geometry; //获取绘制的要素
  1323. if (mGeometry.type == "polyline") {
  1324. if (mGeometry.paths[0].length >= 2) {
  1325. this._measureLengthOrAreaOrCoord(function(lastLength, allLength) {
  1326. self._measureAddText(lastLength, clickPoint);
  1327. self._showMeasureResultPopup("长度测量结果", "当前测量总长:" + allLength, clickPoint);
  1328. });
  1329. } else {
  1330. this._measureAddText('起点', clickPoint);
  1331. }
  1332. } else if (mGeometry.type == "polygon") {
  1333. if (mGeometry.rings[0].length >= 3) {
  1334. this._measureLengthOrAreaOrCoord(function(result) {
  1335. self._showMeasureResultPopup("面积测量结果", result, clickPoint);
  1336. });
  1337. } else if (mGeometry.rings[0].length == 1) {
  1338. this._measureAddText('起点', clickPoint);
  1339. }
  1340. } else if (mGeometry.type == "point") {
  1341. this._measureLengthOrAreaOrCoord(function(result) {
  1342. self._showMeasureResultPopup("当前位置坐标", result, clickPoint);
  1343. });
  1344. }
  1345. },
  1346. /**
  1347. * 添加文字
  1348. * @param {string} text
  1349. * @param {EsriPoint} location 文字位置
  1350. */
  1351. _measureAddText: function(text, location) {
  1352. var self = this;
  1353. require(["esri/Graphic"], function(Graphic) {
  1354. /* 创建文字符号 */
  1355. // var textSymbol = {
  1356. // type: 'text',
  1357. // color: "#FFFFFF",
  1358. // haloColor: "#555555",
  1359. // haloSize: "2px",
  1360. // text: text,
  1361. // xoffset: 0,
  1362. // yoffset: -20,
  1363. // font: {
  1364. // size: 10,
  1365. // family: "Playfair Display",
  1366. // weight: "bold"
  1367. // }
  1368. // };
  1369. self.textSymbol.text = text;
  1370. /* 创建文字要素 */
  1371. var txtGraphic = new Graphic({
  1372. geometry: location,
  1373. symbol: self.textSymbol
  1374. });
  1375. /* 将文字要素添加到绘制图层 */
  1376. self.graphicsLayer.add(txtGraphic);
  1377. });
  1378. },
  1379. /**
  1380. * 转换线的长度
  1381. * @param {float} lineLength 线的长度
  1382. */
  1383. _measureTrasformLineLength: function(lineLength) {
  1384. var len = parseFloat(lineLength);
  1385. var lenResult = "";
  1386. if (len < 1000) {
  1387. lenResult = len.toFixed(2) + "米";
  1388. } else {
  1389. lenResult = (len / 1000).toFixed(2) + "公里";
  1390. }
  1391. return lenResult;
  1392. },
  1393. /**
  1394. * 根据绘制图层绘制的要素类型 计算坐标、长度或者面积
  1395. * @param {function} callback 计算完成回调
  1396. */
  1397. _measureLengthOrAreaOrCoord: function(callback) {
  1398. var self = this;
  1399. require([
  1400. "esri/geometry/support/geodesicUtils",
  1401. "esri/geometry/projection",
  1402. "esri/geometry/SpatialReference",
  1403. "esri/geometry/geometryEngine",
  1404. "esri/geometry/geometryEngineAsync"
  1405. ],
  1406. function(geodesicUtils, projection, SpatialReference, geometryEngine, geometryEngineAsync) {
  1407. //设置要素的投影
  1408. self.measureGraphic.geometry.spatialReference = self.mapView.spatialReference;
  1409. //定义坐标系
  1410. var outSpatialReference = new SpatialReference({
  1411. wkid: 4326
  1412. });
  1413. var mGeometry = self.measureGraphic.geometry;
  1414. if (mGeometry.type == "polyline") {
  1415. //计算最后一段长度
  1416. var pt1 = mGeometry.getPoint(0, mGeometry.paths[0].length - 1);
  1417. var pt2 = mGeometry.getPoint(0, mGeometry.paths[0].length - 2);
  1418. //创建要素
  1419. var lastGeometry = {
  1420. spatialReference: self.mapView.spatialReference,
  1421. type: "polyline",
  1422. paths: [
  1423. [
  1424. [pt1.x, pt1.y],
  1425. [pt2.x, pt2.y]
  1426. ]
  1427. ],
  1428. };
  1429. lastGeometry.spatialReference = self.mapView.spatialReference;
  1430. //开始异步计算长度
  1431. geometryEngineAsync.geodesicLength(lastGeometry, "meters").then(function(
  1432. lastResult) {
  1433. geometryEngineAsync.geodesicLength(mGeometry, "meters").then(function(
  1434. result) {
  1435. var lenLastResult = self._measureTrasformLineLength(
  1436. lastResult);
  1437. var lenAllResult = self._measureTrasformLineLength(result);
  1438. if (callback != undefined) callback(lenLastResult,
  1439. lenAllResult);
  1440. })
  1441. });
  1442. } else if (mGeometry.type == "polygon") {
  1443. var result = geometryEngine.geodesicArea(mGeometry, "square-meters");
  1444. var area = Math.abs(parseFloat(result));
  1445. //如果初次计算的面积小于0 说明坐标串是反序的 将其反过来 重新计算
  1446. if (area < 0) {
  1447. // mGeometry.rings[0].reverse();
  1448. // alert(mGeometry.rings[0]);
  1449. // result = geometryEngine.geodesicArea(mGeometry, "square-meters");
  1450. // area = parseFloat(result[0]);
  1451. }
  1452. var areaMu = area * 0.0015;
  1453. var areaResult = "";
  1454. if (area < 1000000) {
  1455. areaResult = area.toFixed(3) + "平方米 " + areaMu.toFixed(3) +
  1456. "亩";
  1457. } else {
  1458. areaResult = (area / 1000000).toFixed(3) + "平方公里 " + areaMu
  1459. .toFixed(3) + "亩";
  1460. }
  1461. if (callback != undefined) callback(areaResult);
  1462. } else if (mGeometry.type == "point") {
  1463. //对坐标进行转换
  1464. projection.load().then(function() {
  1465. var geometry = projection.project(mGeometry, outSpatialReference);
  1466. var coordResult = "经度:" + geometry.x.toFixed(6) + "<br>纬度:" + geometry.y
  1467. .toFixed(6);
  1468. if (callback != undefined) callback(coordResult);
  1469. });
  1470. }
  1471. }
  1472. );
  1473. },
  1474. /**
  1475. * 显示测量结果
  1476. * @param {string} title 标题
  1477. * @param {string} content 测量结果内容
  1478. * @param {ESRIPoint} location 测量结果内容
  1479. */
  1480. _showMeasureResultPopup: function(title, content, location) {
  1481. /* 显示弹窗试试 */
  1482. var popup = this.mapView.popup;
  1483. /* 显示位置 */
  1484. popup.location = location;
  1485. /* 设置标题 */
  1486. popup.title = title;
  1487. /* 设置显示内容 */
  1488. popup.viewModel.content = content;
  1489. /* 控制窗口的停靠按钮是否显示 */
  1490. popup.dockOptions = {
  1491. buttonEnabled: false,
  1492. }
  1493. /* 控制是否显示关闭按钮 */
  1494. popup.visibleElements = {
  1495. closeButton: true,
  1496. featureNavigation: false
  1497. }
  1498. /* 关闭自动缩放按钮 */
  1499. popup.viewModel.includeDefaultActions = false;
  1500. /* 控制是否显示 */
  1501. popup.visible = true;
  1502. },
  1503. //测量长度
  1504. measureLength: function(option) {
  1505. var options = option || {};
  1506. if (!options.symbol) {
  1507. options.symbol = this.lineSymbol;
  1508. }
  1509. this._measure('polyline', options);
  1510. },
  1511. //测量面积
  1512. measureArea: function(option) {
  1513. var options = option || {};
  1514. if (!options.symbol) {
  1515. options.symbol = this.fillSymbol;
  1516. }
  1517. this._measure('polygon', options);
  1518. },
  1519. //测量坐标
  1520. measureCoordinate: function(option) {
  1521. var options = option || {};
  1522. if (!options.symbol) {
  1523. options.symbol = this.pointSymbol;
  1524. }
  1525. this._measure('point', options);
  1526. }
  1527. })
  1528. /**+++++++++++++++图层相关+++++++++++++++++++++++*/
  1529. JTMapKit.prototype.extend({
  1530. /**
  1531. * 根据图层名称设置其可见性
  1532. * @param {any} layerName 图层名称
  1533. * @param {any} isVisible 是否可见
  1534. */
  1535. _setLayerIsVisibleByName: function(layerName, isVisible) {
  1536. this.map.layers.forEach(function(layer, idx) {
  1537. if (layer.title.toLocaleLowerCase() == layerName.toLocaleLowerCase()) {
  1538. layer.visible = isVisible;
  1539. return idx;
  1540. }
  1541. });
  1542. },
  1543. /** 获取地图中的所有图层 */
  1544. getLayers: function() {
  1545. var self = this;
  1546. if (self.loadLayers) {
  1547. return self.loadLayers;
  1548. }
  1549. },
  1550. /**
  1551. * 根据图层ID查找图层
  1552. * @param {any} layerID
  1553. */
  1554. findLayer: function(layerID) {
  1555. var self = this;
  1556. if (self.map) {
  1557. return self.map.findLayerById(layerID)
  1558. }
  1559. return undefined;
  1560. },
  1561. /**
  1562. * 删除指定的图层
  1563. * @param {any} layer 图层集合中的图层
  1564. */
  1565. removeLayer: function(layer) {
  1566. var self = this;
  1567. if (self.map) {
  1568. return self.map.remove(layer);
  1569. }
  1570. return undefined;
  1571. },
  1572. /**
  1573. * 设置图层可见及不可见
  1574. * @param {any} visibleLayerNames
  1575. * @param {any} noVisibleLayerNames
  1576. */
  1577. setVisibleByNames: function(visibleLayerNames, noVisibleLayerNames) {
  1578. var self = this;
  1579. for (var i = 0; i < self.loadLayers.length; i++) {
  1580. var layer = self.loadLayers[i];
  1581. if (visibleLayerNames.indexOf(layer.title) != -1) {
  1582. layer.slayer.findSublayerById(layer.id).visible = true;
  1583. }
  1584. if (noVisibleLayerNames.indexOf(layer.title) != -1) {
  1585. layer.slayer.findSublayerById(layer.id).visible = false;
  1586. }
  1587. }
  1588. },
  1589. /** 设置单个图层可见或隐藏
  1590. * @param {Object} layerName
  1591. * @param {Object} visible
  1592. */
  1593. setVisibleByName: function(layerName, visible) {
  1594. var self = this;
  1595. for (var i = 0; i < self.loadLayers.length; i++) {
  1596. var layer = self.loadLayers[i];
  1597. if (layer.title == layerName) {
  1598. layer.slayer.findSublayerById(layer.id).visible = visible;
  1599. }
  1600. }
  1601. },
  1602. });
  1603. /**++++++++++++其他相关内容++++++++++++++++++++++*/
  1604. JTMapKit.prototype.extend({
  1605. /** 删除临时绘制的所有内容 */
  1606. DeleteTempDraw: function() {
  1607. if (this.layerSketch) {
  1608. this.layerSketch.removeAll();
  1609. }
  1610. this.clearGraphic();
  1611. },
  1612. /**
  1613. * 地图输出
  1614. * @param {any} title 标题
  1615. * @param {any} layout 模式 A3横式 A3竖式 A4横式 A4竖式
  1616. * @param {any} restype 返回样式
  1617. */
  1618. PrintMap: function(title, layout, restype) {
  1619. var self = this;
  1620. var scale = self.mapView.scale > 1000000 ? 3000 : self.mapView.scale;
  1621. var sLayout = layout;
  1622. if (layout == "A3横向") {
  1623. sLayout = "a3-landscape";
  1624. } else if (layout == "A3竖向") {
  1625. sLayout = "a3-portrait";
  1626. } else if (layout == "A4横向") {
  1627. sLayout = "a4-landscape";
  1628. } else if (layout == "A4竖向") {
  1629. sLayout = "a4-portrait";
  1630. }
  1631. require([
  1632. "esri/tasks/PrintTask",
  1633. "esri/tasks/support/PrintParameters",
  1634. "esri/tasks/support/PrintTemplate"
  1635. ],
  1636. function(PrintTask, PrintParameters, PrintTemplate) {
  1637. //此方法是为了关闭标注层 否则打印的地图将出现黑色
  1638. self._setLayerIsVisibleByName("天地图标注", false);
  1639. var printTask = new PrintTask({
  1640. url: "http://218.59.194.74:6080/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task",
  1641. });
  1642. //打印模板
  1643. var template = new PrintTemplate({
  1644. format: restype,
  1645. exportOptions: {
  1646. dpi: 100,
  1647. width: self.mapView.width,
  1648. height: self.mapView.height
  1649. },
  1650. layout: sLayout,
  1651. layoutOptions: {
  1652. titleText: title,
  1653. authorText: ""
  1654. },
  1655. outScale: scale,
  1656. });
  1657. //打印参数
  1658. var params = new PrintParameters({
  1659. view: self.mapView,
  1660. template: template
  1661. });
  1662. if (self.onPrintEventProgress) {
  1663. self.onPrintEventProgress(false);
  1664. }
  1665. //执行输出地图
  1666. printTask.execute(params).then(function(result) {
  1667. self._setLayerIsVisibleByName("天地图标注", true);
  1668. if (self.onPrintEventProgress) self.onPrintEventProgress(true);
  1669. if (self.onPrintEventComplete) {
  1670. self.onPrintEventComplete(result.url);
  1671. }
  1672. }, function(error) {
  1673. self._setLayerIsVisibleByName("天地图标注", true);
  1674. if (self.onPrintEventProgress) self.onPrintEventProgress(true);
  1675. if (self.onEventError) {
  1676. self.onEventError(error);
  1677. }
  1678. });
  1679. }
  1680. );
  1681. },
  1682. /**
  1683. * 向地图中添加点
  1684. * @param {any} x
  1685. * @param {any} y
  1686. */
  1687. AddPointToMap: function(x, y) {
  1688. var self = this;
  1689. require([
  1690. "esri/symbols/PictureMarkerSymbol",
  1691. "esri/geometry/Point",
  1692. "esri/geometry/SpatialReference",
  1693. "esri/layers/GraphicsLayer",
  1694. "esri/Graphic",
  1695. "esri/geometry/geometryEngine"
  1696. ],
  1697. function(PictureMarkerSymbol, Point, SpatialReference, GraphicsLayer, Graphic,
  1698. geometryEngine) {
  1699. if (!self.graphicsLayer) {
  1700. self.graphicsLayer = new GraphicsLayer();
  1701. self.map.layers.add(self.graphicsLayer);
  1702. }
  1703. //定义坐标系统
  1704. var spa = new SpatialReference({
  1705. wkid: 4326,
  1706. });
  1707. //定义符号
  1708. var ptSymbol = new PictureMarkerSymbol({
  1709. url: self.serverUrl + "location02.png",
  1710. width: "20px",
  1711. height: "20px",
  1712. xoffset: 0,
  1713. yoffset: 7,
  1714. });
  1715. //定义点
  1716. var pt = new Point({
  1717. x: x,
  1718. y: y,
  1719. spatialReference: spa,
  1720. });
  1721. //定义要素
  1722. var graphicPt = new Graphic({
  1723. geometry: pt,
  1724. symbol: ptSymbol,
  1725. });
  1726. //添加要素
  1727. self.graphicsLayer.add(graphicPt);
  1728. //超出范围则重新定位地图中心
  1729. if (!geometryEngine.intersects(pt, self.mapView.extent)) {
  1730. self.mapView.center = [x, y];
  1731. }
  1732. }
  1733. );
  1734. },
  1735. /**
  1736. * 将点数组追加到地图中
  1737. * @param {any} points
  1738. */
  1739. AddPointsToMap: function(points) {
  1740. var self = this;
  1741. require([
  1742. "esri/symbols/SimpleLineSymbol",
  1743. "esri/symbols/SimpleMarkerSymbol",
  1744. "esri/Color",
  1745. "esri/geometry/Polyline",
  1746. "esri/geometry/Point",
  1747. "esri/geometry/SpatialReference",
  1748. "esri/layers/GraphicsLayer",
  1749. "esri/Graphic"
  1750. ],
  1751. function(SimpleLineSymbol, SimpleMarkerSymbol, Color, Polyline, Point, SpatialReference,
  1752. GraphicsLayer, Graphic) {
  1753. if (!self.graphicsLayer) {
  1754. self.graphicsLayer = new GraphicsLayer();
  1755. self.map.layers.add(self.graphicsLayer);
  1756. }
  1757. //定义坐标系统
  1758. var spa = new SpatialReference({
  1759. wkid: 4326,
  1760. });
  1761. //定义颜色
  1762. var r = Math.floor(Math.random() * 200);;
  1763. var g = Math.floor(Math.random() * 200);
  1764. var b = Math.floor(Math.random() * 200);
  1765. var rgb = [r, g, b];
  1766. var ptColor = new Color([34, 139, 34, 1]);
  1767. var lineColorBack = new Color([255, 255, 255]);
  1768. var lineColor = new Color(rgb);
  1769. //定义点样式
  1770. var ptSymbol = new SimpleMarkerSymbol({
  1771. style: "circle",
  1772. color: ptColor,
  1773. size: "8px", // pixels
  1774. });
  1775. //定义线样式
  1776. var lineSymbolBack = new SimpleLineSymbol({
  1777. color: lineColorBack,
  1778. width: "5px",
  1779. style: "solid"
  1780. });
  1781. //定义线样式
  1782. var lineSymbol = new SimpleLineSymbol({
  1783. color: lineColor,
  1784. width: "2px",
  1785. style: "solid"
  1786. });
  1787. //加载数据
  1788. var paths = [];
  1789. var pointStart, pointEnd;
  1790. for (var i = 0; i < points.length; i++) {
  1791. if (i == 0) {
  1792. pointStart = new Point({
  1793. x: points[i].x,
  1794. y: points[i].y,
  1795. spatialReference: spa,
  1796. });
  1797. }
  1798. if (i == points.length - 1) {
  1799. pointEnd = new Point({
  1800. x: points[i].x,
  1801. y: points[i].y,
  1802. spatialReference: spa,
  1803. });
  1804. }
  1805. paths.push([points[i].x, points[i].y]);
  1806. }
  1807. //创建线
  1808. var lineBack = new Polyline({
  1809. paths: paths,
  1810. spatialReference: spa,
  1811. });
  1812. var line = new Polyline({
  1813. paths: paths,
  1814. spatialReference: spa,
  1815. });
  1816. //创建要素
  1817. var graphicBack = new Graphic({
  1818. geometry: lineBack,
  1819. symbol: lineSymbolBack,
  1820. });
  1821. var graphic = new Graphic({
  1822. geometry: line,
  1823. symbol: lineSymbol,
  1824. });
  1825. var graphicStr = new Graphic({
  1826. geometry: pointStart,
  1827. symbol: ptSymbol,
  1828. });
  1829. var graphicEnd = new Graphic({
  1830. geometry: pointEnd,
  1831. symbol: ptSymbol,
  1832. });
  1833. //添加要素
  1834. self.graphicsLayer.add(graphicBack);
  1835. self.graphicsLayer.add(graphic);
  1836. self.graphicsLayer.add(graphicStr);
  1837. self.graphicsLayer.add(graphicEnd);
  1838. //定位范围
  1839. self.mapView.extent = line.extent;
  1840. }
  1841. );
  1842. }
  1843. });