map-easy-client.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. var mapConfig = window[window.EASYMAP_CONFIG['mapConfigName']];
  2. var xzxqLayerGruop ;
  3. function initMap(divId){
  4. // console.log(mapConfig)
  5. if ( mapConfig.option.crs === 'EPSG4326' ){
  6. mapConfig.option.crs = L.CRS.EPSG4326;
  7. }else if (mapConfig.option.crs === 'EPSG3857'){
  8. mapConfig.option.crs = L.CRS.EPSG3857;
  9. }
  10. var map = L.map(divId,mapConfig.option);
  11. // newLayerAddToMap(mapConfig.layers[0],map);
  12. initControl(map);
  13. initMapEvent(map);
  14. // console.log(map.pm)
  15. map.pm.setLang('zh');
  16. window.map = map;
  17. return map;
  18. }
  19. function initControl(map){
  20. setLayerControl(map);
  21. L.control.zoom({position: 'bottomright',zoomInTitle:'放大',zoomOutTitle:'缩小'}).addTo(map);
  22. var topright = L.control.attribution({position: 'topright',prefix:''}).addTo(map);
  23. var rightTopTool1 = '<div id="app-right-top" style="width: 302px;"> <div id="tool-container" class="toolscontainer"><div class="ui3-control-wrap clearfixs" id="ui3_control_wrap">';
  24. var citychangeopt2 = '<div class="citychangeopt ui3-city-change-wrap" id="ui3_city_change" onclick="selectCity(this)" style="width: 96px;" ><span class="adjustpadding"></span><a style="width: 72px;text-align: center;" href="#" onclick="return false" onclick="return false" class="ui3-city-change-inner ui3-control-shadow"><div id="toolCity" data-code="460000000" level="1" style="width: 56px;overflow: hidden;white-space: nowrap;" >海南省</div><em style="margin-top: -20px;" ></em></a></div>';
  25. var citychangeopt2_1 = '';
  26. var leftfloat3 = '<div class="left float-l"><b class="tool-gap"></b>';
  27. var ht4_01 = '<div class="trafficopt" onclick="traffic(this,\'ht\')" style="width: 40px;"><span id="traffic_control" style="background-image: none;width: 10px;" class="last traffic"></span><i>海图</i></div><b></b>';
  28. var trafficopt4 = '<div class="trafficopt" onclick="traffic(this,\'bj\')" style="width: 40px;"><span id="traffic_control" style="background-image: none;width: 10px;" class="last traffic"></span><i>标注</i></div><b></b>';
  29. var boxopt5 = '<div class="boxopt" onclick="boxopt(this)" style="width: 87px;" ><span id="util_control" class="boxutils boxicon"></span><i class="boxtext">工具箱</i><em class=""></em></div>';
  30. var detail_box6 = '<div class="detail-box" style="display: none;"><ul id="boxul" class="boxinfo"><li class="map-measure" onclick="measure(this,\'line\')"><span class="last measure"></span><i>测距</i></li><li class="map-mark" onclick="mark(this)"> <span class="last mark"></span> <i>标记</i></li> </ul></div>';
  31. var endL7 = '</div></div></div></div>';
  32. // topright.addAttribution(rightTopTool1+citychangeopt2+leftfloat3+ht4_01+trafficopt4+boxopt5+detail_box6+endL7 + getCityListHtml() );
  33. xzxqLayerGruop = L.layerGroup();
  34. map.addLayer(xzxqLayerGruop);
  35. var position = L.control.attribution({position: 'bottomright',prefix:''}).addTo(map);
  36. position.getContainer().style="position:relative;right: 6px;bottom: 41px;background: 0;";
  37. position.getContainer().onclick=function(){
  38. map.setView(mapConfig.option.center,mapConfig.option.zoom);
  39. };
  40. position.addAttribution("<div style='width: 26px;height: 26px;overflow: hidden;background-color: #fff;text-align: center;cursor: pointer;line-height: 26px;float: left;box-shadow: 1px 2px 1px rgba(0,0,0,.15);border-radius: 4px;'>" +
  41. "<img src="+mapConfig.positionImgUrl+" style='position: relative;vertical-align: middle;right: 8px;' />"
  42. +"</div>");
  43. //document.getElementById('selCityHotCityId').innerHTML = getCityList();
  44. let sa = L.control.scale({position: 'bottomright',imperial:false}).addTo(map);
  45. sa.getContainer().style="top:175px;right:65px;";
  46. }
  47. function mark(thi){
  48. //boxopt(document.getElementsByClassName('boxopt')[0]);
  49. var box = document.getElementsByClassName('boxopt')[0];
  50. if( box.className.indexOf('box-click') !== -1 )
  51. {
  52. //thi.className = thi.className+' box-click';
  53. box.childNodes.forEach(function(e){
  54. if( e.className.indexOf('boxtext') !== -1 )
  55. {
  56. if( e.innerText ==='工具箱' )
  57. {
  58. box.className = box.className+' box-mark';
  59. e.innerText = '标记';
  60. map.pm.addControls({
  61. position: 'topright',
  62. });
  63. }
  64. }
  65. });
  66. document.getElementsByClassName('detail-box')[0].style.display = 'none';
  67. }
  68. }
  69. function measure(thi,type){
  70. boxopt(document.getElementsByClassName('boxopt')[0]);
  71. var map = window.map;
  72. //map.measure = [];
  73. if( map.measureLayer && map.hasLayer(map.measureLayer) )
  74. {
  75. map.removeLayer(map.measureLayer);
  76. }
  77. if( !map.measure )
  78. {
  79. map.measure = [];
  80. }else{
  81. map.measure.forEach(function(e){
  82. if( e.layer ){
  83. e.layer.closeTooltip();
  84. }
  85. });
  86. map.measure = [];
  87. }
  88. if( type ==='line' )
  89. {
  90. map.on('pm:create', e => {
  91. map.measureLayer = e.layer;
  92. if( e.shape === "Line")
  93. {
  94. map.pm.disableDraw('Line');
  95. map.off('pm:drawstart');
  96. if(map.measureLayer){
  97. map.measureLayer.off('pm:vertexadded');
  98. }
  99. }
  100. });
  101. map.on('pm:drawstart', ({ workingLayer }) => {
  102. workingLayer.on('pm:vertexadded', e => {
  103. if( e.shape === "Line")
  104. {
  105. if(map.measure.length == 0)
  106. {
  107. var marker = L.marker(e.latlng);
  108. marker.bindTooltip('起点',{direction:'top',offset:[0,15]});
  109. //console.log(e.marker);
  110. map.openTooltip(marker.getTooltip(),marker.getLatLng());
  111. map.measure.push({layer:marker});
  112. }else{
  113. var m = parseFloat(map.distance(map.measure[map.measure.length-1].layer.getLatLng(),e.latlng).toFixed(2));
  114. if(map.measure[map.measure.length-1].lastDistance){
  115. m = m + parseFloat(map.measure[map.measure.length-1].lastDistance);
  116. }
  117. var marker = L.marker(e.latlng);
  118. var dw = '米',gl=0;
  119. if( m/1000 >= 1 )
  120. {
  121. gl = m/1000;
  122. marker.bindTooltip(gl.toFixed(2)+'公里',{direction:'top',offset:[0,15]});
  123. }else{
  124. marker.bindTooltip(m+'米',{direction:'top',offset:[0,15]});
  125. }
  126. map.openTooltip(marker.getTooltip(),marker.getLatLng());
  127. map.measure.push({layer:marker,lastDistance:m});
  128. }
  129. }
  130. });
  131. });
  132. map.pm.disableDraw('Line');
  133. map.pm.enableDraw('Line', {});
  134. }
  135. }
  136. function boxopt(thi){
  137. if(thi.className.indexOf('box-mark') !== -1){
  138. map.pm.removeControls();
  139. thi.childNodes.forEach(function(e){
  140. if( e.className.indexOf('boxtext') !== -1 )
  141. {
  142. if( e.innerText ==='标记' )
  143. {
  144. e.innerText = '工具箱';
  145. }
  146. }
  147. if( e.className.indexOf(' active') === -1 )
  148. {
  149. e.className = e.className+' active';
  150. }else{
  151. e.className = e.className.replace('active' , '');
  152. }
  153. });
  154. thi.className = thi.className.replace('box-click' , '');
  155. thi.className = thi.className.replace('box-mark' , '');
  156. document.getElementsByClassName('detail-box')[0].style.display = 'none';
  157. return ;
  158. }
  159. if( thi.className.indexOf('box-click') === -1 )
  160. {
  161. thi.className = thi.className+' box-click';
  162. document.getElementsByClassName('detail-box')[0].style.display = 'block';
  163. }else{
  164. thi.className = thi.className.replace('box-click' , '');
  165. document.getElementsByClassName('detail-box')[0].style.display = 'none';
  166. }
  167. thi.childNodes.forEach(function(e){
  168. if( e.className.indexOf(' active') === -1 )
  169. {
  170. e.className = e.className+' active';
  171. }else{
  172. e.className = e.className.replace('active' , '');
  173. }
  174. });
  175. }
  176. function getCityListHtml(){
  177. var mapPopup1 = '<div class="map_popup " style="width: 302px; display: none; height: auto; right: 5px; top: 57px;"><div class="popup_main">';
  178. var title2 = '<div class="title"><p ><span style="white-space: nowrap;">当前位置:<span id="curNav"><a id="curCityInfo" data-code="460000000" pcode="" onclick="cityListClick(\'460000000\',1,\'海南省\',\'\')" level="1" >海南省</a></span></span></p></div>';
  179. var content3 = '<div class="content" style="height: 100%; overflow: hidden;"> <div class="sel_city blueC">';
  180. var selCityTop4 = '<div id="selCityTop" style="_width:100%;"><div id="selCityHotCityId" class="sel-city-hotcity">';
  181. var cityList5 = initCityList(getHnCityTree().data);
  182. //var cityList5 = '<a href="javascript:void(0)" onclick="" data-code="460100000" level="2" name="海口市" style="margin-right:5px;*margin-right:1px;">海口市</a>'+
  183. // '<a href="javascript:void(0)" data-code="460200000" level="2" name="三亚市" style="margin-right:5px;*margin-right:1px;">三亚市</a>';
  184. var endHtml6 = '</div></div></div></div><button onclick="closeMapPoup()" id="popup_close" title="关闭"></button></div></div>';
  185. return mapPopup1 + title2 + content3+selCityTop4+cityList5+endHtml6;
  186. }
  187. function cityListClick(dataCode , level,name,pcode){
  188. //var cityTree = getHnCityTree();
  189. var curNav = document.getElementById('curNav');
  190. var toolCity = document.getElementById('toolCity');
  191. var curCityInfo = document.getElementById('curCityInfo');//'<span data-code="'+dataCode+'" level="'+level+'" >'+name+'</span>'
  192. if( dataCode === curCityInfo.getAttribute('data-code') )
  193. {
  194. return ;
  195. }
  196. if(level > curCityInfo.getAttribute('level') )
  197. {
  198. var newItem = document.createElement('a');
  199. newItem.setAttribute('data-code',curCityInfo.getAttribute('data-code')) ;
  200. newItem.setAttribute('pcode',curCityInfo.getAttribute('pcode'));
  201. newItem.setAttribute('level',curCityInfo.getAttribute('level'));
  202. newItem.setAttribute('onclick',curCityInfo.getAttribute('onclick'));
  203. newItem.innerText = curCityInfo.innerText+">>";
  204. curNav.insertBefore(newItem,curNav.childNodes[curNav.childNodes.length-1]);
  205. curCityInfo.setAttribute('data-code' ,dataCode);
  206. curCityInfo.setAttribute('name' ,name);
  207. curCityInfo.setAttribute('level' ,level);
  208. curCityInfo.setAttribute('pcode' ,pcode);
  209. curCityInfo.setAttribute('onclick','cityListClick(\''+dataCode+'\','+level+',\''+name+'\',\''+pcode+'\')');
  210. }else if(level < curCityInfo.getAttribute('level') && curNav.childNodes && curNav.childNodes.length > 0 ){
  211. for( var i = curNav.childNodes.length -1 ; i >= 0; i-- )
  212. {
  213. if(curNav.childNodes[i] && curNav.childNodes[i].getAttribute('level') > level )
  214. {
  215. curNav.removeChild(curNav.childNodes[i]);
  216. }
  217. }
  218. curNav.childNodes[curNav.childNodes.length -1].setAttribute('id' ,'curCityInfo');
  219. curCityInfo = document.getElementById('curCityInfo');
  220. var selCityHotCityId = document.getElementById('selCityHotCityId');
  221. for( var i = 0 ; i < selCityHotCityId.childNodes.length ; i++ )
  222. {
  223. selCityHotCityId.childNodes[i].style.display = 'none';
  224. }
  225. }
  226. toolCity.setAttribute('data-code' ,dataCode);
  227. toolCity.setAttribute('name' ,name);
  228. toolCity.setAttribute('level' ,level);
  229. toolCity.setAttribute('pcode' ,pcode);
  230. toolCity.innerText = name;
  231. curCityInfo.innerText = name;
  232. var dataCodeCls = document.getElementsByClassName('datacode'+dataCode);
  233. if(dataCodeCls && dataCodeCls.length > 0 )
  234. {
  235. document.getElementsByClassName('datacode'+dataCode)[0].style.display = 'block';
  236. var pcodeEle = document.getElementsByClassName('datacode'+pcode);
  237. if( pcode && pcode !== '' && pcodeEle && pcodeEle.length > 0 && level < 5 )
  238. {
  239. document.getElementsByClassName('datacode'+pcode)[0].style.display = 'none';
  240. }
  241. }
  242. //加载辖区边界
  243. drawBoundOnMap(dataCode);
  244. }
  245. function drawBoundOnMap(datacode){
  246. var map = window.map;
  247. //console.log(mapConfig.xq_url.replace('{{datacode}}','1CHN'+datacode));
  248. var httpXml = createXMLHttpRequest();
  249. httpXml.open('get', mapConfig.xq_url.replace('{{datacode}}','1CHN'+datacode), true);
  250. httpXml.send(null);
  251. httpXml.onreadystatechange = function() {
  252. if(httpXml.readyState == 4 && httpXml.status == 200) {
  253. // xzxq.push(httpXml.responseText);
  254. var map = window.map;
  255. xzxqLayerGruop.clearLayers();
  256. var data = JSON.parse(httpXml.responseText);
  257. if( data && data.result && data.result.length > 0 )
  258. {
  259. var result = data.result;
  260. var boundGeo ;
  261. for (var i = 0; i < result.length; i++) {
  262. var tempData = result[i];
  263. var geometry = omnivore.wkt.parse(tempData.geometry);
  264. geometry.setStyle({fill:false});
  265. geometry.addTo(xzxqLayerGruop);
  266. if( !boundGeo )
  267. {
  268. boundGeo = geometry.getBounds();
  269. }else{
  270. boundGeo.extend(geometry.getBounds());
  271. }
  272. }
  273. map.fitBounds(boundGeo);
  274. }
  275. }
  276. };
  277. }
  278. function initCityList(data){
  279. //var tree = getHnCityTree();
  280. var htmlList= '';
  281. //var childrenList = '';
  282. for( var i in data )
  283. {
  284. if( i == 0 && (data[i].level === 2 || data[i].level === 3 ) )
  285. {
  286. htmlList += '<div class="datacode'+data[i].pcode+'">';
  287. }
  288. if(i == 0 && data[i].level > 3){
  289. htmlList += '<div style="display:none;" class="datacode'+data[i].pcode+'">';
  290. }
  291. htmlList += '<a href="javascript:void(0)" onclick="cityListClick(\''+data[i].code+'\',\''+data[i].level+'\',\''+data[i].name+'\',\''+data[i].pcode+'\')" data-code="'+data[i].code+'" level="'+data[i].level+'" name="'+data[i].name+'" pcode="'+data[i].pcode+'">'+data[i].name+'</a>';
  292. if( i == data.length -1 )
  293. {
  294. htmlList += '</div>';
  295. }
  296. //加载辖区边界
  297. //drawBoundOnMap(data[i].code);
  298. }
  299. for( var i in data )
  300. {
  301. if( data[i].childrens.length > 0 )
  302. {
  303. htmlList += initCityList(data[i].childrens);
  304. //htmlList += '<div style="display:none;" class="datacode'+data[i].code+'" >'+initCityList(data[i].childrens)+'</div>';
  305. }
  306. }
  307. return htmlList;
  308. }
  309. function selectCity(thi){
  310. if( thi.className.indexOf('ui3-city-change-click') === -1 )
  311. {
  312. thi.className = thi.className+' ui3-city-change-click';
  313. document.getElementsByClassName('map_popup')[0].style.display = 'block';
  314. }else{
  315. thi.className = thi.className.replace('ui3-city-change-click' , '');
  316. document.getElementsByClassName('map_popup')[0].style.display = 'none';
  317. }
  318. }
  319. function closeMapPoup(){
  320. document.getElementById('ui3_city_change').className = document.getElementById('ui3_city_change').className.replace('ui3-city-change-click' , '');
  321. document.getElementsByClassName('map_popup')[0].style.display = 'none';
  322. }
  323. function traffic(thi,name){
  324. var map = window.map;
  325. if(!map[name]){
  326. for( var i in mapConfig.overlayMaps ){
  327. if( name === mapConfig.overlayMaps[i].name ){
  328. if(!map[name]){
  329. map[name] = {};
  330. }
  331. map[name].bjLayer = newLayerByType(mapConfig.overlayMaps[i]).addTo(map);
  332. map[name].flag = true;
  333. }
  334. }
  335. thi.children[0].className = thi.children[0].className+' active';
  336. thi.children[1].className = 'active';
  337. }else{
  338. if(map[name]&&!map[name].flag)
  339. {
  340. map[name].bjLayer.addTo(map);
  341. map[name].flag = true;
  342. thi.children[0].className = thi.children[0].className+' active';
  343. thi.children[1].className = 'active';
  344. }else{
  345. map[name].flag = false;
  346. if(map.hasLayer(map[name].bjLayer)){
  347. map.removeLayer(map[name].bjLayer);
  348. }
  349. thi.children[0].className = thi.children[0].className.replace('active','');
  350. thi.children[1].className = '';
  351. }
  352. }
  353. }
  354. //度转度°分′秒″
  355. function ToDegrees(val) {
  356. val = val.toString();
  357. if (typeof (val) == "undefined" || val == "") {
  358. return "";
  359. }
  360. var i = val.indexOf('.');
  361. var strDu = i < 0 ? val : val.substring(0, i);//获取度
  362. var strFen = "0";
  363. var strMiao = "0";
  364. var preMiao = "0";
  365. var sufMiao = "0";
  366. if (i > 0) {
  367. var strFen = "0" + val.substring(i);
  368. strFen = strFen * 60 + "";
  369. i = strFen.indexOf('.');
  370. if (i > 0) {
  371. strMiao = "0" + strFen.substring(i);
  372. strFen = strFen.substring(0, i);//获取分
  373. strMiao = strMiao * 60 + "";
  374. i = strMiao.indexOf('.');
  375. strMiao = strMiao.substring(0, i + 4);//取到小数点后面三位
  376. strMiao = parseFloat(strMiao).toFixed(2);//精确小数点后面两位
  377. var j = strMiao.indexOf(".")
  378. if(j>0){
  379. preMiao = strMiao.substring(0,j)
  380. sufMiao = strMiao.substring(j+1)
  381. }
  382. }
  383. }
  384. return strDu + "°" + (strFen.length == 2 ? strFen : "0"+strFen) + "'" + (preMiao.length == 2 ? preMiao : "0"+preMiao)+"."+sufMiao+'"';
  385. }
  386. var jwd = "";
  387. function initMapEvent(map){
  388. map.on('mousemove',
  389. function(e) {
  390. var x = e.latlng.lat;
  391. var y = e.latlng.lng;
  392. jwd = ToDegrees(y) + "E&nbsp;&nbsp;&nbsp;" + ToDegrees(x)+"N";
  393. document.getElementById('jwd').style.removeProperty("display")
  394. document.getElementById('jwd').innerHTML = jwd+"&nbsp;&nbsp;&nbsp;图级:"+window.map.getZoom();
  395. });
  396. map.on('zoomend',
  397. function(e) {
  398. // console.log(e)
  399. var target = e.target
  400. // var latlng = target._animateToCenter
  401. // var x = latlng.lat;
  402. // var y = latlng.lng;
  403. var zoom = target._zoom
  404. // var jwd = ToDegrees(y) + "E&nbsp;&nbsp;&nbsp;" + ToDegrees(x)+"N";
  405. document.getElementById('jwd').style.removeProperty("display")
  406. document.getElementById('jwd').innerHTML = jwd+"&nbsp;&nbsp;&nbsp;图级:"+zoom;
  407. });
  408. map.on('click', function(e) {
  409. var event = e.originalEvent;
  410. map.eachLayer(function(layer){
  411. if( layer && layer.type === 'GWVTAnno' )
  412. {
  413. var feature = layer.getFeatureByXY(event.x,event.y);
  414. if(feature){
  415. layer.bindTooltip(feature.label).openTooltip(e.latlng);
  416. }
  417. }
  418. });
  419. });
  420. // map.fire map.initControlLayer
  421. /*map.on('pm:create', e => {
  422. console.log(e.layer);
  423. console.log(e.layer.getBounds());
  424. console.log(e.layer.getLatLngs()[0].toString());
  425. console.log(e.layer.getLatLngs()[0][0].toString());
  426. }); */
  427. }
  428. var layerArr=[]
  429. function clickImg(layer){
  430. for ( var i = 0 ; i < layerArr.length ; i++ )
  431. {
  432. document.getElementById(layerArr[i]).removeAttribute("style");
  433. }
  434. layerArr.push(layer)
  435. document.getElementById(layer).style.borderStyle="solid";
  436. document.getElementById(layer).style.borderColor="yellow";
  437. layerArr = [...new Set(layerArr)];
  438. }
  439. function setLayerControl(map){
  440. var layerHtml = '',currentLayerHtml;
  441. var baseMaps = {},overlayMaps={};
  442. var firstMap = "sl"
  443. for( var i in mapConfig.layers )
  444. {
  445. var layer = mapConfig.layers[i];
  446. baseMaps["<div class='layer-control-label' ><img id=\""+layer.mapType+"\" src='"+layer.imgUrl+"' onclick=\"clickImg('"+layer.mapType+"')\"/> <span onclick=\"clickImg('"+layer.mapType+"')\">"+layer.name+"</span></div>"] = newLayerGroup(layer);
  447. if(i === "0"){
  448. firstMap = layer.mapType
  449. }
  450. }
  451. /*for( var i in mapConfig.overlayMaps )
  452. {
  453. var layer = mapConfig.overlayMaps[i];
  454. overlayMaps[layer.name] = newLayerGroup(layer);
  455. }*/
  456. var layerControl = L.control.layers(baseMaps, overlayMaps,{position:'bottomright'}).addTo(map);
  457. layerControlContailner= layerControl.getContainer();
  458. //layerControlContailner.style.height = '36px';
  459. layerControlContailner.style['background-color'] = 'rgba(255,255,255,.8)';
  460. layerControlContailner.style['transition-property'] = 'width,background-color';
  461. layerControlContailner.style['transition-duration'] = '.4s';
  462. layerControlContailner.children[0].width='50px';
  463. layerControlContailner.children[0].height='62px';
  464. layerControlContailner.children[1].children[0].style.display = '-webkit-inline-box';
  465. setControlImg(map)
  466. map.addLayer(layerControl._layers[0].layer);
  467. var inRedioDOM = document.getElementsByName('leaflet-base-layers');
  468. for ( var i = 0 ; i < inRedioDOM.length ; i++ )
  469. {
  470. inRedioDOM[i].style.display = 'none';
  471. }
  472. clickImg(firstMap)
  473. }
  474. function setControlImg(map){
  475. map.on('baselayerchange',function(res){
  476. // console.log(res.layer.setZIndex)
  477. var src = $(res.name).find('img').attr('src')
  478. // console.log($('.leaflet-control-layers-toggle'))
  479. $('.leaflet-control-layers-toggle').css({
  480. 'background-image':'url('+src+')'
  481. })
  482. })
  483. $('.leaflet-control-layers-toggle').css({
  484. 'background-image':'url('+mapConfig.layers[0].imgUrl+')'
  485. })
  486. }
  487. function newLayerAddToMap(layerConfig,map){
  488. map.addLayer(newLayerGroup(layerConfig));
  489. }
  490. function newLayerGroup(layerConfig){
  491. var layerGroup = L.layerGroup();
  492. // layerGroup.setZIndex(10)
  493. layerGroup.addLayer(newLayerByType(layerConfig));
  494. if(layerConfig.subLayer && layerConfig.subLayer.length > 0 ){
  495. for( var s in layerConfig.subLayer )
  496. {
  497. layerGroup.addLayer(newLayerByType(layerConfig.subLayer[s]));
  498. }
  499. }
  500. return layerGroup;
  501. }
  502. function newLayerByType(layerConfig){
  503. var layer;
  504. if(layerConfig.type === 'wmts')
  505. {
  506. layer = new L.tileLayer.wmts( layerConfig.url, layerConfig.option );
  507. }else if(layerConfig.type === 'wms')
  508. {
  509. layer = new L.tileLayer.wms(layerConfig.url, layerConfig.option);
  510. }else if(layerConfig.type ==='GXYZ'){
  511. layer = new L.GXYZ(layerConfig.url,layerConfig.option);
  512. }else if(layerConfig.type ==='GWVTAnno'){
  513. layer = new L.GWVTAnno(layerConfig.option);
  514. var dataSource = new Custom.URLDataSource();
  515. dataSource.url = layerConfig.url;
  516. layer.addDataSource(dataSource);
  517. }else if(layerConfig.type ==='gd'){
  518. layer = L.tileLayer(
  519. layerConfig.url,
  520. layerConfig.option
  521. )
  522. }
  523. layer.type = layerConfig.type;
  524. return layer ;
  525. }
  526. function doXHR(url,callback){
  527. var httpXml = createXMLHttpRequest();
  528. httpXml.open('get', url, true);
  529. httpXml.send(null);
  530. httpXml.onreadystatechange = function() {
  531. if(httpXml.readyState == 4 && httpXml.status == 200) {
  532. var data = JSON.parse(httpXml.responseText);
  533. if(callback){
  534. callback(data);
  535. }
  536. }
  537. };
  538. }
  539. function createXMLHttpRequest() {
  540. var xmlHttp;
  541. try{
  542. xmlHttp = new XMLHttpRequest();
  543. } catch (e) {
  544. // 适用于IE6
  545. try {
  546. xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  547. } catch (e) {
  548. // 适用于IE5.5,以及IE更早版本
  549. try{
  550. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  551. } catch (e){}
  552. }
  553. }
  554. return xmlHttp;
  555. }