JTMapKit.js 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755
  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: "10px",
  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: "10px",
  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: "10px",
  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: [255, 255, 255],
  63. width: 2,
  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: [255, 255, 255],
  74. width: 2
  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: 10,
  86. font: {
  87. size: 10,
  88. family: "Playfair Display",
  89. weight: "bold"
  90. }
  91. };
  92. /* 草图编辑符号 点符号样式 */
  93. this.sketchPointSymbol = {
  94. type: "simple-marker",
  95. style: "circle",
  96. color: [128, 128, 128],
  97. size: "10px",
  98. outline: {
  99. color: [255, 255, 0],
  100. width: 1
  101. }
  102. }
  103. /* 草图编辑符号 面符号样式 */
  104. this.sketchPolygonSymbol = {
  105. type: "simple-fill",
  106. color: [4, 90, 141, 0.3],
  107. style: "solid",
  108. outline: {
  109. type: "simple-line",
  110. color: [255, 255, 255],
  111. width: 1,
  112. style: 'short-dash-dot',
  113. cap: "round",
  114. join: "round"
  115. }
  116. }
  117. /* 草图编辑符号 现符号样式 */
  118. this.sketchLineSymbol = {
  119. type: "simple-line",
  120. color: [255, 255, 255],
  121. width: 1,
  122. style: 'short-dash-dot',
  123. cap: "round",
  124. join: "round"
  125. }
  126. /* GPS图片符号 */
  127. this.gpsSysmbol = {
  128. type: "picture-marker",
  129. url: this.serverUrl + 'icon_gps_cn.png',
  130. width: "40px",
  131. height: "40px",
  132. angle: 0,
  133. }
  134. this.isDrawing = false; //正在绘制中,用于控制多点绘制
  135. /* 操作类型 */
  136. this.actionType = {
  137. unknown: 'unknown', //未知
  138. LandAnalysis: 'LandAnalysis', //地类分析
  139. CaseAnalysis: 'CaseAnalysis', //案件占压分析
  140. ForestAnalysis: 'ForestAnalysis', //林地占压分析
  141. DiggingsAnalysis: 'DiggingsAnalysis', //矿区治理区占地分析
  142. LayerQueryPoint: 'LayerQueryPoint', //点击查询图层数据
  143. LayerQueryPolygon: 'LayerQueryPolygon', //区域查询图层数据
  144. CommonSpatialAnalysis: 'CommonSpatialAnalysis', //通用空间占地分析
  145. };
  146. this.currentActionType = this.actionType.unknown; //当前操作类型
  147. }
  148. /**
  149. * 原型中添加的方法
  150. * @param {any} option
  151. */
  152. JTMapKit.prototype.extend = function(option) {
  153. for (key in option) {
  154. JTMapKit.prototype[key] = option[key];
  155. }
  156. }
  157. /**+++++++++++++++初始化相关内容+++++++++++++++++*/
  158. JTMapKit.prototype.extend({
  159. /**
  160. * 默认执行的初始化信息
  161. * @param {any} option
  162. */
  163. _init: function(option) {
  164. this.selector = option.selector || 'map'; //地图选择器 对应的DIV控件
  165. this.serverUrl = option.serverUrl;
  166. this.onMapReady = option.onMapReady; //地图加载完后处理
  167. this.onViewReady = option.onViewReady; //mapView加载完成后添加底图
  168. this.onViewEventPointMove = option.onViewEventPointMove; //地图鼠标移动事件
  169. this.onQueryTackEvent = option.onQueryTackEvent; //查询结果监听事件
  170. this.onViewEventChange = option.onViewEventChange; //视图变化监听事件
  171. this.onPrintEventProgress = option.onPrintEventProgress; //地图正在输出中
  172. this.onPrintEventComplete = option.onPrintEventComplete; //地图输出完成事件
  173. this.onEventError = option.onEventError; //错误事件接收
  174. this.onCaseQueryPolygonBegin = option.onCaseQueryPolygonBegin; //区域查询方式查询案件开始监听
  175. this.mapType = option.mapType || '2d'; //地图类型,2d,3d
  176. //小组件
  177. this.uiZoom = null; //放大缩小组件
  178. this.uiScaleBar = null; //比例尺组件
  179. //地图相关参数
  180. this.mapOption.basemap = option.basemap; //底图
  181. this.mapOption.center = option.center; //中心点
  182. this.mapOption.extent = option.extent; //全图
  183. this.subDomains = ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"]; //天地图的URL服务器
  184. //gov.cn
  185. this.baseMapVectorURL =
  186. "http://{subDomain}.tianditu.com/DataServer?T=vec_w&x={col}&y={row}&l={level}&tk=944815e0a333f538646868d896ef7378";
  187. this.baseMapVectorLabelURL =
  188. "http://{subDomain}.tianditu.com/DataServer?T=cva_w&x={col}&y={row}&l={level}&tk=944815e0a333f538646868d896ef7378";
  189. this.baseMapRasterURL =
  190. "http://{subDomain}.tianditu.com/DataServer?T=img_w&x={col}&y={row}&l={level}&tk=944815e0a333f538646868d896ef7378";
  191. this.baseMapRasterLabelURL =
  192. "http://{subDomain}.tianditu.com/DataServer?T=cia_w&x={col}&y={row}&l={level}&tk=944815e0a333f538646868d896ef7378";
  193. //绘图相关
  194. this.draw = undefined;
  195. if (this.mapType.toLocaleLowerCase() === '3d') {
  196. } else {
  197. this._init2dMap(option);
  198. }
  199. },
  200. /**
  201. * 创建tms格式的底图
  202. * @param {String} baseMapUrl 底图服务地址
  203. * @param {Function} callComplete 完成回调callComplete(baseLayer)
  204. */
  205. _initBaseMapTMSLayer: function(baseMapUrl, callComplete) {
  206. if (!baseMapUrl && callComplete) callComplete(undefined);
  207. require(["esri/layers/BaseTileLayer"], function(BaseTileLayer) {
  208. /* 创建TMSLayer */
  209. let TMSLayer = BaseTileLayer.createSubclass({
  210. properties: {
  211. urlTemplate: null
  212. },
  213. /* 获取切片地址 */
  214. getTileUrl: function(z, y, x) {
  215. /* 这里对y轴瓦片编号进行转置 */
  216. let y2 = Math.pow(2, z) - y - 1;
  217. return this.urlTemplate
  218. .replace("{z}", z)
  219. .replace("{x}", x)
  220. .replace("{y}", y2);
  221. },
  222. });
  223. /* 创建图层 */
  224. let tmsLayer = new TMSLayer({
  225. urlTemplate: baseMapUrl,
  226. title: '基础图层',
  227. });
  228. /* 返回图层 */
  229. if (callComplete) callComplete(tmsLayer);
  230. });
  231. },
  232. /**
  233. * 创建Mapbox格式的底图
  234. * @param {String} baseMapUrl 底图服务地址
  235. * @param {Function} callComplete 完成回调callComplete(baseLayer)
  236. */
  237. _initBaseMapMapboxLayer(baseMapUrl, callComplete) {
  238. let _self = this;
  239. require(["esri/layers/WebTileLayer"], function(WebTileLayer) {
  240. let url =
  241. "https://api.mapbox.com/styles/v1/chenchen1990/ckvgc14xrh7mo14qowdw4wrkf/tiles/512/{z}/{x}/{y}?access_token=pk.eyJ1IjoiY2hlbmNoZW4xOTkwIiwiYSI6ImNrbzA3eTY1OTA3dXkyd20zdG40ZGdmNXYifQ.xWKxjBG6mEDh55_oln0nAg";
  242. let mapboxLayer = new WebTileLayer(url, {
  243. "subDomains": ["a", "b", "c"]
  244. });
  245. if (callComplete) callComplete(mapboxLayer);
  246. });
  247. },
  248. /**
  249. * 初始化地图
  250. * @param {JSON} option 配置项
  251. */
  252. _init2dMap: function(option) {
  253. console.log("地图控件开始初始化...");
  254. var self = this;
  255. require(["esri/Map", "esri/views/MapView", "esri/core/urlUtils", "esri/layers/WMTSLayer",
  256. "esri/Basemap"
  257. ], function(Map, MapView, urlUtils, WMTSLayer, Basemap) {
  258. /* 创建底图 */
  259. self._initBaseMapMapboxLayer(option.baseMapUrl, function(baseLayer) {
  260. console.log("创建的底图", baseLayer);
  261. if (baseLayer) {
  262. /* 创建底图 */
  263. let baseMap = new Basemap({
  264. baseLayers: [baseLayer],
  265. });
  266. /* 挂接底图图层创建成功事件 */
  267. baseLayer.on('layerview-create', function() {
  268. if (self.onViewReady) self.onViewReady();
  269. });
  270. /* 挂接底图图层创建失败事件 */
  271. baseLayer.on('layerview-create-error', function(event) {
  272. if (self.onViewReady) self.onViewReady();
  273. });
  274. /* 创建地图实例 */
  275. self.map = new Map({
  276. basemap: baseMap,
  277. });
  278. } else {
  279. if (self.onViewReady) self.onViewReady();
  280. /* 创建地图实例 */
  281. self.map = new Map();
  282. }
  283. /* 创建地图视图实例 */
  284. self.mapView = new MapView({
  285. container: self.selector,
  286. map: self.map,
  287. });
  288. /* 清空所有自带组件 */
  289. self.mapView.ui.components = [];
  290. /* 禁止地图旋转 */
  291. self.mapView.constraints.rotationEnabled = false;
  292. /* 初始化加载的图层 */
  293. if (!self.loadLayers) {
  294. self.loadLayers = [];
  295. }
  296. /* 注册底图事件 */
  297. self._initRegisterMapEvent();
  298. });
  299. });
  300. },
  301. /**
  302. * 注册地图事件
  303. */
  304. _initRegisterMapEvent: function() {
  305. let _self = this;
  306. this.mapView.when(function() {
  307. /* 注册鼠标移动事件 */
  308. _self.mapView.on("pointer-move", function(event) {
  309. if (self.onViewEventPointMove) {
  310. var mapPoint = self.mapView.toMap({
  311. x: event.x,
  312. y: event.y
  313. });
  314. //构建JSON
  315. var result = {
  316. sx: event.x.toFixed(3),
  317. sy: event.y.toFixed(3),
  318. mx: mapPoint.x.toFixed(3),
  319. my: mapPoint.y.toFixed(3)
  320. }
  321. self.onViewEventPointMove(result);
  322. }
  323. });
  324. /* 注册地图拖动事件 */
  325. _self.mapView.on("drag", function(event) {
  326. if (self.onViewEventChange) {
  327. self.onViewEventChange(self.mapView.extent);
  328. }
  329. });
  330. /* 注册滚轮事件 */
  331. _self.mapView.on("mouse-wheel", function(event) {
  332. //此处使用延时 主要是因为mouse-wheel事件是在滚轮刚开始滚动时就开始
  333. //而不是在滚动结束时开始
  334. window.setTimeout(function() {
  335. if (self.onViewEventChange) {
  336. self.onViewEventChange(self.mapView.extent);
  337. }
  338. }, 1000);
  339. });
  340. /* 注册地图变化事件 */
  341. _self.mapView.on("resize", function(event) {
  342. if (_self.onViewEventChange) {
  343. _self.onViewEventChange(_self.mapView.extent);
  344. }
  345. });
  346. });
  347. },
  348. /**
  349. * 设置范围
  350. * @param {any} extent
  351. */
  352. setExtent: function(extent) {
  353. this.mapView.extent = extent;
  354. },
  355. /**
  356. * 设置放大比例
  357. * @param {any} zoom
  358. */
  359. setZoom: function(zoom) {
  360. this.mapView.zoom = zoom;
  361. },
  362. /**
  363. * 设置地图中心点
  364. * @param {any} center
  365. */
  366. setCenter: function(center) {
  367. this.mapView.center = center;
  368. },
  369. /**
  370. * 设置比例尺
  371. * @param {any} scale
  372. */
  373. setScale: function(scale) {
  374. this.mapView.scale = scale;
  375. },
  376. // 隐藏放大缩小的工具条
  377. hidenZoomToolBar: function() {
  378. this.mapView.ui.remove("zoom");
  379. },
  380. /** 隐藏底部的LOGO */
  381. hidenLogo: function() {
  382. this.mapView.ui.remove("attribution");
  383. },
  384. /** 放大 */
  385. zoomIn: function() {
  386. var self = this;
  387. require(["esri/widgets/Zoom"], function(Zoom) {
  388. var zoom = new Zoom({
  389. view: self.mapView
  390. });
  391. zoom.zoomIn();
  392. });
  393. },
  394. /** 缩小 */
  395. zoomOut: function() {
  396. var self = this;
  397. require(["esri/widgets/Zoom"], function(Zoom) {
  398. var zoom = new Zoom({
  399. view: self.mapView
  400. });
  401. zoom.zoomOut();
  402. });
  403. },
  404. /**
  405. * 定位地图中心位置
  406. * @param {any} longitude 经度
  407. * @param {any} latitude 纬度
  408. */
  409. zoomTo: function(longitude, latitude) {
  410. var self = this;
  411. require(["esri/geometry/SpatialReference",
  412. "esri/geometry/Extent"
  413. ], function(SpatialReference, Extent) {
  414. var spa = new SpatialReference({
  415. wkid: 4326
  416. });
  417. var extent = new Extent({
  418. xmin: parseFloat(longitude) - 0.002,
  419. ymin: parseFloat(latitude) - 0.002,
  420. xmax: parseFloat(longitude) + 0.002,
  421. ymax: parseFloat(latitude) + 0.002,
  422. spatialReference: spa
  423. })
  424. self.mapView.extent = extent;
  425. });
  426. },
  427. /**
  428. * 缩放地图至指定区域
  429. * @param {JSON} options 配置项
  430. * xmin{double}:经度最小值
  431. * ymin{double}:纬度最小值
  432. * xmax{double}:经度最大值
  433. * ymax{double}:纬度最大值
  434. */
  435. zoomToExtent: function(options) {
  436. var self = this;
  437. require(["esri/geometry/SpatialReference",
  438. "esri/geometry/Extent"
  439. ], function(SpatialReference, Extent) {
  440. var spa = new SpatialReference({
  441. wkid: 4326
  442. });
  443. var extent = new Extent({
  444. xmin: parseFloat(options.xmin),
  445. ymin: parseFloat(options.ymin),
  446. xmax: parseFloat(options.xmax),
  447. ymax: parseFloat(options.ymax),
  448. spatialReference: spa
  449. })
  450. self.mapView.extent = extent;
  451. });
  452. },
  453. /**
  454. * 缩放地图至指定区域
  455. * @param {JSON} options 配置项
  456. * xmin{double}:经度最小值
  457. * ymin{double}:纬度最小值
  458. * xmax{double}:经度最大值
  459. * ymax{double}:纬度最大值
  460. */
  461. zoomToExtentNormal: function(options) {
  462. let self = this;
  463. require(["esri/geometry/SpatialReference",
  464. "esri/geometry/Extent"
  465. ], function(SpatialReference, Extent) {
  466. let extent = new Extent({
  467. xmin: parseFloat(options.xmin),
  468. ymin: parseFloat(options.ymin),
  469. xmax: parseFloat(options.xmax),
  470. ymax: parseFloat(options.ymax),
  471. spatialReference: self.mapView.spatialReference,
  472. })
  473. self.mapView.extent = extent;
  474. });
  475. },
  476. });
  477. /**++++++++++++++添加小组件++++++++++++++++++++*/
  478. JTMapKit.prototype.extend({
  479. /**
  480. * 添加放大缩小组件
  481. */
  482. setUIZoomVisible: function(isVisible) {
  483. var self = this;
  484. require(["esri/widgets/Zoom"], function(Zoom) {
  485. if (self.uiZoom == null) {
  486. self.uiZoom = new Zoom({
  487. view: self.mapView,
  488. id: "toolsZoom"
  489. });
  490. }
  491. var widget = self.mapView.ui.find("toolsZoom");
  492. if (isVisible) {
  493. if (widget == undefined) {
  494. self.mapView.ui.add(self.uiZoom, {
  495. position: "top-left",
  496. });
  497. }
  498. } else {
  499. if (widget != undefined) {
  500. self.mapView.ui.remove(self.uiZoom);
  501. }
  502. }
  503. });
  504. },
  505. /**
  506. * 添加放大缩小组件
  507. */
  508. setUIScaleBarVisible: function(isVisible) {
  509. var self = this;
  510. require(["esri/widgets/ScaleBar"], function(ScaleBar) {
  511. if (self.uiScaleBar == null) {
  512. self.uiScaleBar = new ScaleBar({
  513. view: self.mapView,
  514. id: "toolsScaleBar",
  515. unit: "metric",
  516. });
  517. }
  518. var widget = self.mapView.ui.find("toolsScaleBar");
  519. if (isVisible) {
  520. if (widget == undefined) {
  521. self.mapView.ui.add(self.uiScaleBar, {
  522. position: "bottom-right",
  523. });
  524. }
  525. } else {
  526. if (widget != undefined) {
  527. self.mapView.ui.remove(self.uiScaleBar);
  528. }
  529. }
  530. });
  531. },
  532. });
  533. /**+++++++++++++++添加加载图层的相关内容+++++++++*/
  534. JTMapKit.prototype.extend({
  535. /**
  536. * 添加到图层中
  537. * @param {any} id 图层ID 如果时服务图层 则代表子图层的索引Index
  538. * @param {any} title 图层的名称
  539. * @param {any} visible 图层是否可见
  540. * @param {any} type 图层的类型
  541. * @param {any} slayer 所属服务图层
  542. */
  543. _addLayerToLayers: function(id, title, visible, type, slayer) {
  544. var self = this;
  545. self.loadLayers.push({
  546. id: id,
  547. title: title,
  548. visible: visible,
  549. type: type,
  550. slayer: slayer
  551. });
  552. },
  553. /**
  554. * 添加动态服务图层
  555. * @param {JSON} options 配置项
  556. * options.url{string}:服务地址
  557. * options.title{string}:图层标题
  558. * options.subids{array}:子图层id集合
  559. * options.visible{boolean}:图层是否显示
  560. * @param {function} callSuccess 成功回调
  561. * @param {function} callError 错误回调
  562. */
  563. addDynamicLayer: function(options, callSuccess, callError) {
  564. if (!options) return;
  565. /* 对子图层进行设置 */
  566. if (!options.subids || !options.subids.length) return;
  567. let sublayers = [];
  568. for (let subid of options.subids) {
  569. sublayers.push({
  570. id: subid,
  571. definitionExpression: options.filter == undefined ? "" : options
  572. .filter,
  573. })
  574. }
  575. let _self = this;
  576. require(["esri/layers/MapImageLayer"], function(MapImageLayer) {
  577. var layer = new MapImageLayer({
  578. url: options.url,
  579. title: options.title,
  580. sublayers: sublayers,
  581. visible: options.visible == undefined ? false : options
  582. .visible,
  583. });
  584. /* 添加成功加载监听 */
  585. layer.on("layerview-create", function() {
  586. layer.sublayers.find(function(subLayer) {
  587. subLayer.labelsVisible = options
  588. .labelVisible ===
  589. undefined ?
  590. false : options.labelVisible;
  591. })
  592. /* 设置子图层 */
  593. if (callSuccess) callSuccess();
  594. });
  595. /* 添加添加错误监听 */
  596. layer.on("layerview-create-error", function(event) {
  597. if (callError) callError(event.error.message);
  598. });
  599. _self.map.add(layer, 1);
  600. });
  601. },
  602. /**
  603. * 添加发布的切片服务
  604. * @param {JSON} options 配置项
  605. * options.url{string}:服务地址
  606. * options.title{string}:图层标题
  607. * @param {function} callSuccess 成功回调
  608. * @param {function} callError 错误回调
  609. */
  610. addTileLayer: function(options, callSuccess, callError) {
  611. if (!options) {
  612. return
  613. }
  614. let _self = this;
  615. require(["esri/layers/TileLayer"], function(TileLayer) {
  616. var layer = new TileLayer({
  617. url: options.url,
  618. title: options.title,
  619. });
  620. /* 添加创建成功回调 */
  621. layer.on("layerview-create", function() {
  622. if (callSuccess) callSuccess();
  623. });
  624. layer.on("layerview-create-error", function(event) {
  625. if (callError) callError(event.error.message);
  626. });
  627. _self.map.add(layer, 0);
  628. });
  629. },
  630. /**
  631. * 添加WMS服务图层
  632. * @param {any} option 地图选项
  633. * @param {any} index 地图图层索引
  634. */
  635. addWebTitleLayer: function(option, index, name, callback) {
  636. if (!option) {
  637. return;
  638. }
  639. var self = this;
  640. require(["esri/layers/WebTileLayer"], function(WebTileLayer) {
  641. var layer = new WebTileLayer(option);
  642. layer.title = name;
  643. //添加图层到视图中执行该代码
  644. layer.on("layerview-create", function() {
  645. //将该图层添加到图层中
  646. self._addLayerToLayers(layer.id, layer.title, layer
  647. .visible,
  648. "webtilelayer",
  649. undefined);
  650. if (callback) {
  651. callback();
  652. }
  653. });
  654. layer.on("layerview-create-error", function(event) {
  655. alert(event.error.message);
  656. });
  657. self.map.add(layer, index);
  658. return layer;
  659. });
  660. },
  661. /**
  662. * 加载底图
  663. * @param {Object} callback 加载完成后回调
  664. */
  665. loadBaseMap: function(callback) {
  666. var self = this;
  667. //添加天地图的影像图 回调后添加其他图层 主要时为了保证能够正常获取地图视图的坐标系
  668. self.addWebTitleLayer({
  669. urlTemplate: self.baseMapRasterURL,
  670. subDomains: self.subDomains,
  671. }, 0, "天地图影像图", function() {
  672. //添加天地图的标注图
  673. self.addWebTitleLayer({
  674. urlTemplate: self.baseMapRasterLabelURL,
  675. subDomains: self.subDomains,
  676. }, 100, "天地图标注", function() {
  677. callback();
  678. });
  679. });
  680. },
  681. /**
  682. * 切换底图为影像底图
  683. * @param {Object} callback 加载完成回调
  684. */
  685. loadBaseRasterMap: function(callback) {
  686. var self = this;
  687. self.map.layers.removeAt(0);
  688. self.map.layers.removeAt(100);
  689. //添加天地图的影像图 回调后添加其他图层 主要时为了保证能够正常获取地图视图的坐标系
  690. self.addWebTitleLayer({
  691. urlTemplate: self.baseMapRasterURL,
  692. subDomains: self.subDomains,
  693. }, 0, "天地图影像图", function() {
  694. //添加天地图的标注图
  695. self.addWebTitleLayer({
  696. urlTemplate: self.baseMapRasterLabelURL,
  697. subDomains: self.subDomains,
  698. }, 100, "天地图标注", function() {
  699. callback();
  700. });
  701. });
  702. },
  703. /**
  704. * 切换底图为标准底图
  705. * @param {Object} callback 加载完成回调
  706. */
  707. loadBaseVectorMap: function(callback) {
  708. var self = this;
  709. self.map.layers.removeAt(0);
  710. self.map.layers.removeAt(100);
  711. //添加天地图的影像图 回调后添加其他图层 主要时为了保证能够正常获取地图视图的坐标系
  712. self.addWebTitleLayer({
  713. urlTemplate: self.baseMapVectorURL,
  714. subDomains: self.subDomains,
  715. }, 0, "天地图影像图", function() {
  716. //添加天地图的标注图
  717. self.addWebTitleLayer({
  718. urlTemplate: self.baseMapVectorLabelURL,
  719. subDomains: self.subDomains,
  720. }, 100, "天地图标注", function() {
  721. callback();
  722. });
  723. });
  724. },
  725. /**
  726. * 获取加载的图层信息
  727. * @param {Object} callback 获取完成回调
  728. * callback([{title:},....])
  729. */
  730. getLayers: function(callback) {
  731. let _self = this;
  732. let resLayers = [];
  733. this.map.layers.forEach(function(layer, index) {
  734. resLayers.push({
  735. title: layer.title,
  736. });
  737. if (index === _self.map.layers.length - 1 && callback) {
  738. callback(resLayers);
  739. }
  740. });
  741. },
  742. });
  743. /**+++++++++++++++草图编辑相关+++++++++++++++++++*/
  744. JTMapKit.prototype.extend({
  745. /**
  746. * 草图视图模型
  747. * @param {function} callback 回调
  748. */
  749. _sketchViewModel: function(callback) {
  750. var self = this;
  751. require([
  752. "esri/layers/GraphicsLayer",
  753. "esri/widgets/Sketch/SketchViewModel",
  754. "esri/geometry/projection",
  755. "esri/geometry/SpatialReference"
  756. ],
  757. function(GraphicsLayer, SketchViewModel, projection, SpatialReference) {
  758. /* 加入草图图层 */
  759. if (!self.layerSketch) {
  760. self.layerSketch = new GraphicsLayer();
  761. self.map.add(self.layerSketch);
  762. }
  763. /* 如果草图编辑工具已经初始化 则释放资源 重新初始化 */
  764. if (self.sketchViewModel) {
  765. self.sketchViewModel.layer.graphics.removeAll();
  766. self.sketchDrawGeometry = undefined;
  767. self.sketchViewModel.destroy();
  768. self.sketchViewModel = null;
  769. }
  770. /* 创建草图编辑工具 */
  771. if (!self.sketchViewModel) {
  772. self.sketchViewModel = new SketchViewModel({
  773. layer: self.layerSketch,
  774. view: self.mapView,
  775. polygonSymbol: self.sketchPolygonSymbol,
  776. });
  777. /* 绑定创建事件 */
  778. self.sketchViewModel.on("create", function(event) {
  779. if (event.state == "start") {
  780. self.layerSketch.removeAll(); //删除全部
  781. }
  782. /* 输出绘制要素 */
  783. self._outGraphic(event, "创建");
  784. });
  785. /* 绑定redo事件 */
  786. self.sketchViewModel.on('redo', function(event) {
  787. self._outGraphic(event, "恢复");
  788. });
  789. /* 绑定undo事件 */
  790. self.sketchViewModel.on('undo', function(event) {
  791. self._outGraphic(event, "回退");
  792. });
  793. }
  794. /* 创建成功回调 */
  795. if (callback) callback();
  796. });
  797. },
  798. /**
  799. * 草图编辑过程中输出
  800. * @param {Object} event 草图编辑事件
  801. * @param {string} eventName 事件名称
  802. */
  803. _outGraphic: function(event, eventName) {
  804. /* 全局替换 */
  805. let _self = this;
  806. /* 全局定义草图编辑绘制的要素 */
  807. this.sketchDrawGeometry = undefined;
  808. /* 对绘制的草图要素进行赋值 */
  809. if (event.graphic != undefined) {
  810. this.sketchDrawGeometry = event.graphic.geometry
  811. }
  812. /* 对绘制的草图要素进行赋值 */
  813. if (event.graphics != undefined) {
  814. this.sketchDrawGeometry = event.graphics[0].geometry;
  815. }
  816. /* 输出回调 这很重要 */
  817. if (this.onQueryPolygonComplete != undefined && this.sketchDrawGeometry !=
  818. undefined && this
  819. .currentActionType == this.actionType.LayerQueryPolygon) {
  820. /* 多图层区域查询回调 */
  821. if (event.type == 'create' && event.state == 'start') {
  822. this.onQueryPolygonComplete(0, this.sketchDrawGeometry);
  823. } else {
  824. this.onQueryPolygonComplete(this.sketchDrawGeometry.rings[0].length,
  825. this
  826. .sketchDrawGeometry);
  827. }
  828. } else if (this.onCommonSpatialAnalysisComplete != undefined && this
  829. .sketchDrawGeometry !=
  830. undefined && this.currentActionType == this.actionType.CommonSpatialAnalysis
  831. ) {
  832. /* 通用空间占地分析回调 */
  833. if (event.type == 'create' && event.state == 'start') {
  834. this._projectTransform({
  835. geometry: this.sketchDrawGeometry,
  836. wkid: 4326,
  837. }, function(outGeometry) {
  838. _self.onCommonSpatialAnalysisComplete(0, outGeometry);
  839. })
  840. } else {
  841. this._projectTransform({
  842. geometry: this.sketchDrawGeometry,
  843. wkid: 4326,
  844. }, function(outGeometry) {
  845. _self.onCommonSpatialAnalysisComplete(outGeometry.rings[0]
  846. .length,
  847. outGeometry);
  848. })
  849. }
  850. }
  851. },
  852. /**
  853. * 坐标转换
  854. * @param {JSON} options 配置项
  855. * @param {function} callSuccess 转换成功回调
  856. */
  857. _projectTransform: function(options, callSuccess) {
  858. require([
  859. "esri/geometry/projection",
  860. "esri/geometry/SpatialReference"
  861. ], function(projection, SpatialReference) {
  862. let outSpatialReference = new SpatialReference({
  863. wkid: options.wkid,
  864. });
  865. projection.load().then(function() {
  866. let outGeometry = projection.project(options.geometry,
  867. outSpatialReference);
  868. if (callSuccess) callSuccess(outGeometry);
  869. })
  870. });
  871. },
  872. /**
  873. * 提取要素坐标
  874. * @param {Object} geo 待提取的要素
  875. * @param {Function} callback 提取完成回调
  876. */
  877. _pickCoordinate: function(geo, callback) {
  878. var resCoords = "";
  879. if (geo.type == "point") {
  880. resCoords = point.x + "," + point.y;
  881. callback("point", resCoords);
  882. } else if (geo.type == "polyline") {
  883. for (var idx = 0; idx < geo.paths[0].length; idx++) {
  884. var point = geo.getPoint(0, idx);
  885. if (resCoords == "") {
  886. resCoords = point.x + "," + point.y;
  887. } else {
  888. resCoords += "," + point.x + "," + point.y;
  889. }
  890. }
  891. callback("polyline", resCoords);
  892. } else if (geo.type == "polygon") {
  893. for (var idx = 0; idx < geo.rings[0].length; idx++) {
  894. var point = geo.getPoint(0, idx);
  895. if (resCoords == "") {
  896. resCoords = point.x + "," + point.y;
  897. } else {
  898. resCoords += "," + point.x + "," + point.y;
  899. }
  900. }
  901. callback("polygon", resCoords);
  902. } else {
  903. callback(undefined, "");
  904. }
  905. },
  906. /**
  907. * 激活草图绘制面要素工具
  908. */
  909. sketchPolygonTools: function() {
  910. var self = this;
  911. this._sketchViewModel(function() {
  912. self.sketchViewModel.create("polygon", {
  913. mode: "click"
  914. });
  915. });
  916. },
  917. /**
  918. * 草图恢复
  919. */
  920. sketchRedo: function() {
  921. if (this.sketchViewModel) {
  922. this.sketchViewModel.redo();
  923. }
  924. },
  925. /**
  926. * 草图回退
  927. */
  928. sketchUndo: function() {
  929. if (this.sketchViewModel) {
  930. this.sketchViewModel.undo();
  931. }
  932. },
  933. /**
  934. * 草图删除全部绘制
  935. */
  936. sketchDelete: function() {
  937. var self = this;
  938. this._sketchViewModel(function() {
  939. self.sketchViewModel.cancel();
  940. self.layerSketch.removeAll(); //删除全部
  941. });
  942. },
  943. /**
  944. * 获取全部绘制要素
  945. * @param {function} callback 回调
  946. */
  947. sketchGraphic: function(callback) {
  948. var self = this;
  949. if (self.layerSketch) {
  950. if (self.layerSketch.graphics.length > 0) {
  951. self.layerSketch.graphics.filter(function(graphic) {
  952. self.sGraphic = graphic;
  953. require([
  954. "esri/geometry/projection",
  955. "esri/geometry/SpatialReference",
  956. "esri/geometry/support/geodesicUtils"
  957. ],
  958. function(projection, SpatialReference,
  959. geodesicUtils) {
  960. //定义坐标系
  961. var outSpatialReference = new SpatialReference({
  962. wkid: 4326
  963. });
  964. //默认没有坐标系 需要设置
  965. self.sGraphic.geometry.spatialReference = self
  966. .mapView
  967. .spatialReference;
  968. //必须这样执行 只有projection.load()执行完成才能调用坐标转换
  969. projection.load().then(function() {
  970. var proGeometry = projection
  971. .project(self
  972. .sGraphic
  973. .geometry,
  974. outSpatialReference);
  975. self._pickCoordinate(proGeometry,
  976. function(type,
  977. result) {
  978. callback(type, result);
  979. });
  980. });
  981. });
  982. });
  983. } else {
  984. //如果没有绘制要素则返回undefined
  985. callback(0, undefined);
  986. }
  987. } else {
  988. callback(false, undefined);
  989. }
  990. },
  991. /**
  992. * 添加绘制编辑要素
  993. * @param {Object} graphic 要素
  994. */
  995. sketchAddGraphic: function(graphic) {
  996. var self = this;
  997. this._sketchViewModel(function() {
  998. require([
  999. "esri/geometry/projection",
  1000. "esri/geometry/SpatialReference",
  1001. "esri/Graphic"
  1002. ],
  1003. function(projection, SpatialReference, Graphic) {
  1004. //定义坐标系
  1005. var outSpatialReference = new SpatialReference({
  1006. wkid: 4326
  1007. });
  1008. graphic.spatialReference = outSpatialReference;
  1009. //由于选择的图斑是WGS84坐标系下的成果 需要改正
  1010. projection.load().then(function() {
  1011. self.layerSketch.removeAll();
  1012. var polygon = projection.project(graphic
  1013. .geometry, self
  1014. .mapView
  1015. .spatialReference);
  1016. var resGraphic = new Graphic({
  1017. geometry: polygon,
  1018. symbol: self.sketchViewModel
  1019. .polygonSymbol,
  1020. });
  1021. self.layerSketch.add(resGraphic);
  1022. });
  1023. });
  1024. });
  1025. },
  1026. });
  1027. /**+++++++++++++++绘制相关+++++++++++++++++++++++*/
  1028. JTMapKit.prototype.extend({
  1029. _draw: function(type, option) {
  1030. var self = this;
  1031. self.isDrawing = true;
  1032. require([
  1033. "esri/views/draw/Draw",
  1034. "esri/layers/GraphicsLayer",
  1035. "esri/Graphic"
  1036. ],
  1037. function(Draw, GraphicsLayer, Graphic) {
  1038. self.clearGraphic();
  1039. if (!self.graphicsLayer) {
  1040. self.graphicsLayer = new GraphicsLayer();
  1041. self.map.layers.add(self.graphicsLayer);
  1042. }
  1043. var draw = new Draw({
  1044. view: self.mapView
  1045. });
  1046. var action = draw.create(type);
  1047. option.type = type;
  1048. //添加点
  1049. action.on("vertex-add", function(evt) {
  1050. self._drawActionHandler(evt, option, Graphic);
  1051. });
  1052. //绘制结束
  1053. action.on("draw-complete", function(evt) {
  1054. self.isDrawing = false;
  1055. self._drawActionHandler(evt, option, Graphic);
  1056. });
  1057. //删除点
  1058. action.on("vertex-remove", function(evt) {
  1059. self._drawActionHandler(evt, option, Graphic);
  1060. });
  1061. action.on("cursor-update", function(evt) {
  1062. if (type === 'circle') {
  1063. self._drawActionHandler(evt, option, Graphic);
  1064. } else if (type === 'rectangle') {
  1065. self._drawActionHandler(evt, option, Graphic);
  1066. }
  1067. })
  1068. if (type === 'multipoint') {
  1069. self.mapView.on('click', function(evt) {
  1070. self._addMultipoint(evt, option, Graphic)
  1071. })
  1072. }
  1073. });
  1074. },
  1075. //绘制处理事件
  1076. _drawActionHandler(evt, option, Graphic) {
  1077. if (option.type === 'circle') {
  1078. this._drawCircleActionHandler(evt, option, Graphic);
  1079. return;
  1080. } else if (option.type === 'rectangle') {
  1081. this._drawRectangleActionHandler(evt, option, Graphic);
  1082. return;
  1083. }
  1084. var geometry;
  1085. if (evt.coordinates) { //绘制单个点时获取的是coordinates
  1086. var coordinates = evt.coordinates;
  1087. geometry = {
  1088. type: "point",
  1089. x: coordinates[0],
  1090. y: coordinates[1],
  1091. spatialReference: this.mapView.spatialReference
  1092. };
  1093. } else if (evt.vertices) {
  1094. var vertices = evt.vertices;
  1095. var type = option.type;
  1096. geometry = {
  1097. spatialReference: this.mapView.spatialReference
  1098. };
  1099. //多点
  1100. if (type === 'multipoint') {
  1101. this.isDrawing = false;
  1102. geometry.points = vertices;
  1103. geometry.type = "multipoint";
  1104. } else if (type === 'polyline') {
  1105. geometry.paths = vertices;
  1106. geometry.type = "polyline";
  1107. } else {
  1108. geometry.rings = vertices;
  1109. geometry.type = "polygon";
  1110. }
  1111. }
  1112. var graphic = new Graphic({
  1113. geometry: geometry,
  1114. symbol: option.symbol
  1115. });
  1116. this.graphicsLayer.removeAll();
  1117. this.graphicsLayer.add(graphic);
  1118. //将绘制的要素通过回调方法回调回去
  1119. if (this.onDrawEventComplete && !this.isDrawing) {
  1120. //将绘制的图元要素返回
  1121. this.onDrawEventComplete(geometry);
  1122. }
  1123. },
  1124. //绘制圆
  1125. _drawCircleActionHandler(evt, option, Graphic) {
  1126. var self = this;
  1127. require(["esri/geometry/Circle",
  1128. "esri/geometry/Point"
  1129. ], function(Circle, Point) {
  1130. var vertices = evt.vertices;
  1131. if (vertices.length < 2) {
  1132. return
  1133. }
  1134. self.graphicsLayer.removeAll();
  1135. var center = new Point({
  1136. hasZ: false,
  1137. hasM: false,
  1138. x: vertices[0][0],
  1139. y: vertices[0][1],
  1140. spatialReference: self.mapView.spatialReference
  1141. });
  1142. var radius = center.distance(new Point({
  1143. hasZ: false,
  1144. hasM: false,
  1145. x: vertices[1][0],
  1146. y: vertices[1][1],
  1147. spatialReference: self.mapView.spatialReference
  1148. }));
  1149. var graphic = new Graphic({
  1150. geometry: new Circle({
  1151. hasZ: false,
  1152. hasM: false,
  1153. center: center,
  1154. radius: radius,
  1155. spatialReference: self.mapView.spatialReference
  1156. }),
  1157. symbol: option.symbol
  1158. });
  1159. self.graphicsLayer.add(graphic);
  1160. });
  1161. },
  1162. //绘制矩形
  1163. _drawRectangleActionHandler(evt, option, Graphic) {
  1164. var self = this;
  1165. require(["esri/geometry/Circle",
  1166. "esri/geometry/Polygon"
  1167. ], function(Circle, Polygon) {
  1168. //获取所有顶点
  1169. var vertices = evt.vertices;
  1170. if (vertices.length < 2) {
  1171. return
  1172. }
  1173. var rings = [vertices[0],
  1174. [vertices[0][0], vertices[1][1]], vertices[1],
  1175. [vertices[1][0], vertices[0][1]]
  1176. ];
  1177. self.graphicsLayer.removeAll();
  1178. var graphic = new Graphic({
  1179. geometry: new Polygon({
  1180. hasZ: false,
  1181. hasM: false,
  1182. rings: [rings],
  1183. spatialReference: self.mapView.spatialReference
  1184. }),
  1185. symbol: option.symbol
  1186. });
  1187. self.graphicsLayer.add(graphic);
  1188. });
  1189. },
  1190. //绘制多个点
  1191. _addMultipoint: function(evt, option, Graphic) {
  1192. if (this.isDrawing) {
  1193. var graphic = new Graphic({
  1194. geometry: evt.mapPoint,
  1195. symbol: option.symbol
  1196. });
  1197. this.graphicsLayer.add(graphic);
  1198. }
  1199. },
  1200. //绘制单个点
  1201. drawPoint: function(option) {
  1202. var options = option || {};
  1203. if (!options.symbol) {
  1204. options.symbol = this.pointSymbol;
  1205. }
  1206. this._draw('point', options);
  1207. },
  1208. //绘制多个点
  1209. drawMultiPoint: function(option) {
  1210. this.isDrawing = true;
  1211. var options = option || {};
  1212. if (!options.symbol) {
  1213. options.symbol = this.pointSymbol;
  1214. }
  1215. this._draw('multipoint', options);
  1216. },
  1217. //绘制线
  1218. drawPolyline: function(option) {
  1219. var options = option || {};
  1220. if (!options.symbol) {
  1221. options.symbol = this.lineSymbol;
  1222. }
  1223. this._draw('polyline', options);
  1224. },
  1225. //绘制多边形
  1226. drawPolygon: function(option) {
  1227. var options = option || {};
  1228. if (!options.symbol) {
  1229. options.symbol = this.fillSymbol;
  1230. }
  1231. this._draw('polygon', options);
  1232. },
  1233. //绘制矩形
  1234. drawRectangle: function(option) {
  1235. var options = option || {};
  1236. if (!options.symbol) {
  1237. options.symbol = this.fillSymbol;
  1238. }
  1239. this._draw('rectangle', options);
  1240. },
  1241. //绘制圆
  1242. drawCircle: function(option) {
  1243. var options = option || {};
  1244. if (!options.symbol) {
  1245. options.symbol = this.fillSymbol;
  1246. }
  1247. this._draw('circle', options);
  1248. },
  1249. //清空绘制图层
  1250. clearGraphic: function() {
  1251. if (this.graphicsLayer) {
  1252. this.graphicsLayer.removeAll();
  1253. }
  1254. this.sketchDelete(); //删除编辑工具编辑内容
  1255. this.queryTaskGraphic = undefined; //初始化查询绘制要素
  1256. this.measureGraphic = undefined; //初始化测量绘制要素
  1257. if (this.draw) {
  1258. this.draw.reset(); //清除全部动作
  1259. this.draw = undefined;
  1260. }
  1261. this.isDrawing = false; //重置测量标志
  1262. this.mapView.popup.visible = false; //关闭气泡窗口
  1263. }
  1264. })
  1265. /**+++++++++++++++查询相关+++++++++++++++++++++++*/
  1266. JTMapKit.prototype.extend({
  1267. /**
  1268. * 查询任务
  1269. * @param {Object} type 类型
  1270. * @param {Object} option 配置项
  1271. */
  1272. _queryTack: function(type, option) {
  1273. var self = this;
  1274. self.clearGraphic();
  1275. self.isDrawing = true;
  1276. require([
  1277. "esri/views/draw/Draw",
  1278. "esri/layers/GraphicsLayer",
  1279. "esri/Graphic"
  1280. ],
  1281. function(Draw, GraphicsLayer, Graphic) {
  1282. if (!self.graphicsLayer) {
  1283. self.graphicsLayer = new GraphicsLayer();
  1284. self.map.layers.add(self.graphicsLayer);
  1285. }
  1286. if (!self.draw) {
  1287. self.draw = draw = new Draw({
  1288. view: self.mapView,
  1289. });
  1290. } else {
  1291. self.draw.reset();
  1292. }
  1293. var action = draw.create(type);
  1294. action.mode = "click"; //只有点击绘制
  1295. option.type = type;
  1296. //添加点
  1297. action.on("vertex-add", function(evt) {
  1298. self._queryTackActionHandler(evt, option, Graphic);
  1299. });
  1300. //绘制结束
  1301. action.on("draw-complete", function(evt) {
  1302. self.isDrawing = false;
  1303. self._queryTackActionHandler(evt, option, Graphic);
  1304. });
  1305. action.on("cursor-update", function(evt) {
  1306. if (type === 'rectangle') {
  1307. self._queryTackRectangleActionHandler(evt, option,
  1308. Graphic);
  1309. }
  1310. })
  1311. });
  1312. },
  1313. /**
  1314. * @param {Object} evt 事件
  1315. * @param {JSON} option 配置项
  1316. * @param {Object} Graphic 要素
  1317. */
  1318. _queryTackActionHandler(evt, option, Graphic) {
  1319. var self = this;
  1320. //矩形查询绘制事件
  1321. if (option.type === 'rectangle') {
  1322. this._queryTackRectangleActionHandler(evt, option, Graphic);
  1323. return;
  1324. }
  1325. var geometry, clickPoint;
  1326. if (evt.coordinates) { //绘制单个点时获取的是coordinates
  1327. var coordinates = evt.coordinates;
  1328. geometry = {
  1329. type: "point",
  1330. x: coordinates[0],
  1331. y: coordinates[1],
  1332. spatialReference: this.mapView.spatialReference
  1333. };
  1334. } else if (evt.vertices) {
  1335. var vertices = evt.vertices;
  1336. //创建点击点 以便实现效果
  1337. clickPoint = {
  1338. type: "point",
  1339. x: vertices[vertices.length - 1][0],
  1340. y: vertices[vertices.length - 1][1],
  1341. spatialReference: this.mapView.spatialReference
  1342. };
  1343. var type = option.type;
  1344. geometry = {
  1345. spatialReference: this.mapView.spatialReference
  1346. };
  1347. if (type === 'polyline') {
  1348. geometry.paths = vertices;
  1349. geometry.type = "polyline";
  1350. } else {
  1351. geometry.rings = vertices;
  1352. geometry.type = "polygon";
  1353. }
  1354. }
  1355. //如果不存在则创建 否则更新
  1356. if (!this.queryTaskGraphic) {
  1357. this.queryTaskGraphic = new Graphic({
  1358. geometry: geometry,
  1359. symbol: option.symbol
  1360. });
  1361. this.graphicsLayer.add(this.queryTaskGraphic);
  1362. } else {
  1363. this.queryTaskGraphic.geometry = geometry;
  1364. }
  1365. //追加点击点
  1366. var graphicPoint = new Graphic({
  1367. geometry: clickPoint,
  1368. symbol: this.pointSymbol
  1369. });
  1370. this.graphicsLayer.add(graphicPoint);
  1371. var mGeometry = this.queryTaskGraphic.geometry; //获取绘制的要素
  1372. /* 判断查询方式 */
  1373. if (option.type === 'point') {
  1374. self._queryTaskFromGeometry(self.queryTaskGraphic.geometry);
  1375. self.queryTaskGraphic = undefined;
  1376. } else if (option.type === 'polygon') {
  1377. //绘制的是查询面 且点数大于等于3
  1378. if (mGeometry.type == 'polygon' && mGeometry.rings[0].length >= 3) {
  1379. if (self.onCaseQueryPolygonBegin != undefined) {
  1380. self.onCaseQueryPolygonBegin();
  1381. }
  1382. }
  1383. }
  1384. },
  1385. /**
  1386. * 显示测量结果
  1387. * @param {ESRIPoint} location 测量结果内容
  1388. * @param {function} callback 点击查询回调
  1389. */
  1390. _showQueryResultPopup: function(location, callback) {
  1391. /* 创建查询按钮 */
  1392. var queryButton = document.createElement("button");
  1393. queryButton.className =
  1394. "mui-btn mui-btn-primary mui-icon app-icon app-icon-map-areaquery wc-btn-child wc-btn-query";
  1395. var btnLabel = document.createTextNode("查询案件");
  1396. queryButton.appendChild(btnLabel);
  1397. queryButton.onclick = function(event) {
  1398. if (callback) callback();
  1399. }
  1400. /* 显示弹窗试试 */
  1401. var popup = this.mapView.popup;
  1402. /* 显示位置 */
  1403. popup.location = location;
  1404. /* 设置标题 */
  1405. popup.title = "区域案件查询";
  1406. /* 设置显示内容 */
  1407. popup.viewModel.content = queryButton;
  1408. /* 控制窗口的停靠按钮是否显示 */
  1409. popup.dockOptions = {
  1410. buttonEnabled: false,
  1411. }
  1412. /* 控制是否显示关闭按钮 */
  1413. popup.visibleElements = {
  1414. closeButton: true,
  1415. featureNavigation: false
  1416. }
  1417. popup.on("trigger-action", function(event) {
  1418. console.log("ActionId = " + event.action.id);
  1419. });
  1420. /* 关闭自动缩放按钮 */
  1421. popup.viewModel.includeDefaultActions = false;
  1422. /* 控制是否显示 */
  1423. popup.visible = true;
  1424. },
  1425. //查询绘制矩形
  1426. _queryTackRectangleActionHandler(evt, option, Graphic) {
  1427. var self = this;
  1428. require(["esri/geometry/Circle",
  1429. "esri/geometry/Polygon"
  1430. ], function(Circle, Polygon) {
  1431. //获取所有顶点
  1432. var vertices = evt.vertices;
  1433. if (vertices.length < 2) {
  1434. return
  1435. }
  1436. var rings = [vertices[0],
  1437. [vertices[0][0], vertices[1][1]], vertices[1],
  1438. [vertices[1][0], vertices[0][1]]
  1439. ];
  1440. self.graphicsLayer.removeAll();
  1441. var graphic = new Graphic({
  1442. geometry: new Polygon({
  1443. hasZ: false,
  1444. hasM: false,
  1445. rings: [rings],
  1446. spatialReference: self.mapView.spatialReference
  1447. }),
  1448. symbol: option.symbol
  1449. });
  1450. self.graphicsLayer.removeAll();
  1451. self.graphicsLayer.add(graphic);
  1452. //执行查询
  1453. if (!self.isDrawing) {
  1454. self._queryTaskFromGeometry(graphic.geometry);
  1455. }
  1456. });
  1457. },
  1458. /**
  1459. * 根据要素进行查询
  1460. * @param {Object} geometry 参考要素
  1461. */
  1462. _queryTaskFromGeometry: function(geometry) {
  1463. var self = this;
  1464. require([
  1465. "esri/tasks/support/Query",
  1466. "esri/tasks/QueryTask",
  1467. "esri/geometry/projection",
  1468. "esri/geometry/SpatialReference"
  1469. ],
  1470. function(Query, QueryTask, projection, SpatialReference) {
  1471. var qTask = new QueryTask({
  1472. url: self.queryURL
  1473. });
  1474. var params = new Query({
  1475. returnGeometry: true,
  1476. outFields: ["*"],
  1477. distance: 2,
  1478. geometry: geometry,
  1479. spatialRelationship: "intersects",
  1480. where: self.queryFilter,
  1481. });
  1482. //定义坐标系
  1483. var outSpatialReference = new SpatialReference({
  1484. wkid: 4326
  1485. });
  1486. qTask.execute(params).then(function(response) {
  1487. /* 打印当前查询类型 */
  1488. console.log("当前查询类型 = " + self.currentActionType);
  1489. /* 如果当前为案件分析动作 且挂接了回调事件 */
  1490. if (self.onForestAnalysisEvent && self
  1491. .currentActionType == self
  1492. .actionType.ForestAnalysis) {
  1493. self.graphicsLayer.removeAll(); //删除绘制的要素
  1494. if (response.features.length == 0) {
  1495. self.onForestAnalysisEvent(undefined);
  1496. } else {
  1497. self.onForestAnalysisEvent(response.features[0]
  1498. .attributes[
  1499. 'OBJECTID']);
  1500. }
  1501. } else if (self.onDiggingsAnalysisEvent && self
  1502. .currentActionType ==
  1503. self
  1504. .actionType.DiggingsAnalysis) {
  1505. self.graphicsLayer.removeAll(); //删除绘制的要素
  1506. if (response.features.length == 0) {
  1507. self.onDiggingsAnalysisEvent(undefined);
  1508. } else {
  1509. self.onDiggingsAnalysisEvent(response.features[
  1510. 0]
  1511. .attributes[
  1512. 'OBJECTID']);
  1513. }
  1514. } else if (self.onQueryTackEvent) {
  1515. self.graphicsLayer.removeAll(); //删除绘制的要素
  1516. //变换投影
  1517. projection.load().then(function() {
  1518. //将最终的要素图形转换为经纬度返回
  1519. response.features.forEach(function(
  1520. graphic, index) {
  1521. graphic.geometry
  1522. .spatialReference = self
  1523. .mapView
  1524. .spatialReference;
  1525. var geo = projection
  1526. .project(graphic
  1527. .geometry,
  1528. outSpatialReference
  1529. );
  1530. graphic.geometry = geo;
  1531. });
  1532. self.onQueryTackEvent(response
  1533. .features);
  1534. });
  1535. }
  1536. /* 清除查询标志 */
  1537. self.currentActionType = self.actionType.unknown;
  1538. });
  1539. }
  1540. );
  1541. },
  1542. /**
  1543. * 根据绘制要素执行查询任务
  1544. */
  1545. queryTaskFromGeometry: function() {
  1546. this._queryTaskFromGeometry(this.queryTaskGraphic.geometry);
  1547. this.queryTaskGraphic = undefined;
  1548. },
  1549. /**
  1550. * 点击查询
  1551. * @param {JSON} option
  1552. */
  1553. queryTackPoint: function(option) {
  1554. var options = option || {};
  1555. this.isDrawing = true;
  1556. if (!options.symbol) {
  1557. options.symbol = this.pointSymbol;
  1558. }
  1559. if (option.url) {
  1560. this.queryURL = option.url;
  1561. }
  1562. if (option.filter) {
  1563. this.queryFilter = option.filter;
  1564. }
  1565. this._queryTack('point', options);
  1566. },
  1567. /**
  1568. * 点图查询案件
  1569. * @param {url} option 配置项
  1570. * url{string}:查询图层访问地址
  1571. * filter{string}:查询图层过滤条件
  1572. * actionType{string}:动作类型
  1573. */
  1574. queryCase: function(option) {
  1575. var options = option || {};
  1576. this.isDrawing = true;
  1577. if (!options.symbol) {
  1578. options.symbol = this.pointSymbol;
  1579. }
  1580. if (option.url) {
  1581. this.queryURL = option.url;
  1582. }
  1583. if (option.filter) {
  1584. this.queryFilter = option.filter;
  1585. }
  1586. /* 设置当前动作类型为案件查询 */
  1587. this.currentActionType = this.actionType.CaseAnalysis;
  1588. this._queryTack('point', options);
  1589. },
  1590. /**
  1591. * 点图查询林地
  1592. * @param {url} option 配置项
  1593. * url{string}:查询图层访问地址
  1594. * filter{string}:查询图层过滤条件
  1595. * actionType{string}:动作类型
  1596. */
  1597. queryForest: function(option) {
  1598. var options = option || {};
  1599. this.isDrawing = true;
  1600. if (!options.symbol) {
  1601. options.symbol = this.pointSymbol;
  1602. }
  1603. if (option.url) {
  1604. this.queryURL = option.url;
  1605. }
  1606. if (option.filter) {
  1607. this.queryFilter = option.filter;
  1608. }
  1609. /* 设置当前动作类型为案件查询 */
  1610. this.currentActionType = this.actionType.ForestAnalysis;
  1611. this._queryTack('point', options);
  1612. },
  1613. /**
  1614. * 点图查询矿区治理区
  1615. * @param {url} option 配置项
  1616. * url{string}:查询图层访问地址
  1617. * filter{string}:查询图层过滤条件
  1618. * actionType{string}:动作类型
  1619. */
  1620. queryDiggingsAnalysis: function(option) {
  1621. var options = option || {};
  1622. this.isDrawing = true;
  1623. if (!options.symbol) {
  1624. options.symbol = this.pointSymbol;
  1625. }
  1626. if (option.url) {
  1627. this.queryURL = option.url;
  1628. }
  1629. if (option.filter) {
  1630. this.queryFilter = option.filter;
  1631. }
  1632. /* 设置当前动作类型为案件查询 */
  1633. this.currentActionType = this.actionType.DiggingsAnalysis;
  1634. this._queryTack('point', options);
  1635. },
  1636. /**
  1637. * @param {Object} option 多边形查询
  1638. */
  1639. queryTackPolygon: function(option) {
  1640. var options = option || {};
  1641. this.isDrawing = true;
  1642. if (!options.symbol) {
  1643. options.symbol = this.fillSymbol;
  1644. }
  1645. if (option.url) {
  1646. this.queryURL = option.url;
  1647. }
  1648. if (option.filter) {
  1649. this.queryFilter = option.filter;
  1650. }
  1651. this._queryTack('polygon', options);
  1652. },
  1653. /**
  1654. * 矩形查询
  1655. * @param {JSON} option 配置项
  1656. */
  1657. queryTackRectangle: function(option) {
  1658. var options = option || {};
  1659. this.isDrawing = true;
  1660. if (!options.symbol) {
  1661. options.symbol = this.fillSymbol;
  1662. }
  1663. if (option.url) {
  1664. this.queryURL = option.url;
  1665. }
  1666. if (option.filter) {
  1667. this.queryFilter = option.filter;
  1668. }
  1669. this._queryTack('rectangle', options);
  1670. },
  1671. /**
  1672. * 根据传入的参数查询图层数据
  1673. * @param {JSON} option 配置项
  1674. * type:{string} 类型<point\polygon>
  1675. * url{string}:查询的服务地址
  1676. * filter{string}:过滤条件
  1677. * queryLayers{array}:查询图层数组
  1678. */
  1679. queryLayerByGeometry: function(option) {
  1680. let _self = this;
  1681. if (option.url) {
  1682. this.queryURL = option.url;
  1683. }
  1684. if (option.filter) {
  1685. this.queryFilter = option.filter;
  1686. }
  1687. if (option.queryLayers) {
  1688. this.queryLayers = option.queryLayers;
  1689. }
  1690. require([
  1691. "esri/geometry/Polygon",
  1692. "esri/geometry/Point",
  1693. ], function(Polygon, Point) {
  1694. let geometry = undefined;
  1695. if (option.type === 'polygon') {
  1696. geometry = new Polygon({
  1697. spatialReference: {
  1698. wkid: option.geometry.spatialReference.wkid,
  1699. },
  1700. rings: option.geometry.rings,
  1701. })
  1702. } else if (option.type === 'point') {
  1703. geometry = new Point({
  1704. spatialReference: {
  1705. wkid: option.geometry.spatialReference.wkid,
  1706. },
  1707. x: option.geometry.x,
  1708. y: option.geometry.y,
  1709. })
  1710. }
  1711. if (geometry != undefined) {
  1712. _self._queryLayersTaskFromGeometry(geometry);
  1713. }
  1714. });
  1715. },
  1716. /**
  1717. * @param {string} type
  1718. * @param {JSON} option 配置项
  1719. */
  1720. _queryLayersTack: function(type, option) {
  1721. var self = this;
  1722. /* 清除绘制的要素 */
  1723. self.clearGraphic();
  1724. /* 设置当前为绘制模式 */
  1725. self.isDrawing = true;
  1726. require([
  1727. "esri/views/draw/Draw",
  1728. "esri/layers/GraphicsLayer",
  1729. "esri/Graphic"
  1730. ],
  1731. function(Draw, GraphicsLayer, Graphic) {
  1732. /* 判断绘制要素的图层是否存在,如果不存在则创建 */
  1733. if (!self.graphicsLayer) {
  1734. self.graphicsLayer = new GraphicsLayer();
  1735. self.map.layers.add(self.graphicsLayer);
  1736. }
  1737. /* 判断绘制是否启用,如果未启用,则创建,如果启用则重置 */
  1738. if (!self.draw) {
  1739. self.draw = draw = new Draw({
  1740. view: self.mapView,
  1741. });
  1742. } else {
  1743. self.draw.reset();
  1744. }
  1745. /* 创建绘制动作 */
  1746. let action = draw.create(type);
  1747. action.mode = "click"; //只有点击绘制
  1748. option.type = type;
  1749. /* 添加绘制点监听 */
  1750. action.on("vertex-add", function(evt) {
  1751. self._queryLayersTackActionHandler(evt, option,
  1752. Graphic);
  1753. });
  1754. /* 添加绘制完成监听 */
  1755. action.on("draw-complete", function(evt) {
  1756. self.isDrawing = false;
  1757. self._queryLayersTackActionHandler(evt, option,
  1758. Graphic);
  1759. });
  1760. });
  1761. },
  1762. /**
  1763. * @param {Object} evt 事件
  1764. * @param {JSON} option 配置项
  1765. * @param {Object} Graphic 要素
  1766. */
  1767. _queryLayersTackActionHandler(evt, option, Graphic) {
  1768. let self = this;
  1769. let geometry, clickPoint;
  1770. if (evt.coordinates) {
  1771. /* 绘制单个点时获取的是coordinates */
  1772. let coordinates = evt.coordinates;
  1773. geometry = {
  1774. type: "point",
  1775. x: coordinates[0],
  1776. y: coordinates[1],
  1777. spatialReference: this.mapView.spatialReference
  1778. };
  1779. } else if (evt.vertices) {
  1780. var vertices = evt.vertices;
  1781. clickPoint = {
  1782. type: "point",
  1783. x: vertices[vertices.length - 1][0],
  1784. y: vertices[vertices.length - 1][1],
  1785. spatialReference: this.mapView.spatialReference
  1786. };
  1787. var type = option.type;
  1788. geometry = {
  1789. spatialReference: this.mapView.spatialReference
  1790. };
  1791. if (type === 'polyline') {
  1792. geometry.paths = vertices;
  1793. geometry.type = "polyline";
  1794. } else {
  1795. geometry.rings = vertices;
  1796. geometry.type = "polygon";
  1797. }
  1798. }
  1799. //如果不存在则创建 否则更新
  1800. if (!this.queryTaskGraphic) {
  1801. this.queryTaskGraphic = new Graphic({
  1802. geometry: geometry,
  1803. symbol: option.symbol
  1804. });
  1805. this.graphicsLayer.add(this.queryTaskGraphic);
  1806. } else {
  1807. this.queryTaskGraphic.geometry = geometry;
  1808. }
  1809. //追加点击点
  1810. var graphicPoint = new Graphic({
  1811. geometry: clickPoint,
  1812. symbol: this.pointSymbol
  1813. });
  1814. this.graphicsLayer.add(graphicPoint);
  1815. var mGeometry = this.queryTaskGraphic.geometry; //获取绘制的要素
  1816. /* 判断查询方式 */
  1817. if (option.type === 'point') {
  1818. if (self.onQueryPointComplete) self.onQueryPointComplete(mGeometry);
  1819. self.queryTaskGraphic = undefined;
  1820. } else if (option.type === 'polygon') {
  1821. //绘制的是查询面 且点数大于等于3
  1822. if (mGeometry.type == 'polygon' && mGeometry.rings[0].length >= 3) {
  1823. if (self.onQueryPolygonComplete) onQueryPolygonComplete(3, mGeometry);
  1824. self.queryTaskGraphic = undefined;
  1825. }
  1826. }
  1827. },
  1828. /**
  1829. * 根据空间要素进行查询
  1830. * @param {Object} geometry 参考要素
  1831. */
  1832. _queryLayersTaskFromGeometry: function(geometry) {
  1833. let self = this;
  1834. require([
  1835. "esri/rest/support/IdentifyParameters",
  1836. "esri/rest/identify",
  1837. "esri/geometry/SpatialReference",
  1838. "esri/geometry/Polygon",
  1839. "esri/geometry/Polyline",
  1840. "esri/geometry/Point",
  1841. ],
  1842. function(IdentifyParameters, identify, SpatialReference, Polygon,
  1843. Polyline, Point) {
  1844. /* 定义查询地址 */
  1845. let url = self.queryURL;
  1846. /* 组织查询的Id数组 */
  1847. let queryIds = [];
  1848. for (let i in self.queryLayers) {
  1849. queryIds.push(parseInt(self.queryLayers[i].id));
  1850. }
  1851. /* 定义查询参数 */
  1852. let parameters = new IdentifyParameters({
  1853. tolerance: 3,
  1854. layerIds: queryIds,
  1855. geometry: geometry,
  1856. mapExtent: self.mapView.extent,
  1857. returnGeometry: true,
  1858. layerOption: 'all',
  1859. })
  1860. /* 结果集合 */
  1861. let results = [];
  1862. /* 开始查询之前传递个事件出去,显示等待框 */
  1863. if (self.onQueryLayersTaskBegin) self.onQueryLayersTaskBegin();
  1864. /* 打印测试 */
  1865. // console.log("查询图层 = " + JSON.stringify(self.queryLayers));
  1866. // console.log("查询图层Id = " + JSON.stringify(queryIds));
  1867. /* 开始查询 */
  1868. identify.identify(url, parameters).then(function(response) {
  1869. // console.log("成功");
  1870. /* 对数据进行组合 */
  1871. for (let i in response.results) {
  1872. let item = response.results[i];
  1873. /* 创建要素 */
  1874. let geometry = undefined;
  1875. let geometryType = item.feature.geometry.type;
  1876. if (geometryType === 'polygon') {
  1877. geometry = new Polygon({
  1878. spatialReference: {
  1879. wkid: item.feature.geometry
  1880. .spatialReference
  1881. .wikd,
  1882. },
  1883. rings: item.feature.geometry.rings,
  1884. })
  1885. } else if (geometryType === 'polyline') {
  1886. geometry = new Polyline({
  1887. spatialReference: {
  1888. wkid: item.feature.geometry
  1889. .spatialReference
  1890. .wikd,
  1891. },
  1892. paths: item.feature.geometry.paths,
  1893. })
  1894. } else if (geometryType === 'point') {
  1895. geometry = new Polyline({
  1896. spatialReference: {
  1897. wkid: item.feature.geometry
  1898. .spatialReference
  1899. .wikd,
  1900. },
  1901. x: item.feature.geometry.x,
  1902. y: item.feature.geometry.y,
  1903. })
  1904. }
  1905. /* 返回的要素不符合要求 则进入下一次循环 */
  1906. if (geometry == undefined) continue;
  1907. let extent = geometry.extent;
  1908. /* 过滤数组 */
  1909. let layerConfigs = self.queryLayers.filter(function(
  1910. p) {
  1911. return p.eName.toUpperCase() === item
  1912. .layerName;
  1913. });
  1914. let layerConfig = layerConfigs[0];
  1915. /* 创建结果要素 */
  1916. let resultFeature = {
  1917. fId: item.feature.attributes['OBJECTID'],
  1918. sValue: item.feature.attributes[layerConfig
  1919. .fldName
  1920. .toUpperCase()],
  1921. xmin: extent.xmin,
  1922. ymin: extent.ymin,
  1923. xmax: extent.xmax,
  1924. ymax: extent.ymax,
  1925. xcenter: extent.center.x,
  1926. ycenter: extent.center.y,
  1927. // attribute: item.feature.attributes,
  1928. }
  1929. /* 结果集中是否已经存在 */
  1930. let isExists = false;
  1931. for (let rIdx in results) {
  1932. let result = results[rIdx];
  1933. if (result.eName == item.layerName) {
  1934. result.features.push(resultFeature)
  1935. isExists = true;
  1936. break;
  1937. }
  1938. }
  1939. /* 如果不存在 */
  1940. if (!isExists) {
  1941. results.push({
  1942. eName: item.layerName,
  1943. cName: layerConfig.cName,
  1944. features: [resultFeature]
  1945. })
  1946. }
  1947. }
  1948. /* 回调返回结果 */
  1949. if (self.onQueryLayersTaskComplete) self
  1950. .onQueryLayersTaskComplete(
  1951. results);
  1952. });
  1953. }
  1954. );
  1955. },
  1956. })
  1957. /**+++++++++++++++测量相关+++++++++++++++++++++++*/
  1958. JTMapKit.prototype.extend({
  1959. /**
  1960. * @param {string} type 测量类型 point/polyline/polygon
  1961. * @param {JSON} option 配置项
  1962. */
  1963. _measure: function(type, option) {
  1964. var self = this;
  1965. //清除已绘制内容
  1966. self.clearGraphic();
  1967. self.isDrawing = true;
  1968. require([
  1969. "esri/views/draw/Draw",
  1970. "esri/layers/GraphicsLayer",
  1971. "esri/Graphic"
  1972. ],
  1973. function(Draw, GraphicsLayer, Graphic) {
  1974. /* 判断绘制图层是否存在 如果不存在 则创建 并加入到地图中 */
  1975. if (!self.graphicsLayer) {
  1976. self.graphicsLayer = new GraphicsLayer();
  1977. self.map.layers.add(self.graphicsLayer);
  1978. }
  1979. /* 判断绘制工具是否已经实例化 如果未实例化则创建 否则重置 */
  1980. if (!self.draw) {
  1981. self.draw = new Draw({
  1982. view: self.mapView
  1983. });
  1984. } else {
  1985. self.draw.reset();
  1986. }
  1987. /* 未绘制工具创建动作 因为reset会清除已添加的动作 */
  1988. action = self.draw.create(type, {
  1989. mode: 'click'
  1990. });
  1991. option.type = type;
  1992. //添加点
  1993. action.on("vertex-add", function(evt) {
  1994. self._measureActionHandler(evt, option, Graphic);
  1995. });
  1996. //绘制结束
  1997. action.on("draw-complete", function(evt) {
  1998. self.isDrawing = false;
  1999. self._measureActionHandler(evt, option, Graphic);
  2000. });
  2001. //删除点
  2002. action.on("vertex-remove", function(evt) {
  2003. self._measureActionHandler(evt, option, Graphic);
  2004. });
  2005. });
  2006. },
  2007. /**
  2008. * 处理绘制事件
  2009. * @param {JSON} evt 事件
  2010. * @param {JSON} option 配置项
  2011. * @param {Object} Graphic 要素类原型
  2012. */
  2013. _measureActionHandler(evt, option, Graphic) {
  2014. var geometry, clickPoint;
  2015. if (evt.coordinates) { //绘制单个点时获取的是coordinates
  2016. var coordinates = evt.coordinates;
  2017. clickPoint = geometry = {
  2018. type: "point",
  2019. x: coordinates[0],
  2020. y: coordinates[1],
  2021. spatialReference: this.mapView.spatialReference
  2022. };
  2023. } else if (evt.vertices) {
  2024. var vertices = evt.vertices;
  2025. var type = option.type;
  2026. //创建点击点 以便实现效果
  2027. clickPoint = {
  2028. type: "point",
  2029. x: vertices[vertices.length - 1][0],
  2030. y: vertices[vertices.length - 1][1],
  2031. spatialReference: this.mapView.spatialReference
  2032. };
  2033. geometry = {
  2034. spatialReference: this.mapView.spatialReference
  2035. };
  2036. if (type === 'polyline') {
  2037. geometry.paths = vertices;
  2038. geometry.type = "polyline";
  2039. } else {
  2040. geometry.rings = vertices;
  2041. geometry.type = "polygon";
  2042. }
  2043. }
  2044. if (!this.measureGraphic) {
  2045. this.measureGraphic = new Graphic({
  2046. geometry: geometry,
  2047. symbol: option.symbol
  2048. });
  2049. this.graphicsLayer.add(this.measureGraphic);
  2050. } else {
  2051. this.measureGraphic.geometry = geometry;
  2052. }
  2053. //追加点击点
  2054. var graphicPoint = new Graphic({
  2055. geometry: clickPoint,
  2056. symbol: this.pointSymbol
  2057. });
  2058. this.graphicsLayer.add(graphicPoint);
  2059. /* 判断是否需要计算并返回结果 */
  2060. var self = this;
  2061. var mGeometry = this.measureGraphic.geometry; //获取绘制的要素
  2062. if (mGeometry.type == "polyline") {
  2063. if (mGeometry.paths[0].length >= 2) {
  2064. this._measureLengthOrAreaOrCoord(function(lastLength, allLength) {
  2065. self._measureAddText(lastLength, clickPoint);
  2066. // self._showMeasureResultPopup("长度测量结果", "当前测量总长:" + allLength, clickPoint);
  2067. if (self.onMeasureEventComplete != undefined) {
  2068. self.onMeasureEventComplete('length', "当前测量总长:" +
  2069. allLength);
  2070. }
  2071. });
  2072. } else {
  2073. this._measureAddText('起点', clickPoint);
  2074. }
  2075. } else if (mGeometry.type == "polygon") {
  2076. if (mGeometry.rings[0].length >= 3) {
  2077. this._measureLengthOrAreaOrCoord(function(result) {
  2078. // self._showMeasureResultPopup("面积测量结果", result, clickPoint);
  2079. if (self.onMeasureEventComplete != undefined) {
  2080. self.onMeasureEventComplete('area', result);
  2081. }
  2082. });
  2083. } else if (mGeometry.rings[0].length == 1) {
  2084. this._measureAddText('起点', clickPoint);
  2085. }
  2086. } else if (mGeometry.type == "point") {
  2087. this._measureLengthOrAreaOrCoord(function(result) {
  2088. // self._showMeasureResultPopup("当前位置坐标", result, clickPoint);
  2089. if (self.onMeasureEventComplete != undefined) {
  2090. self.onMeasureEventComplete('coord', result);
  2091. }
  2092. });
  2093. }
  2094. },
  2095. /**
  2096. * 添加文字
  2097. * @param {string} text
  2098. * @param {EsriPoint} location 文字位置
  2099. */
  2100. _measureAddText: function(text, location) {
  2101. var self = this;
  2102. require(["esri/Graphic"], function(Graphic) {
  2103. /* 创建文字符号 */
  2104. // var textSymbol = {
  2105. // type: 'text',
  2106. // color: "#FFFFFF",
  2107. // haloColor: "#555555",
  2108. // haloSize: "2px",
  2109. // text: text,
  2110. // xoffset: 0,
  2111. // yoffset: -20,
  2112. // font: {
  2113. // size: 10,
  2114. // family: "Playfair Display",
  2115. // weight: "bold"
  2116. // }
  2117. // };
  2118. self.textSymbol.text = text;
  2119. /* 创建文字要素 */
  2120. var txtGraphic = new Graphic({
  2121. geometry: location,
  2122. symbol: self.textSymbol
  2123. });
  2124. /* 将文字要素添加到绘制图层 */
  2125. self.graphicsLayer.add(txtGraphic);
  2126. });
  2127. },
  2128. /**
  2129. * 转换线的长度
  2130. * @param {float} lineLength 线的长度
  2131. */
  2132. _measureTrasformLineLength: function(lineLength) {
  2133. var len = parseFloat(lineLength);
  2134. var lenResult = "";
  2135. if (len < 1000) {
  2136. lenResult = len.toFixed(2) + "米";
  2137. } else {
  2138. lenResult = (len / 1000).toFixed(2) + "公里";
  2139. }
  2140. return lenResult;
  2141. },
  2142. /**
  2143. * 根据绘制图层绘制的要素类型 计算坐标、长度或者面积
  2144. * @param {function} callback 计算完成回调
  2145. */
  2146. _measureLengthOrAreaOrCoord: function(callback) {
  2147. var self = this;
  2148. require([
  2149. "esri/geometry/support/geodesicUtils",
  2150. "esri/geometry/projection",
  2151. "esri/geometry/SpatialReference",
  2152. "esri/geometry/geometryEngine",
  2153. "esri/geometry/geometryEngineAsync"
  2154. ],
  2155. function(geodesicUtils, projection, SpatialReference, geometryEngine,
  2156. geometryEngineAsync) {
  2157. //设置要素的投影
  2158. self.measureGraphic.geometry.spatialReference = self.mapView
  2159. .spatialReference;
  2160. //定义坐标系
  2161. var outSpatialReference = new SpatialReference({
  2162. wkid: 4326
  2163. });
  2164. var mGeometry = self.measureGraphic.geometry;
  2165. if (mGeometry.type == "polyline") {
  2166. //计算最后一段长度
  2167. var pt1 = mGeometry.getPoint(0, mGeometry.paths[0].length - 1);
  2168. var pt2 = mGeometry.getPoint(0, mGeometry.paths[0].length - 2);
  2169. //创建要素
  2170. var lastGeometry = {
  2171. spatialReference: self.mapView.spatialReference,
  2172. type: "polyline",
  2173. paths: [
  2174. [
  2175. [pt1.x, pt1.y],
  2176. [pt2.x, pt2.y]
  2177. ]
  2178. ],
  2179. };
  2180. lastGeometry.spatialReference = self.mapView.spatialReference;
  2181. //开始异步计算长度
  2182. geometryEngineAsync.geodesicLength(lastGeometry, "meters").then(
  2183. function(
  2184. lastResult) {
  2185. geometryEngineAsync.geodesicLength(mGeometry,
  2186. "meters").then(
  2187. function(
  2188. result) {
  2189. var lenLastResult = self
  2190. ._measureTrasformLineLength(
  2191. lastResult);
  2192. var lenAllResult = self
  2193. ._measureTrasformLineLength(
  2194. result);
  2195. if (callback != undefined) callback(
  2196. lenLastResult,
  2197. lenAllResult);
  2198. })
  2199. });
  2200. } else if (mGeometry.type == "polygon") {
  2201. var result = geometryEngine.geodesicArea(mGeometry,
  2202. "square-meters");
  2203. var area = Math.abs(parseFloat(result));
  2204. //如果初次计算的面积小于0 说明坐标串是反序的 将其反过来 重新计算
  2205. if (area < 0) {
  2206. // mGeometry.rings[0].reverse();
  2207. // alert(mGeometry.rings[0]);
  2208. // result = geometryEngine.geodesicArea(mGeometry, "square-meters");
  2209. // area = parseFloat(result[0]);
  2210. }
  2211. var areaMu = area * 0.0015;
  2212. var areaResult = "";
  2213. if (area < 1000000) {
  2214. areaResult = area.toFixed(3) + "平方米 " + areaMu.toFixed(3) +
  2215. "亩";
  2216. } else {
  2217. areaResult = (area / 1000000).toFixed(3) + "平方公里 " + areaMu
  2218. .toFixed(3) + "亩";
  2219. }
  2220. if (callback != undefined) callback(areaResult);
  2221. } else if (mGeometry.type == "point") {
  2222. //对坐标进行转换
  2223. projection.load().then(function() {
  2224. var geometry = projection.project(mGeometry,
  2225. outSpatialReference);
  2226. var coordResult = "经度:" + geometry.x.toFixed(6) +
  2227. " 纬度:" +
  2228. geometry.y
  2229. .toFixed(6);
  2230. if (callback != undefined) callback(coordResult);
  2231. });
  2232. }
  2233. }
  2234. );
  2235. },
  2236. /**
  2237. * 显示测量结果
  2238. * @param {string} title 标题
  2239. * @param {string} content 测量结果内容
  2240. * @param {ESRIPoint} location 测量结果内容
  2241. */
  2242. _showMeasureResultPopup: function(title, content, location) {
  2243. /* 显示弹窗试试 */
  2244. var popup = this.mapView.popup;
  2245. /* 显示位置 */
  2246. popup.location = location;
  2247. /* 设置标题 */
  2248. popup.title = title;
  2249. /* 设置显示内容 */
  2250. popup.viewModel.content = content;
  2251. /* 控制窗口的停靠按钮是否显示 */
  2252. popup.dockOptions = {
  2253. buttonEnabled: false,
  2254. }
  2255. /* 控制是否显示关闭按钮 */
  2256. popup.visibleElements = {
  2257. closeButton: true,
  2258. featureNavigation: false
  2259. }
  2260. /* 关闭自动缩放按钮 */
  2261. popup.viewModel.includeDefaultActions = false;
  2262. /* 控制是否������������������������ */
  2263. popup.visible = true;
  2264. },
  2265. //测量长度
  2266. measureLength: function(option) {
  2267. var options = option || {};
  2268. if (!options.symbol) {
  2269. options.symbol = this.lineSymbol;
  2270. }
  2271. this._measure('polyline', options);
  2272. },
  2273. //测量面积
  2274. measureArea: function(option) {
  2275. var options = option || {};
  2276. if (!options.symbol) {
  2277. options.symbol = this.fillSymbol;
  2278. }
  2279. this._measure('polygon', options);
  2280. },
  2281. //测量坐标
  2282. measureCoordinate: function(option) {
  2283. var options = option || {};
  2284. if (!options.symbol) {
  2285. options.symbol = this.pointSymbol;
  2286. }
  2287. this._measure('point', options);
  2288. }
  2289. })
  2290. /**+++++++++++++++图层操作相关+++++++++++++++++++++++*/
  2291. JTMapKit.prototype.extend({
  2292. /**
  2293. * 根据���层名称设置其可见性
  2294. * @param {any} layerName 图层名称
  2295. * @param {any} isVisible 是否可见
  2296. */
  2297. _setLayerIsVisibleByName: function(layerName, isVisible) {
  2298. this.map.layers.forEach(function(layer, idx) {
  2299. if (layer.title && layer.title.toLocaleLowerCase() == layerName
  2300. .toLocaleLowerCase()) {
  2301. layer.visible = isVisible;
  2302. return idx;
  2303. }
  2304. });
  2305. },
  2306. /**
  2307. * 根据图层名称查找图层
  2308. * @param {string} layerName
  2309. */
  2310. findLayer: function(layerName) {
  2311. let findLayer = this.map.allLayers.find(function(layer) {
  2312. return layer.title === layerName;
  2313. });
  2314. if (findLayer) return findLayer;
  2315. else return undefined;
  2316. },
  2317. /**
  2318. * 删除指定名称的图层
  2319. * @param {string} layerName 图层名称
  2320. */
  2321. removeLayer: function(layerName, callSuccess, callError) {
  2322. let _self = this;
  2323. let findLayer = this.map.allLayers.find(function(layer) {
  2324. return layer.title === layerName;
  2325. })
  2326. if (findLayer) {
  2327. this.map.layers.remove(findLayer);
  2328. if (callSuccess) callSuccess();
  2329. } else {
  2330. if (callError) callError("名称为" + layerName +
  2331. "的图层不存在,无法完成删除!"
  2332. );
  2333. }
  2334. },
  2335. /**
  2336. * 设�����图层可见及不可见
  2337. * @param {any} visibleLayerNames
  2338. * @param {any} noVisibleLayerNames
  2339. */
  2340. setVisibleByNames: function(visibleLayerNames, noVisibleLayerNames) {
  2341. var self = this;
  2342. for (var i = 0; i < self.loadLayers.length; i++) {
  2343. var layer = self.loadLayers[i];
  2344. if (visibleLayerNames.indexOf(layer.title) != -1) {
  2345. layer.slayer.findSublayerById(layer.id).visible = true;
  2346. }
  2347. if (noVisibleLayerNames.indexOf(layer.title) != -1) {
  2348. layer.slayer.findSublayerById(layer.id).visible = false;
  2349. }
  2350. }
  2351. },
  2352. /** 设置单个图层可见或隐藏
  2353. * @param {string} layerName 图层名称
  2354. * @param {boolean} visible 显示或隐藏
  2355. */
  2356. setVisibleByName: function(layerName, visible) {
  2357. this._setLayerIsVisibleByName(layerName, visible);
  2358. },
  2359. /**
  2360. * 设置图层标注是否可见
  2361. * @param {string} layerName 图层名称
  2362. * @param {boolean} visible 标注是否可见
  2363. */
  2364. setLabelVisibleByName: function(layerName, visible) {
  2365. this.map.layers.forEach(function(layer, idx) {
  2366. if (layer.title.toLocaleLowerCase() == layerName
  2367. .toLocaleLowerCase()) {
  2368. layer.sublayers.find(function(subLayer) {
  2369. subLayer.labelsVisible = visible;
  2370. });
  2371. }
  2372. });
  2373. },
  2374. });
  2375. /**++++++++++++其他相关内内容++++++++++++++++++++++*/
  2376. JTMapKit.prototype.extend({
  2377. /**
  2378. * 初始化
  2379. * @param {function} callback 成功回调
  2380. */
  2381. DeleteTempDraw: function(callback) {
  2382. this.clearGraphic();
  2383. this.currentActionType = this.actionType.unknown; //初始化状态
  2384. if (callback) callback();
  2385. },
  2386. /**
  2387. * 地图输出
  2388. * @param {any} title 标题
  2389. * @param {any} layout 模式 A3���式 A3竖式 A4横式 A4竖式
  2390. * @param {any} restype 返回���式
  2391. */
  2392. PrintMap: function(title, layout, restype) {
  2393. var self = this;
  2394. var scale = self.mapView.scale > 1000000 ? 3000 : self.mapView.scale;
  2395. var sLayout = layout;
  2396. if (layout == "A3横向") {
  2397. sLayout = "a3-landscape";
  2398. } else if (layout == "A3竖向") {
  2399. sLayout = "a3-portrait";
  2400. } else if (layout == "A4横向") {
  2401. sLayout = "a4-landscape";
  2402. } else if (layout == "A4竖向") {
  2403. sLayout = "a4-portrait";
  2404. }
  2405. require([
  2406. "esri/tasks/PrintTask",
  2407. "esri/tasks/support/PrintParameters",
  2408. "esri/tasks/support/PrintTemplate"
  2409. ],
  2410. function(PrintTask, PrintParameters, PrintTemplate) {
  2411. //此方法是为了关闭标注层 否则打印的地图将出现黑色
  2412. self._setLayerIsVisibleByName("天地图���注", false);
  2413. var printTask = new PrintTask({
  2414. url: "http://218.59.194.74:6080/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task",
  2415. });
  2416. //打印模板
  2417. var template = new PrintTemplate({
  2418. format: restype,
  2419. exportOptions: {
  2420. dpi: 100,
  2421. width: self.mapView.width,
  2422. height: self.mapView.height
  2423. },
  2424. layout: sLayout,
  2425. layoutOptions: {
  2426. titleText: title,
  2427. authorText: ""
  2428. },
  2429. outScale: scale,
  2430. });
  2431. //打印参数
  2432. var params = new PrintParameters({
  2433. view: self.mapView,
  2434. template: template
  2435. });
  2436. if (self.onPrintEventProgress) {
  2437. self.onPrintEventProgress(false);
  2438. }
  2439. //执行输出地图
  2440. printTask.execute(params).then(function(result) {
  2441. self._setLayerIsVisibleByName("天地图标注", true);
  2442. if (self.onPrintEventProgress) self
  2443. .onPrintEventProgress(true);
  2444. if (self.onPrintEventComplete) {
  2445. self.onPrintEventComplete(result.url);
  2446. }
  2447. }, function(error) {
  2448. self._setLayerIsVisibleByName("天地图标注", true);
  2449. if (self.onPrintEventProgress) self
  2450. .onPrintEventProgress(true);
  2451. if (self.onEventError) {
  2452. self.onEventError(error);
  2453. }
  2454. });
  2455. }
  2456. );
  2457. },
  2458. /**
  2459. * 向地图中添加点
  2460. * @param {any} x
  2461. * @param {any} y
  2462. */
  2463. AddPointToMap: function(x, y) {
  2464. var self = this;
  2465. require([
  2466. "esri/symbols/PictureMarkerSymbol",
  2467. "esri/geometry/Point",
  2468. "esri/geometry/SpatialReference",
  2469. "esri/layers/GraphicsLayer",
  2470. "esri/Graphic",
  2471. "esri/geometry/geometryEngine"
  2472. ],
  2473. function(PictureMarkerSymbol, Point, SpatialReference, GraphicsLayer,
  2474. Graphic,
  2475. geometryEngine) {
  2476. if (!self.graphicsLayer) {
  2477. self.graphicsLayer = new GraphicsLayer();
  2478. self.map.layers.add(self.graphicsLayer);
  2479. }
  2480. //定义坐标系统
  2481. var spa = new SpatialReference({
  2482. wkid: 4326,
  2483. });
  2484. //定义符号
  2485. var ptSymbol = new PictureMarkerSymbol({
  2486. url: self.serverUrl + "location02.png",
  2487. width: "20px",
  2488. height: "20px",
  2489. xoffset: 0,
  2490. yoffset: 7,
  2491. });
  2492. //定义点
  2493. var pt = new Point({
  2494. x: x,
  2495. y: y,
  2496. spatialReference: spa,
  2497. });
  2498. //定义要素
  2499. var graphicPt = new Graphic({
  2500. geometry: pt,
  2501. symbol: ptSymbol,
  2502. });
  2503. //添加要素
  2504. self.graphicsLayer.add(graphicPt);
  2505. //超�����范围则重新定���地图中���
  2506. if (!geometryEngine.intersects(pt, self.mapView.extent)) {
  2507. self.mapView.center = [x, y];
  2508. }
  2509. }
  2510. );
  2511. },
  2512. /**
  2513. * 将点数组追加到地图中
  2514. * @param {any} points
  2515. */
  2516. AddPointsToMap: function(points) {
  2517. var self = this;
  2518. require([
  2519. "esri/symbols/SimpleLineSymbol",
  2520. "esri/symbols/SimpleMarkerSymbol",
  2521. "esri/Color",
  2522. "esri/geometry/Polyline",
  2523. "esri/geometry/Point",
  2524. "esri/geometry/SpatialReference",
  2525. "esri/layers/GraphicsLayer",
  2526. "esri/Graphic"
  2527. ],
  2528. function(SimpleLineSymbol, SimpleMarkerSymbol, Color, Polyline, Point,
  2529. SpatialReference,
  2530. GraphicsLayer, Graphic) {
  2531. if (!self.graphicsLayer) {
  2532. self.graphicsLayer = new GraphicsLayer();
  2533. self.map.layers.add(self.graphicsLayer);
  2534. }
  2535. //定义坐标系统
  2536. var spa = new SpatialReference({
  2537. wkid: 4326,
  2538. });
  2539. //定义颜色
  2540. var r = Math.floor(Math.random() * 200);;
  2541. var g = Math.floor(Math.random() * 200);
  2542. var b = Math.floor(Math.random() * 200);
  2543. var rgb = [r, g, b];
  2544. var ptColor = new Color([34, 139, 34, 1]);
  2545. var lineColorBack = new Color([255, 255, 255]);
  2546. var lineColor = new Color(rgb);
  2547. //定义点样式
  2548. var ptSymbol = new SimpleMarkerSymbol({
  2549. style: "circle",
  2550. color: ptColor,
  2551. size: "8px", // pixels
  2552. });
  2553. //定义线样式
  2554. var lineSymbolBack = new SimpleLineSymbol({
  2555. color: lineColorBack,
  2556. width: "5px",
  2557. style: "solid"
  2558. });
  2559. //定义线样式
  2560. var lineSymbol = new SimpleLineSymbol({
  2561. color: lineColor,
  2562. width: "2px",
  2563. style: "solid"
  2564. });
  2565. //加载数据
  2566. var paths = [];
  2567. var pointStart, pointEnd;
  2568. for (var i = 0; i < points.length; i++) {
  2569. if (i == 0) {
  2570. pointStart = new Point({
  2571. x: points[i].x,
  2572. y: points[i].y,
  2573. spatialReference: spa,
  2574. });
  2575. }
  2576. if (i == points.length - 1) {
  2577. pointEnd = new Point({
  2578. x: points[i].x,
  2579. y: points[i].y,
  2580. spatialReference: spa,
  2581. });
  2582. }
  2583. paths.push([points[i].x, points[i].y]);
  2584. }
  2585. //创建线
  2586. var lineBack = new Polyline({
  2587. paths: paths,
  2588. spatialReference: spa,
  2589. });
  2590. var line = new Polyline({
  2591. paths: paths,
  2592. spatialReference: spa,
  2593. });
  2594. //创建要素
  2595. var graphicBack = new Graphic({
  2596. geometry: lineBack,
  2597. symbol: lineSymbolBack,
  2598. });
  2599. var graphic = new Graphic({
  2600. geometry: line,
  2601. symbol: lineSymbol,
  2602. });
  2603. var graphicStr = new Graphic({
  2604. geometry: pointStart,
  2605. symbol: ptSymbol,
  2606. });
  2607. var graphicEnd = new Graphic({
  2608. geometry: pointEnd,
  2609. symbol: ptSymbol,
  2610. });
  2611. //添加要素
  2612. self.graphicsLayer.add(graphicBack);
  2613. self.graphicsLayer.add(graphic);
  2614. self.graphicsLayer.add(graphicStr);
  2615. self.graphicsLayer.add(graphicEnd);
  2616. //定位范围
  2617. self.mapView.extent = line.extent;
  2618. }
  2619. );
  2620. },
  2621. /**
  2622. * 将polygon添加到地图
  2623. * @param {JSON} options
  2624. * points{Array}:[{longitude,latitude},{longitude,latitude},...]
  2625. * lineColor{Array}:[r,g,b,a]
  2626. * fillColor{Array}:[r,g,b,a]
  2627. * success{Function}: 回调
  2628. */
  2629. AddPolygonToMap: function(options) {
  2630. var self = this;
  2631. require([
  2632. "esri/symbols/SimpleLineSymbol",
  2633. "esri/symbols/SimpleFillSymbol",
  2634. "esri/Color",
  2635. "esri/geometry/Polygon",
  2636. "esri/geometry/SpatialReference",
  2637. "esri/layers/GraphicsLayer",
  2638. "esri/Graphic"
  2639. ],
  2640. function(SimpleLineSymbol, SimpleFillSymbol, Color, Polygon,
  2641. SpatialReference,
  2642. GraphicsLayer, Graphic) {
  2643. if (!self.graphicsLayer) {
  2644. self.graphicsLayer = new GraphicsLayer();
  2645. self.map.layers.add(self.graphicsLayer);
  2646. }
  2647. //定义坐标系统
  2648. var spa = new SpatialReference({
  2649. wkid: 4326,
  2650. });
  2651. //定义颜色
  2652. var fillColor = options.fillColor == undefined ? new Color([0, 0,
  2653. 255, 0.3
  2654. ]) :
  2655. options
  2656. .fillColor;
  2657. var lineColor = options.lineColor == undefined ? new Color([255, 0,
  2658. 0, 0.5
  2659. ]) :
  2660. options
  2661. .lineColor;
  2662. //定义线样式
  2663. var lineSymbol = new SimpleLineSymbol({
  2664. color: lineColor,
  2665. width: "2px",
  2666. style: "solid"
  2667. });
  2668. //定义面样式
  2669. var fullSymbol = new SimpleFillSymbol({
  2670. color: fillColor,
  2671. style: "solid",
  2672. outline: lineSymbol,
  2673. });
  2674. //构建数据
  2675. var rings = [
  2676. []
  2677. ];
  2678. for (var i in options.points) {
  2679. var point = options.points[i]
  2680. rings[0].push([point.longitude, point.latitude]);
  2681. }
  2682. //创建区域
  2683. var polygon = new Polygon({
  2684. rings: rings,
  2685. spatialReference: spa,
  2686. });
  2687. //创建要素
  2688. var graphicPolygon = new Graphic({
  2689. geometry: polygon,
  2690. symbol: fullSymbol,
  2691. });
  2692. //添加要素
  2693. self.graphicsLayer.add(graphicPolygon);
  2694. if (options.success != undefined) options.success(graphicPolygon);
  2695. //定位范围
  2696. self.mapView.extent = graphicPolygon.extent;
  2697. }
  2698. );
  2699. },
  2700. /**
  2701. * 删除绘制的临时要素
  2702. * @param {Object} graphic 绘制的要素
  2703. */
  2704. removeGrahpic: function(graphic) {
  2705. if (graphic != undefined) {
  2706. if (this.graphicsLayer) {
  2707. this.graphicsLayer.remove(graphic);
  2708. }
  2709. }
  2710. },
  2711. /**
  2712. * 删除全部绘制的临时要素
  2713. */
  2714. removeAllGraphic: function() {
  2715. if (this.graphicsLayer) {
  2716. this.graphicsLayer.removeAll();
  2717. }
  2718. },
  2719. /**
  2720. * 绘制Polygon,MultPolygon到临时图层
  2721. * @param {JSON} options 配置项
  2722. * options.lineColor{array}:[r,g,b,a]
  2723. * options.fillColor{Array}:[r,g,b,a]
  2724. * options.type{string}:绘制的区域类型 Polygon/MultiPolygon
  2725. * options.coordinates{array}:绘制区域的坐标串
  2726. */
  2727. appendPolygonToMap: function(options) {
  2728. var _self = this;
  2729. require([
  2730. "esri/symbols/SimpleLineSymbol",
  2731. "esri/symbols/SimpleFillSymbol",
  2732. "esri/Color",
  2733. "esri/geometry/Polygon",
  2734. "esri/geometry/SpatialReference",
  2735. "esri/layers/GraphicsLayer",
  2736. "esri/Graphic"
  2737. ],
  2738. function(SimpleLineSymbol, SimpleFillSymbol, Color, Polygon,
  2739. SpatialReference,
  2740. GraphicsLayer, Graphic) {
  2741. /* 判断临时绘制图层是否存在 不存在则创建 */
  2742. if (!_self.graphicsLayer) {
  2743. _self.graphicsLayer = new GraphicsLayer();
  2744. _self.map.layers.add(_self.graphicsLayer);
  2745. } else {
  2746. _self.graphicsLayer.removeAll();
  2747. }
  2748. /* 定义绘制要素的坐标系统 */
  2749. let spa = new SpatialReference({
  2750. wkid: 4326,
  2751. });
  2752. /* 定义颜色 */
  2753. let fillColor = options.fillColor == undefined ? new Color([0, 0,
  2754. 255, 0.2
  2755. ]) :
  2756. options
  2757. .fillColor;
  2758. let lineColor = options.lineColor == undefined ? new Color([255, 255,
  2759. 255, 0.8
  2760. ]) :
  2761. options
  2762. .lineColor;
  2763. /* 定义符号样式 */
  2764. var lineSymbol = new SimpleLineSymbol({
  2765. color: lineColor,
  2766. width: "2px",
  2767. style: "short-dash-dot-dot"
  2768. });
  2769. let fullSymbol = new SimpleFillSymbol({
  2770. color: fillColor,
  2771. style: "solid",
  2772. outline: lineSymbol,
  2773. });
  2774. /* 绘制的要素 */
  2775. let graphics = [];
  2776. /* 根据类型进行绘制 */
  2777. if (options.type === 'Polygon') {
  2778. /* 创建区域 */
  2779. let polygon = new Polygon({
  2780. rings: options.coordinates,
  2781. spatialReference: spa,
  2782. });
  2783. /* 绘制区域 */
  2784. let graphicPolygon = new Graphic({
  2785. geometry: polygon,
  2786. symbol: fullSymbol,
  2787. });
  2788. /* 添加要素 */
  2789. graphics.push(graphicPolygon);
  2790. } else if (options.type === 'MultiPolygon') {
  2791. for (rings of options.coordinates) {
  2792. /* 创建区域 */
  2793. let polygon = new Polygon({
  2794. rings: rings,
  2795. spatialReference: spa,
  2796. });
  2797. /* 绘制区域 */
  2798. let graphicPolygon = new Graphic({
  2799. geometry: polygon,
  2800. symbol: fullSymbol,
  2801. });
  2802. /* 添加要素 */
  2803. graphics.push(graphicPolygon);
  2804. }
  2805. }
  2806. /* 绘制 */
  2807. _self.graphicsLayer.addMany(graphics);
  2808. /* 缩放至位置 */
  2809. // _self.mapView.extent = graphics[0].geometry.extent;
  2810. // _self.mapView.zoom = _self.mapView.zoom - 1;
  2811. _self.mapView.goTo(graphics[0].geometry.extent, {
  2812. duration: 500
  2813. }).then(function() {
  2814. _self.mapView.zoom = _self.mapView.zoom - 1;
  2815. console.log("设置地图缩放等级 = " + _self.mapView.zoom);
  2816. });
  2817. }
  2818. );
  2819. },
  2820. });
  2821. /**
  2822. * 扩展工具
  2823. */
  2824. JTMapKit.prototype.extend({
  2825. /**
  2826. * 启用草图编辑工具
  2827. * @param {JSON} options 配置项
  2828. * options.onadded{function}:添加点回调
  2829. * options.onredo{function}:恢复回调
  2830. * options.onundo{function}:撤销回调
  2831. * @param {function} callSuccess 成功回调
  2832. */
  2833. _sketchToolsInit: function(options, callSuccess) {
  2834. /* 自身替换 */
  2835. let _self = this;
  2836. require([
  2837. "esri/layers/GraphicsLayer",
  2838. "esri/widgets/Sketch/SketchViewModel",
  2839. "esri/geometry/projection",
  2840. "esri/geometry/SpatialReference"
  2841. ],
  2842. function(GraphicsLayer, SketchViewModel, projection, SpatialReference) {
  2843. /* 判断是否存在草图图层 不存在则创建并加入地图中 */
  2844. if (!_self.layerSketch) {
  2845. _self.layerSketch = new GraphicsLayer();
  2846. _self.map.add(_self.layerSketch);
  2847. }
  2848. /* 草图辅助图层 */
  2849. if (!_self.graphicsLayer) {
  2850. _self.graphicsLayer = new GraphicsLayer();
  2851. _self.map.layers.add(_self.graphicsLayer);
  2852. } else {
  2853. _self.graphicsLayer.removeAll();
  2854. }
  2855. /* 判断草图工具是否存在 不存在则创建 */
  2856. if (!_self.sketchViewModel) {
  2857. _self.sketchViewModel = new SketchViewModel({
  2858. layer: _self.layerSketch,
  2859. view: _self.mapView,
  2860. pointSymbol: _self.sketchPointSymbol,
  2861. polylineSymbol: _self.sketchLineSymbol,
  2862. polygonSymbol: _self.sketchPolygonSymbol,
  2863. });
  2864. /* 绑定创建事件 */
  2865. _self.sketchViewModel.on("create", function(event) {
  2866. if (event.state == "start") {
  2867. _self.layerSketch.removeAll(); //删除全部
  2868. }
  2869. if (event.toolEventInfo && event.toolEventInfo
  2870. .type ===
  2871. "vertex-add") {
  2872. if (options && options.onadded) options.onadded(
  2873. event
  2874. .graphic
  2875. .geometry);
  2876. }
  2877. if (event.state === 'complete') {
  2878. if (options && options.oncomplete) options
  2879. .oncomplete(event
  2880. .graphic
  2881. .geometry);
  2882. }
  2883. });
  2884. /* 绑定redo事件 */
  2885. _self.sketchViewModel.on('redo', function(event) {
  2886. if (event.graphics && options && options.onredo)
  2887. options.onredo(
  2888. event
  2889. .graphics[0]
  2890. .geometry);
  2891. });
  2892. /* 绑定undo事件 */
  2893. _self.sketchViewModel.on('undo', function(event) {
  2894. if (event.graphics && options && options.onundo)
  2895. options.onundo(
  2896. event
  2897. .graphics[0]
  2898. .geometry);
  2899. });
  2900. /* 回调 */
  2901. if (callSuccess) callSuccess();
  2902. }
  2903. });
  2904. },
  2905. /**
  2906. * 启用草图工具
  2907. * @param {string} type 工具类型
  2908. * @param {JSON} options 配置项
  2909. */
  2910. _activeSketchTools: function(type, options) {
  2911. let _self = this;
  2912. /* 如果草图编辑工具已经初始化 则释放资源 重新初始化 */
  2913. if (this.sketchViewModel) {
  2914. this.sketchViewModel.layer.graphics.removeAll();
  2915. this.sketchViewModel.destroy();
  2916. this.sketchViewModel = null;
  2917. }
  2918. /* 初始化草图工具 */
  2919. this._sketchToolsInit(options, function() {
  2920. /* 启用草图工具 */
  2921. if (type === 'point') {
  2922. _self.sketchViewModel.create('point');
  2923. } else if (type === 'line') {
  2924. _self.sketchViewModel.create('polyline');
  2925. } else if (type === 'rectangle') {
  2926. _self.sketchViewModel.create('rectangle');
  2927. } else {
  2928. _self.sketchViewModel.create('polygon');
  2929. }
  2930. });
  2931. },
  2932. /**
  2933. * 草图编辑工具完成确认
  2934. */
  2935. _sketchComplete: function() {
  2936. if (this.sketchViewModel) {
  2937. this.sketchViewModel.complete();
  2938. }
  2939. },
  2940. /**
  2941. * 启用点草图工具
  2942. * @param {JSON} options 配置项
  2943. */
  2944. activeSketchPointTools: function(options) {
  2945. this._activeSketchTools('point', options);
  2946. },
  2947. /**
  2948. * 启用线草图工具
  2949. * @param {JSON} options 配置项
  2950. */
  2951. activeSketchLineTools: function(options) {
  2952. this._activeSketchTools('line', options);
  2953. },
  2954. /**
  2955. * 启用矩形草图工具
  2956. * @param {JSON} options 配置项
  2957. */
  2958. activeSketchRectangleTools: function(options) {
  2959. this._activeSketchTools('rectangle', options);
  2960. },
  2961. /**
  2962. * 启用区域草图工具
  2963. * @param {JSON} options 配置项
  2964. */
  2965. activeSketchPolygonTools: function(options) {
  2966. this._activeSketchTools('polygon', options);
  2967. },
  2968. /**
  2969. * 草图编辑提交
  2970. */
  2971. sketchComplete: function() {
  2972. this._sketchComplete();
  2973. },
  2974. })
  2975. /**
  2976. * 草图功能扩展
  2977. */
  2978. JTMapKit.prototype.extend({
  2979. /**
  2980. * 多图层查询 点方式
  2981. * @param {JSON} options 配置项
  2982. * options.queryLayers{JSON}:查询图层参考
  2983. * options.url{string}:查询地址
  2984. * @param {function} callSuccess 成功回调
  2985. * callSuccess({})
  2986. * @param {function} callError 错误回调
  2987. * callError(string)
  2988. */
  2989. manyLayerQueryByPoint: function(options, callSuccess, callError) {
  2990. let _self = this;
  2991. _self.activeSketchPointTools({
  2992. oncomplete: function(geometry) {
  2993. _self.manyLayerQuery({
  2994. geometry: geometry,
  2995. queryLayers: options.queryLayers,
  2996. url: options.url
  2997. }, callSuccess, callError);
  2998. }
  2999. })
  3000. },
  3001. /**
  3002. * 多图层查询 绘制区域方式
  3003. * @param {JSON} options 配置项
  3004. * options.queryLayers{JSON}:查询图层参考
  3005. * options.url{string}:查询地址
  3006. * @param {function} callOperation 操作回调
  3007. * callOperation({})
  3008. * @param {function} callSuccess 成功回调
  3009. * callSuccess({})
  3010. * @param {function} callError 错误回调
  3011. * callError(string)
  3012. */
  3013. manyLayerQueryByPolygon: function(options, callOperation, callSuccess, callError) {
  3014. let _self = this;
  3015. _self.activeSketchPolygonTools({
  3016. onadded: function(geometry) {
  3017. if (callOperation) callOperation({
  3018. geometry: geometry,
  3019. })
  3020. },
  3021. oncomplete: function(geometry) {
  3022. _self.manyLayerQuery({
  3023. geometry: geometry,
  3024. queryLayers: options.queryLayers,
  3025. url: options.url
  3026. }, callSuccess, callError);
  3027. }
  3028. })
  3029. },
  3030. /**
  3031. * 多图层查询 绘制区域方式 查询确认
  3032. */
  3033. manyLayerQueryByPolygonComplete: function() {
  3034. this._sketchComplete();
  3035. },
  3036. /**
  3037. * 多图层查询
  3038. * @param {JSON} options 配置项
  3039. * options.geometry{JSON}:查询参考图形要素
  3040. * options.url{string}:查询地址
  3041. * options.queryLayers{array{JSON}}:查询图层集合
  3042. * @param {function} callSuccess 成功回调
  3043. * callSuccess({})
  3044. * @param {function} callError 错误回调
  3045. * callError(string)
  3046. */
  3047. manyLayerQuery: function(options, callSuccess, callError) {
  3048. let _self = this;
  3049. require([
  3050. "esri/rest/support/IdentifyParameters",
  3051. "esri/rest/identify"
  3052. ], function(IdentifyParameters, identify) {
  3053. /* 过滤查询的图层 */
  3054. let queryIds = [];
  3055. for (let layer of options.queryLayers) {
  3056. queryIds.push(parseInt(layer.id));
  3057. }
  3058. /* 设置查询参数 */
  3059. let params = new IdentifyParameters({
  3060. geometry: options.geometry,
  3061. mapExtent: _self.mapView.extent,
  3062. returnGeometry: true,
  3063. tolerance: 3,
  3064. layerIds: queryIds,
  3065. layerOption: 'all',
  3066. });
  3067. identify.identify(options.url, params).then(function(response) {
  3068. /* 查询结果 */
  3069. let results = [];
  3070. /* 循环查询数据集 */
  3071. for (let item of response.results) {
  3072. /* 巡查查询数据对应的配置图层 */
  3073. let layers = options.queryLayers.filter(function(
  3074. p) {
  3075. return p.eName.toUpperCase() === item
  3076. .layerName;
  3077. });
  3078. let layer = layers[0];
  3079. /* 创建结果要素 */
  3080. let resultFeature = {
  3081. fId: item.feature.attributes['OBJECTID'],
  3082. tabName: layer.eName,
  3083. sValue: item.feature.attributes[layer
  3084. .fldName
  3085. .toUpperCase()],
  3086. }
  3087. /* 结果集中是否已经存在 */
  3088. let isExists = false;
  3089. for (let result of results) {
  3090. if (result.eName == item.layerName) {
  3091. result.features.push(resultFeature)
  3092. isExists = true;
  3093. break;
  3094. }
  3095. }
  3096. /* 如果不存在 */
  3097. if (!isExists) {
  3098. results.push({
  3099. eName: item.layerName,
  3100. cName: layer.cName,
  3101. isCase: layer.isCase,
  3102. features: [resultFeature]
  3103. })
  3104. }
  3105. }
  3106. /* 返回结果 */
  3107. if (results.length === 0 && callError) callError(
  3108. "未查询到任何数据!");
  3109. if (results.length > 0 && callSuccess) callSuccess(
  3110. results);
  3111. });
  3112. });
  3113. },
  3114. /**
  3115. * 通用占压分析
  3116. * @param {function} callSuccess 成功回调
  3117. * @param {function} callError 失败回调
  3118. */
  3119. commonOccupyAnalysis: function(callSuccess, callError) {
  3120. let _self = this;
  3121. require([
  3122. "esri/geometry/projection",
  3123. "esri/geometry/SpatialReference"
  3124. ], function(projection, SpatialReference) {
  3125. /* 定义输出投影 */
  3126. let outSReference = new SpatialReference({
  3127. wkid: 4326,
  3128. });
  3129. _self.activeSketchPolygonTools({
  3130. onadded: function(geometry) {
  3131. /* 进行坐标转换 */
  3132. projection.load().then(function() {
  3133. let outGeometry = projection
  3134. .project(geometry,
  3135. outSReference);
  3136. let outCoordinate = undefined;
  3137. for (let ring of outGeometry.rings[
  3138. 0]) {
  3139. if (outCoordinate) {
  3140. outCoordinate += "," + ring[
  3141. 0] + "," +
  3142. ring[1];
  3143. } else {
  3144. outCoordinate = ring[0] +
  3145. "," + ring[1];
  3146. }
  3147. }
  3148. /* 回调输出 */
  3149. if (callSuccess) callSuccess(
  3150. outCoordinate);
  3151. })
  3152. }
  3153. })
  3154. });
  3155. },
  3156. /**
  3157. * 占压分析
  3158. * @param {JSON} options 配置项
  3159. * @param {String} options.url 查询地址
  3160. * @param {Array} options.queryLayers 查询配置表
  3161. * @param {function} callSuccess 成功回调callSuccess({tabName:features:{objId:showName}})
  3162. * @param {function} callError 错误回调callError(string)
  3163. */
  3164. occupyAnalysis: function(options, callSuccess, callError) {
  3165. let _self = this;
  3166. require([
  3167. "esri/rest/support/IdentifyParameters",
  3168. "esri/rest/identify"
  3169. ], function(IdentifyParameters, identify) {
  3170. /* 启用草图工具 */
  3171. _self.activeSketchPointTools({
  3172. onadded: function(geometry) {
  3173. let params = new IdentifyParameters({
  3174. geometry: geometry,
  3175. mapExtent: _self.mapView.extent,
  3176. returnGeometry: true,
  3177. tolerance: 3,
  3178. layerOption: 'all',
  3179. });
  3180. identify.identify(options.url, params).then(
  3181. function(
  3182. response) {
  3183. /* 查询结果 */
  3184. let qResults = [];
  3185. /* 循环查询结果 */
  3186. for (let item of response.results) {
  3187. let layerName = item.layerName
  3188. .toUpperCase();
  3189. /* 巡查查询数据对应的配置图层 */
  3190. let layers = options.queryLayers
  3191. .filter(
  3192. function(
  3193. p) {
  3194. return p.eName
  3195. .toUpperCase() ===
  3196. layerName;
  3197. });
  3198. let configLayer = layers !=
  3199. undefined &&
  3200. layers
  3201. .length > 0 ? layers[0] :
  3202. undefined;
  3203. if (configLayer === undefined)
  3204. continue;
  3205. let tabCName = configLayer
  3206. .cName;
  3207. let fldName = configLayer
  3208. .fldName;
  3209. /* 创建返回要素 */
  3210. let objId = item.feature
  3211. .attributes[
  3212. 'OBJECTID'];
  3213. let showName = item.feature
  3214. .attributes[
  3215. fldName
  3216. .toUpperCase()];
  3217. let feature = {
  3218. objId: objId,
  3219. tabName: layerName,
  3220. showName: showName,
  3221. }
  3222. /* 寻找 */
  3223. let tempResults = qResults
  3224. .filter(function(
  3225. p) {
  3226. return p
  3227. .tabEName ===
  3228. layerName;
  3229. })
  3230. if (tempResults === undefined ||
  3231. tempResults
  3232. .length === 0) {
  3233. qResults.push({
  3234. tabEName: layerName,
  3235. tabCName: tabCName,
  3236. features: [
  3237. feature
  3238. ],
  3239. })
  3240. } else {
  3241. tempResults[0].features
  3242. .push(feature);
  3243. }
  3244. }
  3245. /* 返回结果 */
  3246. if (qResults.length > 0 &&
  3247. callSuccess) {
  3248. callSuccess(qResults);
  3249. } else if (qResults.length === 0 &&
  3250. callError) {
  3251. callError('未查询到任何数据!');
  3252. }
  3253. });
  3254. }
  3255. })
  3256. });
  3257. },
  3258. /**
  3259. * 案件查询 点方式
  3260. * @param {JSON} options 配置项
  3261. * options.url{string}:查询地址
  3262. * options.filter{string}:案件查询过滤条件
  3263. * @param {function} callSuccess 成功回调
  3264. * callSuccess{array{string}}
  3265. * @param {function} callError 错误回调
  3266. * callError{string}
  3267. */
  3268. queryCaseByPoint: function(options, callSuccess, callError) {
  3269. let _self = this;
  3270. _self.activeSketchPointTools({
  3271. onadded: function(geometry) {
  3272. _self._queryCaseFeature({
  3273. geometry: geometry,
  3274. url: options.url,
  3275. id: options.id
  3276. }, callSuccess, callError);
  3277. }
  3278. })
  3279. },
  3280. /**
  3281. * 案件查询 绘制区域方式
  3282. * @param {JSON} options 配置项
  3283. * options.url{string}:查询地址
  3284. * options.filter{string}:案件查询过滤条件
  3285. * @param {function} callOperation 操作回调
  3286. * callOperation({})
  3287. * @param {function} callSuccess 成功回调
  3288. * callSuccess{array{string}}
  3289. * @param {function} callError 错误回调
  3290. * callError{string}
  3291. */
  3292. queryCaseByPolygon: function(options, callOperation, callSuccess, callError) {
  3293. let _self = this;
  3294. _self.activeSketchPolygonTools({
  3295. onadded: function(geometry) {
  3296. if (callOperation) callOperation({
  3297. geometry: geometry,
  3298. });
  3299. },
  3300. oncomplete: function(geometry) {
  3301. _self._queryCaseFeature({
  3302. geometry: geometry,
  3303. url: options.url,
  3304. id: options.id
  3305. }, callSuccess, callError);
  3306. }
  3307. })
  3308. },
  3309. /**
  3310. * 区域案件查询完成操作
  3311. */
  3312. queryCaseByPolygonComplete: function() {
  3313. this._sketchComplete();
  3314. },
  3315. /**
  3316. * 案件要素查询
  3317. * @param {JSON} options 配置项
  3318. * options.geometry{JSON}:查询参考要素
  3319. * options.url{string}:查询地址
  3320. * options.filter{string}:查询数据的过滤条件
  3321. * @param {function} callSuccess 成功回调
  3322. * callSuccess{array{string}}
  3323. * @param {function} callError 错误回调
  3324. * callError{string}
  3325. */
  3326. _queryCaseFeature: function(options, callSuccess, callError) {
  3327. let _self = this;
  3328. var self = this;
  3329. require([
  3330. "esri/tasks/support/Query",
  3331. "esri/tasks/QueryTask",
  3332. ],
  3333. function(Query, QueryTask) {
  3334. /* 定义查询任务 */
  3335. let qTask = new QueryTask({
  3336. url: options.url
  3337. });
  3338. /* 定义查询参数 */
  3339. let params = new Query({
  3340. returnGeometry: true,
  3341. outFields: ["*"],
  3342. distance: 3,
  3343. geometry: options.geometry,
  3344. spatialRelationship: "intersects",
  3345. where: options.filter,
  3346. });
  3347. /* 开始查询 */
  3348. qTask.execute(params).then(function(response) {
  3349. let result = [];
  3350. for (let feature of response.features) {
  3351. result.push(feature.attributes['AJH']);
  3352. }
  3353. if (result.length === 0 && callError) callError(
  3354. '未查询到任何案件!');
  3355. if (result.length > 0 && callSuccess) callSuccess(
  3356. result);
  3357. });
  3358. });
  3359. },
  3360. /**
  3361. * 长度测量
  3362. * @param {function} callSuccess 成功回调
  3363. * callSuccess(float)
  3364. */
  3365. eMeasureLength: function(callSuccess) {
  3366. _self = this;
  3367. /* 存储临时长度 */
  3368. _self.tempLen = 0;
  3369. /* 绘制的文字要素集合 */
  3370. _self.drawLabelGraphics = [];
  3371. _self.activeSketchLineTools({
  3372. onadded: function(geometry) {
  3373. /* 传递的geometry为polyline类型 数据存储在paths中 */
  3374. let path = geometry.paths[0];
  3375. let pt = path[path.length - 1];
  3376. _self._measureLengthByGeometry(geometry, function(len) {
  3377. let lenTemp = len - _self.tempLen;
  3378. _self.tempLen = len;
  3379. let showLen = _self._measureLengthTrasform(
  3380. lenTemp);
  3381. let label = len === 0 ? '起点' : showLen;
  3382. _self._measureLengthAddLabel({
  3383. x: pt[0],
  3384. y: pt[1],
  3385. label: label
  3386. }, function(graphic) {
  3387. _self.drawLabelGraphics.push(
  3388. graphic);
  3389. });
  3390. /* 长度回调 */
  3391. if (len > 0 && callSuccess) callSuccess(_self
  3392. ._measureLengthTrasform(len));
  3393. })
  3394. },
  3395. onredo: function(geometry) {
  3396. /* 传递的geometry为polyline类型 数据存储在paths中 */
  3397. let path = geometry.paths[0];
  3398. let pt = path[path.length - 1];
  3399. _self._measureLengthByGeometry(geometry, function(len) {
  3400. let lenTemp = len - _self.tempLen;
  3401. _self.tempLen = len;
  3402. let showLen = _self._measureLengthTrasform(
  3403. lenTemp);
  3404. let label = len === 0 ? '起点' : showLen;
  3405. _self._measureLengthAddLabel({
  3406. x: pt[0],
  3407. y: pt[1],
  3408. label: label
  3409. }, function(graphic) {
  3410. _self.drawLabelGraphics.push(
  3411. graphic);
  3412. });
  3413. /* 长度回调 */
  3414. if (len > 0 && callSuccess) callSuccess(_self
  3415. ._measureLengthTrasform(len));
  3416. })
  3417. },
  3418. onundo: function(geometry) {
  3419. _self._measureLengthByGeometry(geometry, function(len) {
  3420. /* 干掉最后一个 */
  3421. let graphic = _self.drawLabelGraphics.pop();
  3422. _self.graphicsLayer.graphics.remove(graphic);
  3423. /* 赋值临时长度 */
  3424. _self.tempLen = len;
  3425. /* 长度回调 */
  3426. if (callSuccess) callSuccess(_self
  3427. ._measureLengthTrasform(
  3428. len));
  3429. });
  3430. }
  3431. })
  3432. },
  3433. /**
  3434. * 面积测量
  3435. * @param {function} callSuccess 成功回调
  3436. * callSuccess(float)
  3437. */
  3438. eMeasureArea: function(callSuccess) {
  3439. let _self = this;
  3440. _self.activeSketchPolygonTools({
  3441. onadded: function(geometry) {
  3442. _self._measureAreaByGeometry(geometry, function(area) {
  3443. if (callSuccess) callSuccess(_self
  3444. ._measureAreaTrasform(
  3445. area));
  3446. });
  3447. },
  3448. onredo: function(geometry) {
  3449. _self._measureAreaByGeometry(geometry, function(area) {
  3450. if (callSuccess) callSuccess(_self
  3451. ._measureAreaTrasform(
  3452. area));
  3453. });
  3454. },
  3455. onundo: function(geometry) {
  3456. _self._measureAreaByGeometry(geometry, function(area) {
  3457. if (callSuccess) callSuccess(_self
  3458. ._measureAreaTrasform(
  3459. area));
  3460. });
  3461. }
  3462. })
  3463. },
  3464. /**
  3465. * 查询坐标
  3466. * @param {function} callSuccess 成功回调
  3467. */
  3468. eMeasureCoordinate: function(callSuccess) {
  3469. let _self = this;
  3470. _self.activeSketchPointTools({
  3471. onadded: function(geometry) {
  3472. require(["esri/geometry/SpatialReference",
  3473. "esri/geometry/projection"
  3474. ],
  3475. function(SpatialReference, projection) {
  3476. let outProject = new SpatialReference({
  3477. wkid: 4326,
  3478. });
  3479. projection.load().then(function() {
  3480. let outGeometry = projection
  3481. .project(geometry,
  3482. outProject);
  3483. if (callSuccess) callSuccess({
  3484. x: outGeometry.x,
  3485. y: outGeometry.y,
  3486. })
  3487. });
  3488. });
  3489. }
  3490. });
  3491. },
  3492. /**
  3493. * 转换长度为字符串
  3494. * @param {float} len 长度
  3495. * @return {string} 带单位的长度表示
  3496. */
  3497. _measureLengthTrasform: function(len) {
  3498. let strLen = len < 1000 ? len.toFixed(2) + '米' : (len /
  3499. 1000).toFixed(2) + '公里';
  3500. return strLen;
  3501. },
  3502. /**
  3503. * 转换面积为字符串
  3504. * @param {float} area 面积
  3505. * @return {string} 带单位的面积表示
  3506. */
  3507. _measureAreaTrasform: function(area) {
  3508. let strMuArea = (area * 0.0015).toFixed(2) + "亩";
  3509. let strGqArea = (area * 0.0001).toFixed(2) + "公顷";
  3510. let strArea = area < 1000000 ? area.toFixed(2) + "平方米 " + strMuArea : (area /
  3511. 1000000)
  3512. .toFixed(2) +
  3513. "平方公里 " + strGqArea;
  3514. return strArea;
  3515. },
  3516. /**
  3517. * 测量长度时添加标签
  3518. * @param {JSON} options 配置项
  3519. * options.x{float}:标注点位置x坐标
  3520. * options.y{float}:标注点位置y坐标
  3521. * options.label{string}:标注点文字
  3522. * @param {function} callSuccess 成功回调
  3523. * callSuccess(graphic)
  3524. */
  3525. _measureLengthAddLabel: function(options, callSuccess) {
  3526. let _self = this;
  3527. require([
  3528. "esri/Graphic",
  3529. "esri/geometry/Point"
  3530. ], function(Graphic, Point) {
  3531. _self.textSymbol.text = options.label;
  3532. /* 创建文字要素 */
  3533. let txtGraphic = new Graphic({
  3534. geometry: new Point({
  3535. x: options.x,
  3536. y: options.y,
  3537. spatialReference: _self.mapView
  3538. .spatialReference,
  3539. }),
  3540. symbol: _self.textSymbol
  3541. });
  3542. /* 将文字要素添加到绘制图层 */
  3543. _self.graphicsLayer.add(txtGraphic);
  3544. /* 回调 */
  3545. if (callSuccess) callSuccess(txtGraphic);
  3546. });
  3547. },
  3548. /**
  3549. * 计算长度
  3550. * @param {JSON} geometry 待计算要素
  3551. * @param {function} callSuccess 计算成功回调
  3552. * callSuccess({float})
  3553. */
  3554. _measureLengthByGeometry: function(geometry, callSuccess) {
  3555. require([
  3556. "esri/geometry/geometryEngine"
  3557. ], function(geometryEngine) {
  3558. let len = geometryEngine.geodesicLength(geometry, "meters");
  3559. if (callSuccess) callSuccess(len);
  3560. });
  3561. },
  3562. /**
  3563. * 计算面积
  3564. * @param {JSON} geometry 待计算要素
  3565. * @param {function} callSuccess 计算成功回调
  3566. * callSuccess({float})
  3567. */
  3568. _measureAreaByGeometry: function(geometry, callSuccess) {
  3569. require([
  3570. "esri/geometry/geometryEngine"
  3571. ], function(geometryEngine) {
  3572. let len = geometryEngine.geodesicArea(geometry, "square-meters");
  3573. if (callSuccess) callSuccess(len);
  3574. });
  3575. }
  3576. })
  3577. /* GPS定位及方向变化相关 */
  3578. JTMapKit.prototype.extend({
  3579. /**
  3580. * 添加GPS位置
  3581. * @param {Object} options 配置项
  3582. * @param {String} options.longitude 定位经度
  3583. * @param {String} options.latitude 定位纬度
  3584. * @param {String} options.angle 角度
  3585. * @param {Function} callSuccess 成功回调callSuccess()
  3586. */
  3587. _appendGPSSymbol: function(options, callSuccess) {
  3588. let _self = this;
  3589. require(["esri/Graphic", "esri/geometry/Point",
  3590. "esri/geometry/SpatialReference",
  3591. "esri/layers/GraphicsLayer"
  3592. ], function(Graphic,
  3593. Point, SpatialReference, GraphicsLayer) {
  3594. /* 定义坐标系 */
  3595. let spatialReference = new SpatialReference({
  3596. wkid: 4326,
  3597. })
  3598. /* 定义点 */
  3599. let gpsPoint = new Point({
  3600. x: parseFloat(options.longitude),
  3601. y: parseFloat(options.latitude),
  3602. spatialReferenct: spatialReference,
  3603. })
  3604. /* 创建要素 */
  3605. if (_self.gpsGraphic === undefined) {
  3606. _self.gpsGraphic = new Graphic({
  3607. geometry: gpsPoint,
  3608. symbol: _self.gpsSysmbol,
  3609. })
  3610. if (!_self.gpsLayer) {
  3611. _self.gpsLayer = new GraphicsLayer();
  3612. _self.map.layers.add(_self.gpsLayer);
  3613. } else {
  3614. _self.gpsLayer.removeAll();
  3615. }
  3616. _self.gpsLayer.graphics.add(_self.gpsGraphic);
  3617. } else {
  3618. _self.gpsGraphic.geometry = gpsPoint;
  3619. }
  3620. /* 定位要素 */
  3621. // _self.zoomTo(options.longitude, options.latitude);
  3622. /* 成功回调 */
  3623. // console.log(options.longitude, options.latitude);
  3624. if (callSuccess) callSuccess();
  3625. });
  3626. },
  3627. /**
  3628. * 添加GPS位置
  3629. * @param {Object} options 配置项
  3630. * @param {String} options.longitude 定位经度
  3631. * @param {String} options.latitude 定位纬度
  3632. * @param {String} options.angle 角度
  3633. * @param {Function} callSuccess 成功回调callSuccess()
  3634. */
  3635. gpsAppendToMap: function(options, callSuccess) {
  3636. this._appendGPSSymbol(options, callSuccess);
  3637. },
  3638. /**
  3639. * 移除GPS符号
  3640. */
  3641. gpsRemoveFromMap: function() {
  3642. /* 移除展示元素 */
  3643. if (this.gpsLayer != undefined) {
  3644. this.gpsLayer.removeAll();
  3645. }
  3646. /* 重置展示元素 */
  3647. this.gpsGraphic = undefined;
  3648. },
  3649. /**
  3650. * 设置GPS符号的角度
  3651. * @param {float} angle 角度
  3652. */
  3653. gpsSetAngle: function(angle) {
  3654. if (this.gpsGraphic != undefined) {
  3655. this.gpsSysmbol.angle = parseFloat(-1 * angle);
  3656. this.gpsGraphic.symbol = this.gpsSysmbol;
  3657. }
  3658. }
  3659. })