67b334426ffe359eeabce5cbeb4a3bf07912e389.svn-base 19 KB

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