MapView.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view id="container" class=""></view>
  3. </template>
  4. <script>
  5. import "ol/ol.css";
  6. import gcoord from "gcoord";
  7. import { transform } from "ol/proj.js";
  8. import Map from "ol/Map";
  9. import View from "ol/View";
  10. // import TileLayer from "ol/layer/Tile";
  11. import XYZ from "ol/source/XYZ";
  12. import Tile from "ol/layer/Tile";
  13. // const getUserInfo = () => {
  14. // apiUserInfo().then(res => {
  15. // console.log(res);
  16. // userinfo.value = res.data
  17. // })
  18. // }
  19. let map: any = null;
  20. export default {
  21. name: "map-view",
  22. data() {
  23. return {
  24. maps: {},
  25. };
  26. },
  27. mounted() {
  28. // this.initAMap();
  29. this.createMap();
  30. },
  31. unmounted() {
  32. map && map.destroy();
  33. },
  34. methods: {
  35. initAMap() {
  36. map = new AMap.Map("container", {
  37. viewMode: "3D", // 是否为3D地图模式
  38. zoom: 11,
  39. center: [116.397428, 39.90923],
  40. });
  41. // 设置离线地图
  42. map.setPlugins({
  43. "AMap.Offline": {},
  44. });
  45. // 添加离线地图数据
  46. map.offline.setCity("北京");
  47. },
  48. createMap() {
  49. var view = new View({
  50. center: transform([103.23, 35.33], "EPSG:4326", "EPSG:3857"), //地图初始中心点
  51. // center: gcoord.transform([103.23, 35.33], "EPSG:4326", "EPSG:3857"), //地图初始中心点
  52. // [106.67591743605254, 38.21012898330025],
  53. // gcoord.WGS84,
  54. // gcoord.BD09
  55. // ), //地图初始中心点
  56. projection: "EPSG:4326",
  57. zoom: 4,
  58. minZoom: 1,
  59. });
  60. this.addMap("container", view);
  61. },
  62. addMap(target: any, view) {
  63. if (this.maps[target]) return;
  64. // var gaodeMapLayer = new TileLayer({
  65. // title: "高德地图",
  66. // source: new XYZ({
  67. // url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}",
  68. // wrapX: false,
  69. // }),
  70. // });
  71. var key = "12df6e32906dbb916fad14dc65ebdbf8"; // 请替换成你的key
  72. // 矢量底图
  73. var vecLayer = new Tile({
  74. source: new XYZ({
  75. url:
  76. "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=" +
  77. key,
  78. }),
  79. });
  80. // 矢量标注
  81. var cvaLayer = new Tile({
  82. source: new XYZ({
  83. url:
  84. "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=" +
  85. key,
  86. }),
  87. });
  88. // 影像底图
  89. var imgLayer = new Tile({
  90. source: new XYZ({
  91. url:
  92. "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=" +
  93. key,
  94. }),
  95. });
  96. // 影像标注
  97. var imgCiaLayer = new Tile({
  98. source: new XYZ({
  99. url:
  100. "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=" +
  101. key,
  102. }),
  103. });
  104. // 地形晕染
  105. var terLayer = new Tile({
  106. source: new XYZ({
  107. url:
  108. "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=" +
  109. key,
  110. }),
  111. });
  112. // 地形标注层
  113. var ctaLayer = new Tile({
  114. source: new XYZ({
  115. url:
  116. "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=" +
  117. key,
  118. }),
  119. });
  120. let map = new Map({
  121. //地图容器div的ID
  122. target,
  123. //地图容器中加载的图层
  124. // layers: [gaodeMapLayer],
  125. layers: [vecLayer, cvaLayer],
  126. //地图视图设置
  127. view, //同一个view
  128. });
  129. this.maps[target] = map;
  130. window.map = this.maps;
  131. console.log(window.map, "window.map");
  132. },
  133. },
  134. };
  135. </script>
  136. <style scoped>
  137. #container {
  138. width: 100%;
  139. height: 100vh;
  140. }
  141. </style>