cmap.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <div>
  3. <div class="input-card content-window-card" ref="infoBox" v-if="infoBoxData" v-show="infoBoxData">
  4. <div style="padding:7px 0px 0px 0px;">
  5. <h4>{{infoBoxData.properties.name}}</h4>
  6. <p class="input-item">完成率: {{ infoBoxData.baseItem.完成率 }}%</p>
  7. <p class="input-item">移栽面积 {{ infoBoxData.baseItem.面积 }}平方米 移栽数量 {{ infoBoxData.baseItem.已种植 }}</p>
  8. </div>
  9. </div>
  10. <div id="container" class="g-bg1"></div>
  11. </div>
  12. </template>
  13. <script>
  14. import AMapLoader from '@amap/amap-jsapi-loader'
  15. import mapOfTongren from '@/http/mapOfTongren.json'
  16. window.map = {}
  17. export default {
  18. components: {},
  19. data() {
  20. return {
  21. infoBoxData: undefined,
  22. }
  23. },
  24. computed: {},
  25. async created() { },
  26. mounted() {
  27. this.initAMap()
  28. },
  29. unmounted() {
  30. window.map.destroy()
  31. },
  32. methods: {
  33. async initAMap() {
  34. const vm = this
  35. let infoWindow
  36. //在指定位置打开信息窗体
  37. function openInfo() {
  38. const item = vm.infoBoxData
  39. const { center } = item.properties
  40. infoWindow = new AMap.InfoWindow({
  41. content: vm.$refs.infoBox,
  42. });
  43. infoWindow.open(map, center || map.getCenter());
  44. }
  45. window._AMapSecurityConfig = {
  46. securityJsCode: `a2017cdc059be6c03b430a3185098003`,
  47. }
  48. AMapLoader.load({
  49. key: `6260c9b9128ad802001c8a5ff18bde16`, // 申请好的Web端开发者Key,首次调用 load 时必填
  50. version: `2.0`, // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  51. plugins: [`AMap.Scale`], // 需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['...','...']
  52. })
  53. .then(async (AMap) => {
  54. function addPolygon(data) {
  55. const path = data.geometry.coordinates
  56. let polygon = new AMap.Polygon({
  57. path: path,
  58. fillColor: `#ccebc5`,
  59. strokeOpacity: 1,
  60. fillOpacity: 0,
  61. strokeColor: `#2b8cbe`,
  62. strokeWeight: 4,
  63. // strokeStyle: `dashed`,
  64. strokeDasharray: [5, 5],
  65. lineJoin: `round`,
  66. lineCap: `round`,
  67. })
  68. polygon.on(`mouseover`, (e) => {
  69. // 如果缩放过大还变色, 万一人家是想看放大后的详细地点, 变色后反而不方便
  70. if (window.map.getZoom() <= 11) {
  71. vm.infoBoxData = data
  72. openInfo()
  73. polygon.setOptions({
  74. fillOpacity: 0.7,
  75. fillColor: `#7bccc4`,
  76. })
  77. }
  78. })
  79. polygon.on(`click`, (e) => {
  80. console.log(`点击了`, data.properties)
  81. })
  82. polygon.on(`mouseout`, () => {
  83. polygon.setOptions({
  84. fillOpacity: 0,
  85. fillColor: `#ccebc5`,
  86. })
  87. })
  88. window.map.add(polygon)
  89. }
  90. window.map = new AMap.Map(`container`, {
  91. // resizeEnable: true,
  92. // 设置地图容器id
  93. // viewMode: `2D`, // 2D视图
  94. // mapStyle: `amap://styles/whitesmoke`, // 卫星地图样式
  95. viewMode: `3D`, // 是否为3D地图模式
  96. // zoom: 7, // 初始化地图级别
  97. zoom: 9.22, // 初始化地图级别
  98. // center: [107.943579, 30.131735], // 初始化地图中心点位置
  99. center: [108.597461, 28.047503], // 初始化地图中心点位置
  100. resizeEnable: true,
  101. layers: [
  102. new AMap.TileLayer.Satellite(),
  103. new AMap.TileLayer.RoadNet(),
  104. // 谷歌图
  105. // new AMap.TileLayer({
  106. // // getTileUrl: `//mt1.google.cn/vt/lyrs=s&hl=zh-CN&gl=CN&x=[x]&y=[y]&z=[z]&s=Gali`,
  107. // 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`,
  108. // zIndex: 20,
  109. // zoom: 17,
  110. // }),
  111. // new AMap.TileLayer({
  112. // getTileUrl: `//mt1.google.cn/vt/imgtp=png32&lyrs=h@207000000&hl=zh-CN&gl=cn&x=[x]&y=[y]&z=[z]&s=Galil`,
  113. // zIndex: 20,
  114. // zoom: 17,
  115. // }),
  116. // 天地图
  117. // new AMap.TileLayer({
  118. // 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`,
  119. // zIndex: 20,
  120. // zoom: 17,
  121. // }),
  122. // new AMap.TileLayer({
  123. // 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`,
  124. // zIndex: 20,
  125. // zoom: 17,
  126. // }),
  127. ],
  128. // zooms: [16, 20],
  129. })
  130. window.map.setFeatures([`point`]) // 单一种类要素显示
  131. const baseList = await this.$http.get(`/api/base`)
  132. let list = mapOfTongren.features.map((item) => {
  133. const baseItem = baseList.find((baseItem) => String(baseItem.区域编码) === String(item.properties.adcode)) || {}
  134. item.baseItem = baseItem
  135. return item
  136. })
  137. list.forEach((item) => {
  138. addPolygon(item)
  139. })
  140. const item = list[0]
  141. this.infoBoxData = item
  142. this.$nextTick(() => {
  143. openInfo()
  144. })
  145. })
  146. .catch((e) => {
  147. console.log(e)
  148. })
  149. },
  150. tabChange(val) {
  151. if (val === 0) {
  152. this.$router.push(`/page1`)
  153. }
  154. console.log(`val`, val)
  155. },
  156. },
  157. }
  158. </script>
  159. <style label="less">
  160. .custom-input-card {
  161. width: 18rem;
  162. }
  163. .custom-input-card .btn:last-child {
  164. margin-left: 1rem;
  165. }
  166. .content-window-card {
  167. color: #000;
  168. position: relative;
  169. width: 23rem;
  170. padding: 0.75rem 0 0 1.25rem;
  171. box-shadow: none;
  172. bottom: 0;
  173. left: 0;
  174. }
  175. .content-window-card p {
  176. height: 2rem;
  177. }
  178. </style>
  179. <style lang="less" scoped>
  180. #container {
  181. position: absolute;
  182. padding: 0px;
  183. margin: 0px;
  184. width: 1920px;
  185. height: 1080px;
  186. z-index: 8;
  187. &::after {
  188. content: '';
  189. display: block;
  190. width: 100%;
  191. height: 100%;
  192. position: absolute;
  193. z-index: 9;
  194. background: radial-gradient(transparent 40vh, rgba(0, 0, 0, 1));
  195. pointer-events: none;
  196. }
  197. }
  198. </style>