1
0

MapView.vue 9.8 KB

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