MapView.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <div class="map">
  3. <div id="mapDiv" v-show="maptype == 'normal'"></div>
  4. <template>
  5. <div id="mapCon1" v-show="maptype == 'split'"></div>
  6. <div id="mapCon2" v-show="maptype == 'split'"></div>
  7. </template>
  8. <!-- 单屏影像切换 -->
  9. <div class="ImageList" v-show="maptype == 'normal'">
  10. <div
  11. :class="['item', item.imgName]"
  12. @click="shpBaseMap('mapDiv', item.index)"
  13. v-for="(item, i) in containerList"
  14. :key="i"
  15. >
  16. <span class="label"> {{ item.name }} </span>
  17. </div>
  18. </div>
  19. <!-- 分屏的影像切换 -->
  20. <div v-show="maptype == 'split'">
  21. <div class="ImageList ImageList1">
  22. <div
  23. :class="['item', item.imgName]"
  24. @click="shpBaseMap('mapCon1', item.index)"
  25. v-for="(item, i) in containerList"
  26. :key="i"
  27. >
  28. <span class="label"> {{ item.name }} </span>
  29. </div>
  30. </div>
  31. <div class="ImageList">
  32. <div
  33. :class="['item', item.imgName]"
  34. @click="shpBaseMap('mapCon2', item.index)"
  35. v-for="(item, i) in containerList"
  36. :key="i"
  37. >
  38. <span class="label"> {{ item.name }} </span>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </template>
  44. <script>
  45. import { ref, onMounted } from "vue";
  46. import "ol/ol.css";
  47. import { get as getProjection, transform } from "ol/proj.js";
  48. import { Map, View } from "ol";
  49. import Tile from "ol/layer/Tile";
  50. import OSM from "ol/source/OSM";
  51. import { Vector as SourceVec, XYZ } from "ol/source";
  52. import { defaults as defaultControls, ScaleLine } from "ol/control";
  53. export default {
  54. props: {
  55. maptype: {
  56. type: String,
  57. default: "normal",
  58. },
  59. },
  60. data() {
  61. return {
  62. // type: "normal",
  63. maps: {},
  64. curPageResultLayer: null,
  65. vectorLayer: null,
  66. vecLayer: null, //矢量地图
  67. cvaLayer: null, //矢量标注
  68. imgLayer: null, //影像地图
  69. imgCiaLayer: null, //影像标注
  70. terLayer: null, //地形晕染
  71. ctaLayer: null, //地形标注
  72. containerList: [
  73. {
  74. name: "矢量地图",
  75. index: 1,
  76. imgName: "shiliangditu",
  77. },
  78. {
  79. name: "影像地图",
  80. index: 2,
  81. imgName: "yingxiangditu",
  82. },
  83. {
  84. name: "地形晕染",
  85. index: 3,
  86. imgName: "dixingyunran",
  87. },
  88. ],
  89. };
  90. },
  91. methods: {
  92. shpBaseMap(container, base) {
  93. // this.changeBaseLayers("mapDiv", 2);
  94. this.changeBaseLayers(container, base);
  95. },
  96. createMap() {
  97. var view = new View({
  98. center: transform([103.23, 35.33], "EPSG:4326", "EPSG:3857"), //地图初始中心点
  99. // center: gcoord.transform(
  100. // [106.67591743605254, 38.21012898330025],
  101. // gcoord.WGS84,
  102. // gcoord.BD09
  103. // ), //地图初始中心点
  104. projection: "EPSG:4326",
  105. zoom: 4,
  106. minZoom: 1,
  107. });
  108. this.addMap("mapDiv", view);
  109. // } else {
  110. this.addMap("mapCon1", view);
  111. this.addMap("mapCon2", view);
  112. // }
  113. },
  114. changeBaseLayers(container, base) {
  115. let indexs = [];
  116. switch (base) {
  117. case 1:
  118. indexs = [0, 1];
  119. break;
  120. case 2:
  121. indexs = [2, 3];
  122. break;
  123. case 3:
  124. indexs = [4, 5];
  125. break;
  126. }
  127. for (let i = 0; i <= 5; i++) {
  128. this.maps[container]
  129. .getAllLayers()
  130. [i].setVisible(indexs.indexOf(i) > -1);
  131. }
  132. },
  133. addMap(target, view) {
  134. if (this.maps[target]) return;
  135. // var gaodeMapLayer = new TileLayer({
  136. // title: "高德地图",
  137. // // source: new XYZ({
  138. // // url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}",
  139. // // wrapX: false,
  140. // // }),
  141. // source: new XYZ({
  142. // url: `https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}?token=${this.arcgisKey}`,
  143. // // url: `https://wayback.maptiles.arcgis.com/arcgis/rest/services/World_Imagery/WMTS/1.0.0/default028mm/MapServer/tile/4905/{z}/{y}/{x}`,
  144. // crossOrigin: "anonymous",
  145. // // attributions: ["ArcGIS", new Date().getFullYear()],
  146. // }),
  147. // });
  148. //实例化比例尺控件(ScaleLine)
  149. var scaleLineControl = new ScaleLine({
  150. //设置比例尺单位,degrees、imperial、us、nautical、metric(度量单位)
  151. units: "metric",
  152. });
  153. var key = "12df6e32906dbb916fad14dc65ebdbf8"; // 请替换成你的key
  154. // 矢量底图
  155. let vecLayer = new Tile({
  156. source: new XYZ({
  157. url:
  158. "http://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=" +
  159. key,
  160. }),
  161. visible: true,
  162. });
  163. // 矢量标注
  164. let cvaLayer = new Tile({
  165. source: new XYZ({
  166. url:
  167. "http://t0.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=" +
  168. key,
  169. }),
  170. visible: true,
  171. });
  172. // 影像底图
  173. let imgLayer = new Tile({
  174. source: new XYZ({
  175. url:
  176. "http://t0.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=" +
  177. key,
  178. }),
  179. visible: false,
  180. });
  181. // 影像标注
  182. let imgCiaLayer = new Tile({
  183. source: new XYZ({
  184. url:
  185. "http://t0.tianditu.gov.cn/cia_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=" +
  186. key,
  187. }),
  188. visible: false,
  189. });
  190. // 地形晕染
  191. let terLayer = new Tile({
  192. source: new XYZ({
  193. url:
  194. "http://t0.tianditu.gov.cn/ter_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ter&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=" +
  195. key,
  196. }),
  197. visible: false,
  198. });
  199. // 地形标注层
  200. let ctaLayer = new Tile({
  201. source: new XYZ({
  202. url:
  203. "http://t0.tianditu.gov.cn/cta_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cta&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=" +
  204. key,
  205. }),
  206. visible: false,
  207. });
  208. let map = new Map({
  209. //地图容器div的ID
  210. target,
  211. //地图容器中加载的图层
  212. // layers: [gaodeMapLayer],
  213. layers: [vecLayer, cvaLayer, imgLayer, imgCiaLayer, terLayer, ctaLayer], //
  214. // layers: [this.imgLayer,this.imgCiaLayer,],
  215. //地图视图设置
  216. view, //同一个view
  217. controls: defaultControls({
  218. //地图中默认控件
  219. attribution: false, //地图数据源信息控件是否可收缩,默认为true
  220. }).extend([scaleLineControl]), //加载鼠标位置控件
  221. });
  222. this.maps[target] = map;
  223. window.map = this.maps;
  224. map.on("moveend", function (e) {
  225. var zoom = map.getView().getZoom(); //获取当前地图的缩放级别
  226. console.log(zoom);
  227. if (zoom >= 12) {
  228. // tianjinlayer.getSource().clear(); //控制地图图层不可见
  229. console.log("我大于12");
  230. } else {
  231. console.log("我小于12");
  232. }
  233. });
  234. },
  235. },
  236. mounted() {
  237. this.createMap();
  238. },
  239. watch: {
  240. maptype() {
  241. this.$nextTick(() => {
  242. window.map.mapDiv.updateSize(); // 更新地图尺寸
  243. window.map.mapCon1.updateSize(); // 更新地图尺寸
  244. window.map.mapCon2.updateSize(); // 更新地图尺寸
  245. });
  246. },
  247. },
  248. };
  249. </script>
  250. <style lang="scss" scoped>
  251. .map {
  252. // width: calc(100% - 520px);
  253. width: 70%;
  254. height: 91vh;
  255. position: relative;
  256. }
  257. #mapDiv {
  258. width: 100%;
  259. height: 100%;
  260. }
  261. #mapCon2 {
  262. position: absolute;
  263. top: 0px;
  264. left: 50%;
  265. }
  266. #mapCon1 {
  267. position: absolute;
  268. top: 0px;
  269. left: 0%;
  270. }
  271. #mapCon1,
  272. #mapCon2 {
  273. width: 50%;
  274. height: 100%;
  275. }
  276. .ImageList {
  277. width: 265px;
  278. height: 70px;
  279. // background-color: rgba(255, 192, 203, 0.427);
  280. position: absolute;
  281. bottom: 20px;
  282. // left: calc(50% - 100px);
  283. left: calc(100% - 290px);
  284. z-index: 40;
  285. display: flex;
  286. justify-content: space-around;
  287. align-items: center;
  288. .item {
  289. width: 76px;
  290. height: 57px;
  291. background: rgba(14, 30, 69, 0.52);
  292. line-height: 30px;
  293. text-align: center;
  294. cursor: pointer;
  295. margin-right: 6px;
  296. position: relative;
  297. border-radius: 1.5px;
  298. // box-sizing: content-box;
  299. // box-sizing: border-box;
  300. .label {
  301. display: inline-block;
  302. width: 146px;
  303. height: 26px;
  304. line-height: 26px;
  305. position: absolute;
  306. right: 2px;
  307. bottom: 2px;
  308. font-size: 21px;
  309. background: #dddddd8f;
  310. transform: scale(0.5);
  311. transform-origin: bottom right;
  312. border-radius: 2px 0 0 2px;
  313. color: rgb(19, 113, 251);
  314. }
  315. }
  316. .item1 {
  317. position: absolute;
  318. bottom: 20px;
  319. // left: calc(50% - 100px);
  320. left: calc(50% - 290px);
  321. }
  322. .shiliangditu {
  323. background: url("@/assets/dituimg/shiliangditu.png") no-repeat !important;
  324. background-size: 100% 100% !important;
  325. }
  326. .yingxiangditu {
  327. background: url("@/assets/dituimg/yingxiangditu.png") no-repeat;
  328. background-size: 100% 100%;
  329. }
  330. .dixingyunran {
  331. background: url("@/assets/dituimg/dixingyunran.png") no-repeat;
  332. background-size: 100% 100%;
  333. }
  334. }
  335. .ImageList1 {
  336. position: absolute;
  337. bottom: 20px;
  338. // left: calc(50% - 100px);
  339. left: calc(50% - 290px);
  340. z-index: 40;
  341. }
  342. </style>