cmap.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <div>
  3. <div id="container" class="g-bg1"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import AMapLoader from '@amap/amap-jsapi-loader'
  8. window.map = {}
  9. export default {
  10. components: {},
  11. data() {
  12. return {}
  13. },
  14. computed: {},
  15. async created() {},
  16. mounted() {
  17. this.initAMap()
  18. },
  19. unmounted() {
  20. window.map.destroy()
  21. },
  22. methods: {
  23. async initAMap() {
  24. const json = await this.$http.get(
  25. `https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=520600_full`
  26. )
  27. window._AMapSecurityConfig = {
  28. securityJsCode: `a2017cdc059be6c03b430a3185098003`,
  29. }
  30. AMapLoader.load({
  31. key: `6260c9b9128ad802001c8a5ff18bde16`, // 申请好的Web端开发者Key,首次调用 load 时必填
  32. version: `2.0`, // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  33. plugins: [`AMap.Scale`], // 需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['...','...']
  34. })
  35. .then((AMap) => {
  36. let content = [
  37. `<div class='info_box_contant'> ` + `vo.aname` + ` </div>`,
  38. ]
  39. function addPolygon(data) {
  40. const { name, center } = data.properties
  41. const path = data.geometry.coordinates
  42. let polygon = new AMap.Polygon({
  43. path: path,
  44. fillColor: `#ccebc5`,
  45. strokeOpacity: 1,
  46. fillOpacity: 0,
  47. strokeColor: `#2b8cbe`,
  48. strokeWeight: 4,
  49. // strokeStyle: `dashed`,
  50. strokeDasharray: [5, 5],
  51. lineJoin: `round`,
  52. lineCap: `round`,
  53. })
  54. polygon.on(`mouseover`, (e) => {
  55. console.log(`mouseover`)
  56. // 如果缩放过大还变色, 万一人家是想看放大后的详细地点, 变色后反而不方便
  57. if (window.map.getZoom() <= 11) {
  58. // infoWindow.open(window.map, [e.lnglat.lng, e.lnglat.lat]) // 后面的参数指的是经纬度,在此显示窗口
  59. infoWindow.open(window.map, center) // 后面的参数指的是经纬度,在此显示窗口
  60. polygon.setOptions({
  61. fillOpacity: 0.7,
  62. fillColor: `#7bccc4`,
  63. })
  64. }
  65. })
  66. polygon.on(`click`, (e) => {
  67. console.log(`点击了`, data.properties)
  68. })
  69. polygon.on(`mouseout`, () => {
  70. polygon.setOptions({
  71. fillOpacity: 0,
  72. fillColor: `#ccebc5`,
  73. })
  74. infoWindow.close()
  75. })
  76. window.map.add(polygon)
  77. }
  78. const infoWindow = new AMap.InfoWindow({
  79. isCustom: true, // 使用自定义窗体
  80. content: content.join(`<br>`),
  81. offset: new AMap.Pixel(-15, -25),
  82. })
  83. window.map = new AMap.Map(`container`, {
  84. // resizeEnable: true,
  85. // 设置地图容器id
  86. // viewMode: `2D`, // 2D视图
  87. // mapStyle: `amap://styles/whitesmoke`, // 卫星地图样式
  88. viewMode: `3D`, // 是否为3D地图模式
  89. // zoom: 7, // 初始化地图级别
  90. zoom: 9.22, // 初始化地图级别
  91. // center: [107.943579, 30.131735], // 初始化地图中心点位置
  92. center: [108.597461, 28.047503], // 初始化地图中心点位置
  93. resizeEnable: true,
  94. layers: [
  95. new AMap.TileLayer.Satellite(),
  96. new AMap.TileLayer.RoadNet(),
  97. // 谷歌图
  98. // new AMap.TileLayer({
  99. // // getTileUrl: `//mt1.google.cn/vt/lyrs=s&hl=zh-CN&gl=CN&x=[x]&y=[y]&z=[z]&s=Gali`,
  100. // getTileUrl: `http://mt{1,2,3,0}.google.cn/vt/lyrs=y@142&hl=zh-CN&gl=cn&x=[x]&y=[y]&z=[z]&s=Galil`,
  101. // zIndex: 20,
  102. // zoom: 17,
  103. // }),
  104. // new AMap.TileLayer({
  105. // getTileUrl: `//mt1.google.cn/vt/imgtp=png32&lyrs=h@207000000&hl=zh-CN&gl=cn&x=[x]&y=[y]&z=[z]&s=Galil`,
  106. // zIndex: 20,
  107. // zoom: 17,
  108. // }),
  109. // 天地图
  110. // new AMap.TileLayer({
  111. // getTileUrl: `//t{0,1,2,3,4,5,6,7}.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=d7fcf6db577fd86e0cc48e9169065727`,
  112. // zIndex: 20,
  113. // zoom: 17,
  114. // }),
  115. // new AMap.TileLayer({
  116. // getTileUrl: `//t{0,1,2,3,4,5,6,7}.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=d7fcf6db577fd86e0cc48e9169065727`,
  117. // zIndex: 20,
  118. // zoom: 17,
  119. // }),
  120. ],
  121. // zooms: [16, 20],
  122. })
  123. window.map.setFeatures([`point`]) // 单一种类要素显示
  124. let list = json.features.map((item) => item)
  125. list.forEach((item) => {
  126. addPolygon(item)
  127. })
  128. })
  129. .catch((e) => {
  130. console.log(e)
  131. })
  132. },
  133. tabChange(val) {
  134. if (val === 0) {
  135. this.$router.push(`/page1`)
  136. }
  137. console.log(`val`, val)
  138. },
  139. },
  140. }
  141. </script>
  142. <style lang="less" scoped>
  143. #container {
  144. position: absolute;
  145. padding: 0px;
  146. margin: 0px;
  147. width: 100vw;
  148. height: 100vh;
  149. z-index: 8;
  150. &::after {
  151. content: '';
  152. display: block;
  153. width: 100%;
  154. height: 100%;
  155. position: absolute;
  156. z-index: 9;
  157. background: radial-gradient(transparent 40vh, rgba(0, 0, 0, 1));
  158. pointer-events: none;
  159. }
  160. }
  161. </style>