4dc246fbc477bda4a9ac7d3c424f6d0b0b51dfbe.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. var mapObject = null;
  2. import {removeLayerById} from './removeLayerById.js'
  3. export function addOneMapLayer(_self,map,item1,leftLayersAddedList){
  4. mapObject = map;
  5. var ol = _self.$ol;
  6. removeLayerById(_self,item1.id);
  7. let item = item1;
  8. var resolutions = [
  9. 78271.5169628125,
  10. 39135.758482729165,
  11. 19567.879240041664,
  12. 9783.939620020832,
  13. 4891.9698113333325,
  14. 2445.9849056666662,
  15. 1222.9924528333331,
  16. 611.4962264166666,
  17. 305.7481132083333,
  18. 152.87405660416664,
  19. 76.43702697916666,
  20. 38.2185148125,
  21. 19.10925608333333,
  22. 9.554628041666666,
  23. 4.777314020833333,
  24. 2.3886583333333333,
  25. 1.1943291666666667,
  26. 0.5971645833333333,
  27. 0.29858229166666667,
  28. 0.14929114583333333
  29. ]
  30. var resolutions_vector = [78271.51696402045,39135.75848201023,19567.879241005114,9783.939620502557,4891.969810251278,2445.984905125639,1222.9924525628196,611.4962262814098,305.7481131407049,152.87405657035245,76.43702828517623,38.21851414258811,19.109257071294056,9.554628535647028,4.777314267823514,2.388657133911757];
  31. var matrixIds = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
  32. var matrixIds_vector = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
  33. var projection = ol.proj.get("EPSG:3857");
  34. var projectionExtent = projection.getExtent();
  35. var tileGrid = new ol.tilegrid.TileGrid({
  36. origin: ol.extent.getTopLeft(projectionExtent), //原点(左上角)
  37. resolutions: resolutions, //分辨率数组
  38. matrixIds: matrixIds //矩阵标识列表,与地图级数保持一致
  39. });
  40. var tileGrid_vector = new ol.tilegrid.TileGrid({
  41. origin: ol.extent.getTopLeft(projectionExtent), //原点(左上角)
  42. resolutions: resolutions_vector, //分辨率数组
  43. matrixIds: matrixIds_vector //矩阵标识列表,与地图级数保持一致
  44. });
  45. if(item.checked == false){
  46. if(item.id == "oneMap_nsbd"){
  47. var lyr = new ol.layer.Tile({
  48. id:item.id,
  49. source: new ol.source.XYZ({
  50. // crossOrigin: "anonymous",
  51. projection: projection,
  52. tileGrid: tileGrid,
  53. tileUrlFunction: function (tileCoord) {
  54. var z = tileCoord[0];
  55. var x = tileCoord[1];
  56. var y = -tileCoord[2]-1;
  57. return "http://ditu.mwr.gov.cn"+item.url+"&SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=M_NSBD"+
  58. "&STYLE=Default&FORMAT=image/png&TILEMATRIXSET="+item.tileMatrixSet+"&tilecol="+x+"&tilerow="+y+"&tilematrix="+z;
  59. },
  60. wrapX: true,
  61. })
  62. });
  63. mapObject.addLayer(lyr);
  64. var layers = new ol.layer.Image({
  65. id:item.id,
  66. source: new ol.source.ImageArcGISRest({
  67. ratio: 1,
  68. params: {},
  69. url: 'http://gis.goldenwater.com.cn:6080/arcgis/rest/services/dc/nsbd/MapServer'
  70. })
  71. });
  72. mapObject.addLayer(layers)
  73. }else if(item.id.substring(7,13) != 'Vector'){//非矢量瓦片服务
  74. var lyr = new ol.layer.Tile({
  75. id:item.id,
  76. source: new ol.source.XYZ({
  77. // crossOrigin: "anonymous",
  78. projection: projection,
  79. tileGrid: tileGrid,
  80. tileUrlFunction: function (tileCoord) {
  81. var z = tileCoord[0];
  82. var x = tileCoord[1];
  83. var y = -tileCoord[2]-1;
  84. return "http://ditu.mwr.gov.cn"+item.url+"&SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER="+item.tileMatrixSet.substring(7)+
  85. "&STYLE=Default&FORMAT=image/png&TILEMATRIXSET="+item.tileMatrixSet+"&tilecol="+x+"&tilerow="+y+"&tilematrix="+z;
  86. },
  87. wrapX: true,
  88. })
  89. });
  90. mapObject.addLayer(lyr);
  91. if(item.type && item.type != ""){
  92. leftLayersAddedList.push(item.type);
  93. }
  94. }else{
  95. var tileUrl = item.url + "/tiles/{z}/{x}/{y}.mvt";
  96. if(item.id == 'oneMap_Vector_hp'){
  97. var lyr = new ol.layer.VectorTile({
  98. declutter: true,
  99. id:item.id,
  100. source: new ol.source.VectorTile({
  101. // projection: projection,
  102. // url: tileUrl,
  103. attributions:{'layerId':item.id},
  104. tileUrlFunction: function (tileCoord) {
  105. var z = tileCoord[0]+1;
  106. var x = tileCoord[1];
  107. var y = -tileCoord[2]-1;
  108. return item.url+"/tiles/"+z+"/"+x+"/"+y+".mvt";
  109. },
  110. wrapX: false,
  111. tileGrid: tileGrid_vector,
  112. format: new ol.format.MVT(),
  113. }),
  114. style: createMapboxStreetsV6Style(ol.style.Style, ol.style.Fill, ol.style.Stroke, ol.style.Icon, ol.style.Text)
  115. });
  116. }
  117. else if(item.id == "oneMap_Vector_hl"){
  118. var lyr = new ol.layer.VectorTile({
  119. declutter: true,
  120. id:item.id,
  121. source: new ol.source.VectorTile({
  122. // projection: projection,
  123. // url: tileUrl,
  124. tileUrlFunction: function (tileCoord) {
  125. var z = tileCoord[0]+1;
  126. var x = tileCoord[1];
  127. var y = -tileCoord[2]-1;
  128. return item.url+"/tiles/"+z+"/"+x+"/"+y+".mvt";
  129. },
  130. wrapX: false,
  131. tileGrid: tileGrid_vector,
  132. format: new ol.format.MVT(),
  133. }),
  134. style: createMapboxStreetsV6HlStyle(ol.style.Style, ol.style.Fill, ol.style.Stroke, ol.style.Icon, ol.style.Text)
  135. });
  136. }
  137. else if(item.id == "oneMap_Vector_sk"){
  138. var lyr = new ol.layer.VectorTile({
  139. declutter: true,
  140. id:item.id,
  141. source: new ol.source.VectorTile({
  142. // projection: projection,
  143. // url: tileUrl,
  144. tileUrlFunction: function (tileCoord) {
  145. var z = tileCoord[0]+1;
  146. var x = tileCoord[1];
  147. var y = -tileCoord[2]-1;
  148. return item.url+"/tiles/"+z+"/"+x+"/"+y+".mvt";
  149. },
  150. wrapX: false,
  151. tileGrid: tileGrid_vector,
  152. format: new ol.format.MVT(),
  153. }),
  154. style: createMapboxStreetsV6SkStyle(ol.style.Style, ol.style.Fill, ol.style.Stroke, ol.style.Icon, ol.style.Text)
  155. });
  156. }
  157. else if(item.id == "oneMap_Vector_sz"){
  158. var lyr = new ol.layer.VectorTile({
  159. declutter: true,
  160. id:item.id,
  161. source: new ol.source.VectorTile({
  162. // projection: projection,
  163. // url: tileUrl,
  164. tileUrlFunction: function (tileCoord) {
  165. var z = tileCoord[0]+1;
  166. var x = tileCoord[1];
  167. var y = -tileCoord[2]-1;
  168. return item.url+"/tiles/"+z+"/"+x+"/"+y+".mvt";
  169. },
  170. wrapX: false,
  171. tileGrid: tileGrid_vector,
  172. format: new ol.format.MVT(),
  173. }),
  174. style: createMapboxStreetsV6SzStyle(ol.style.Style, ol.style.Fill, ol.style.Stroke, ol.style.Icon, ol.style.Text)
  175. });
  176. }
  177. else if(item.id == "oneMap_Vector_sdz"){
  178. var lyr = new ol.layer.VectorTile({
  179. declutter: true,
  180. id:item.id,
  181. source: new ol.source.VectorTile({
  182. // projection: projection,
  183. // url: tileUrl,
  184. tileUrlFunction: function (tileCoord) {
  185. var z = tileCoord[0]+1;
  186. var x = tileCoord[1];
  187. var y = -tileCoord[2]-1;
  188. return item.url+"/tiles/"+z+"/"+x+"/"+y+".mvt";
  189. },
  190. wrapX: false,
  191. tileGrid: tileGrid_vector,
  192. format: new ol.format.MVT(),
  193. }),
  194. style: createMapboxStreetsV6SdzStyle(ol.style.Style, ol.style.Fill, ol.style.Stroke, ol.style.Icon, ol.style.Text)
  195. });
  196. }
  197. mapObject.addLayer(lyr);
  198. if(item.type && item.type != ""){
  199. leftLayersAddedList.push(item.type);
  200. }
  201. }
  202. }else{
  203. mapObject.getLayers().forEach((val)=>{
  204. // console.log(val.getProperties().id)
  205. if(val.getProperties().id == item.id){
  206. // val.setVisible(false);
  207. mapObject.removeLayer(val)
  208. return;
  209. }
  210. })
  211. for(var i=0;i<leftLayersAddedList.length;i++){
  212. if(leftLayersAddedList[i] == item.type){
  213. leftLayersAddedList.splice(i,1);
  214. }
  215. }
  216. }
  217. item.checked = !item.checked;
  218. }
  219. function createMapboxStreetsV6Style(Style, Fill, Stroke, Icon, Text) {
  220. var fill = new Fill({color: ''});
  221. var stroke = new Stroke({color: '', width: 1});
  222. var polygon = new Style({fill: fill});
  223. var strokedPolygon = new Style({fill: fill, stroke: stroke});
  224. var line = new Style({stroke: stroke});
  225. var text = new Style({text: new Text({
  226. text: '', fill: fill, stroke: stroke
  227. })});
  228. var styles = [];
  229. return function(feature, resolution) {
  230. var length = 0;
  231. var layer = feature.get('layer');
  232. var geom = feature.getGeometry().getType();
  233. console.log(geom);
  234. if (geom == 'Polygon') {
  235. fill.setColor('rgba(171,225,250,1.00)');
  236. styles[length++] = polygon;
  237. } else if (geom == 'Point') {
  238. text.getText().setText(feature.get('NAME'));
  239. text.getText().setFont('"SimSun Regular"');
  240. fill.setColor('rgba(255,255,255,1.00)');
  241. stroke.setColor('rgba(0,0,0,1.00)');
  242. stroke.setWidth(3);
  243. styles[length++] = text;
  244. }else {
  245. stroke.setColor('rgba(171,225,250,1.00)');
  246. stroke.setWidth(0.38);
  247. styles[length++] = line;
  248. }
  249. styles.length = length;
  250. return styles;
  251. };
  252. }
  253. function createMapboxStreetsV6HlStyle(Style, Fill, Stroke, Icon, Text) {
  254. var fill = new Fill({color: ''});
  255. var stroke = new Stroke({color: '', width: 1});
  256. var polygon = new Style({fill: fill});
  257. var strokedPolygon = new Style({fill: fill, stroke: stroke});
  258. var line = new Style({stroke: stroke});
  259. var text = new Style({text: new Text({
  260. text: '', fill: fill, stroke: stroke
  261. })});
  262. var styles = [];
  263. return function(feature, resolution) {
  264. var length = 0;
  265. var layer = feature.get('layer');
  266. var geom = feature.getGeometry().getType();
  267. text.getText().setText(feature.get('RV_NAME'));
  268. text.getText().setFont('12px SimSun Regular');
  269. fill.setColor('rgba(255,255,255,1.00)');
  270. stroke.setColor("rgba(0,155,232,1.00)");
  271. styles[length++] = text;
  272. if(layer == 'RIVER_1@river'){
  273. stroke.setWidth(3.45);
  274. styles[length++] = line;
  275. } else if(layer == 'RIVER_2@river'){
  276. stroke.setWidth(2.05);
  277. styles[length++] = line;
  278. } else if(layer == 'RIVER_3@river'){
  279. stroke.setWidth(1.91);
  280. styles[length++] = line;
  281. } else if(layer == 'RIVER_4@river'){
  282. stroke.setWidth(0.78);
  283. styles[length++] = line;
  284. } else if(layer == 'RIVER_5@river'){
  285. stroke.setWidth(0.59);
  286. styles[length++] = line;
  287. } else if(layer == 'RIVER_6@river'){
  288. stroke.setWidth(0.38);
  289. styles[length++] = line;
  290. } else if(layer == 'RIVER_7@river') {
  291. stroke.setWidth(0.38);
  292. styles[length++] = line;
  293. }
  294. styles.length = length;
  295. return styles;
  296. };
  297. }
  298. function createMapboxStreetsV6SkStyle(Style, Fill, Stroke, Icon, Text) {
  299. var fill = new Fill({color: ''});
  300. var stroke = new Stroke({color: '', width: 1});
  301. var polygon = new Style({fill: fill});
  302. var strokedPolygon = new Style({fill: fill, stroke: stroke});
  303. var line = new Style({stroke: stroke});
  304. var text = new Style({text: new Text({
  305. text: '', fill: fill, stroke: stroke
  306. })});
  307. var styles = [];
  308. return function(feature, resolution) {
  309. var length = 0;
  310. var layer = feature.get('layer');
  311. var geom = feature.getGeometry().getType();
  312. console.log(layer);
  313. console.log(geom);
  314. // if (geom == 'Point') {
  315. // text.getText().setText(feature.get('NAME'));
  316. // text.getText().setFont('11.57px SimSun Regular');
  317. // fill.setColor('rgba(115,178,255,1.00)');
  318. // styles[length++] = text;
  319. // }
  320. if (geom == 'Point') {
  321. //返回一个样式
  322. return new Style({
  323. //把点的样式换成ICON图标
  324. image: new Icon({
  325. //设置图标偏移
  326. anchor: [0.5, 1.5],
  327. anchorXUnits: 'fraction',
  328. //图片路径
  329. // imgSize: [28],
  330. src:'/src/views/zongHeXinXiOl/assets/shuiku.png'
  331. }),
  332. //文本样式
  333. text: new Text({
  334. font: '12px SimSun Regular',
  335. //文本内容
  336. text: feature.get('NAME'),
  337. // text: "",
  338. //填充样式
  339. fill: new Fill({
  340. color: '#ffffff'
  341. }),
  342. stroke: new Stroke({
  343. color:'#000000'
  344. })
  345. })
  346. });
  347. }
  348. styles.length = length;
  349. return styles;
  350. };
  351. }
  352. function createMapboxStreetsV6SzStyle(Style, Fill, Stroke, Icon, Text) {
  353. var fill = new Fill({color: ''});
  354. var stroke = new Stroke({color: '', width: 1});
  355. var polygon = new Style({fill: fill});
  356. var strokedPolygon = new Style({fill: fill, stroke: stroke});
  357. var line = new Style({stroke: stroke});
  358. var text = new Style({text: new Text({
  359. text: '', fill: fill, stroke: stroke
  360. })});
  361. var styles = [];
  362. return function(feature, resolution) {
  363. var length = 0;
  364. var layer = feature.get('layer');
  365. var geom = feature.getGeometry().getType();
  366. console.log(layer);
  367. console.log(geom);
  368. if (geom == 'Point') {
  369. //返回一个样式
  370. return new Style({
  371. //把点的样式换成ICON图标
  372. image: new Icon({
  373. //设置图标偏移
  374. anchor: [0.5, 1.5],
  375. anchorXUnits: 'fraction',
  376. //图片路径
  377. src:'/src/views/zongHeXinXiOl/assets/shuizha.png'
  378. }),
  379. //文本样式
  380. text: new Text({
  381. font: '12px SimSun Regular',
  382. //文本内容
  383. text: feature.get('NAME'),
  384. // text: "",
  385. //填充样式
  386. fill: new Fill({
  387. color: '#ffffff'
  388. }),
  389. stroke: new Stroke({
  390. color:'#000000'
  391. })
  392. })
  393. });
  394. }
  395. styles.length = length;
  396. return styles;
  397. };
  398. }
  399. function createMapboxStreetsV6SdzStyle(Style, Fill, Stroke, Icon, Text) {
  400. var fill = new Fill({color: ''});
  401. var stroke = new Stroke({color: '', width: 1});
  402. var polygon = new Style({fill: fill});
  403. var strokedPolygon = new Style({fill: fill, stroke: stroke});
  404. var line = new Style({stroke: stroke});
  405. var text = new Style({text: new Text({
  406. text: '', fill: fill, stroke: stroke
  407. })});
  408. var styles = [];
  409. return function(feature, resolution) {
  410. var length = 0;
  411. var layer = feature.get('layer');
  412. var geom = feature.getGeometry().getType();
  413. console.log(layer);
  414. console.log(geom);
  415. if (geom == 'Point') {
  416. //返回一个样式
  417. return new Style({
  418. //把点的样式换成ICON图标
  419. image: new Icon({
  420. //设置图标偏移
  421. anchor: [0.5, 1.5],
  422. //标注样式的起点位置
  423. // anchorOrigin: 'top-right',
  424. // //X方向单位:分数
  425. anchorXUnits: 'fraction',
  426. // //Y方向单位:像素
  427. // anchorYUnits: 'pixels',
  428. // //偏移起点位置的方向
  429. // offsetOrigin: 'top-right',
  430. //透明度
  431. // imgSize: [20, 20],
  432. // opacity: 0.9,
  433. //图片路径
  434. src:'/src/views/zongHeXinXiOl/assets/shuidianzhan.png'
  435. }),
  436. //文本样式
  437. text: new Text({
  438. //对齐方式
  439. // textAlign: 'center',
  440. //文本基线
  441. // textBaseline: 'middle',
  442. //字体样式
  443. font: '12px SimSun Regular',
  444. //文本内容
  445. text: feature.get('NAME'),
  446. // text: "",
  447. //填充样式
  448. fill: new Fill({
  449. color: '#ffffff'
  450. }),
  451. stroke: new Stroke({
  452. color:'#000000'
  453. })
  454. //笔触
  455. // stroke: new Stroke({
  456. // color: '#ffcc33',
  457. // width: 2
  458. // })
  459. }),
  460. //层
  461. // zIndex: 20
  462. });
  463. }
  464. styles.length = length;
  465. return styles;
  466. };
  467. }