factoryindex5.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view class="page">
  3. <!-- 顶部标题 -->
  4. <view class="header">
  5. <text class="title">{{ title }}</text>
  6. <text class="list-btn" @tap="goList('全部')">列表</text>
  7. </view>
  8. <!-- 轮播图 -->
  9. <swiper
  10. class="swiper"
  11. circular
  12. :indicator-dots="true"
  13. :autoplay="true"
  14. :interval="3000"
  15. :duration="800"
  16. >
  17. <swiper-item v-for="(src, i) in pictures" :key="i">
  18. <image :src="src" mode="aspectFill" class="swiper-item" />
  19. </swiper-item>
  20. </swiper>
  21. <!-- 腾讯地图 -->
  22. <map
  23. id="tencentMap"
  24. class="tmap"
  25. :longitude="centerLng"
  26. :latitude="centerLat"
  27. :scale="scale"
  28. :subkey="mapKey"
  29. :show-location="false"
  30. :enable-zoom="true"
  31. :enable-scroll="true"
  32. @tap="mapTap"
  33. @regionchange="regionChange"
  34. >
  35. <!-- 各镇覆盖物(polygon) -->
  36. <polygon
  37. v-for="item in townPolygons"
  38. :key="item.name"
  39. :points="item.points"
  40. :strokeWidth="2"
  41. :strokeColor="item.stroke"
  42. :fillColor="item.fill"
  43. @tap="tapTown(item.name)"
  44. />
  45. </map>
  46. </view>
  47. </template>
  48. <script>
  49. import loginService from '@/api/auth/loginService'
  50. const qqMap = require('../../libs/qqmap-wx-jssdk.min.js') // 官方下载
  51. const qqmapsdk = new qqMap({ key: 'N4YBZ-EB2WZ-JCIXK-7G6R5-6NJNV-7OFNK' })
  52. export default {
  53. data() {
  54. return {
  55. title: '盐都区闲置厂房分布图',
  56. pictures: [
  57. 'https://ydwqfw.com.cn/yd_qycpfbH5/bg1.jpg',
  58. 'https://ydwqfw.com.cn/yd_qycpfbH5/bg2.jpg',
  59. 'https://ydwqfw.com.cn/yd_qycpfbH5/bg3.jpg',
  60. 'https://ydwqfw.com.cn/yd_qycpfbH5/bg4.jpg'
  61. ],
  62. mapKey: 'N4YBZ-EB2WZ-JCIXK-7G6R5-6NJNV-7OFNK',
  63. centerLng: 120.156, // 盐都区政府大致经度
  64. centerLat: 33.108,
  65. scale: 10, // 初始缩放
  66. townCntMap: new Map(), // name -> building_cnt
  67. townPolygons: [] // 供给 <polygon> 组件
  68. }
  69. },
  70. async onLoad() {
  71. await this.getList() // 先拉取各镇厂房数量
  72. await this.loadDistrictBorder() // 再画行政区
  73. },
  74. methods: {
  75. /* 1. 获取各镇厂房数,与之前逻辑完全一致 */
  76. getList() {
  77. return loginService.getParkList().then(({ data }) => {
  78. let all = 0
  79. data.forEach(item => { all += item.building_cnt })
  80. this.townCntMap = new Map(data.map(item => [item.label, item.building_cnt]))
  81. })
  82. },
  83. /* 2. 拉取盐都区边界并拆成各镇 */
  84. /* 2. 通过 WebService 拿盐都区各镇边界 */
  85. loadDistrictBorder() {
  86. return new Promise((resolve, reject) => {
  87. uni.request({
  88. url: 'https://apis.map.qq.com/ws/district/v1/getchildren',
  89. method: 'GET',
  90. data: {
  91. keyword: '盐都区',
  92. key: this.mapKey, // 你申请的 key
  93. output: 'json',
  94. extensions: 'all' // 返回边界 polyline
  95. },
  96. success: res => {
  97. const data = res.data
  98. if (data.status !== 0) {
  99. console.error('district api error:', data.message)
  100. reject(data.message)
  101. return
  102. }
  103. // data.result[0] 就是盐都区下属镇/街道
  104. const towns = data.result[0]
  105. const polygons = []
  106. towns.forEach(t => {
  107. // 边界字符串 -> 经纬度数组
  108. const path = []
  109. if (t.polyline) {
  110. t.polyline.split(';').forEach(ll => {
  111. const [lat, lng] = ll.split(',').map(Number)
  112. path.push({ latitude: lat, longitude: lng })
  113. })
  114. }
  115. const cnt = this.townCntMap.get(t.fullname) || 0
  116. polygons.push({
  117. name: t.fullname,
  118. points: path,
  119. fill: cnt > 0 ? '#4A90E2AA' : '#E0E0E0AA',
  120. stroke: cnt > 0 ? '#ffffff' : '#cccccc'
  121. })
  122. })
  123. this.townPolygons = polygons
  124. resolve()
  125. },
  126. fail: reject
  127. })
  128. })
  129. },
  130. /* 3. 点击镇面 -> 跳转列表 */
  131. tapTown(name) {
  132. uni.navigateTo({ url: `/pages/factoryBuildings/factoryBuildingsList?parkid=${name}` })
  133. },
  134. /* 4. 右上角列表按钮 */
  135. goList() {
  136. uni.navigateTo({ url: '/pages/factoryBuildings/factoryBuildingsList?parkid=全部' })
  137. },
  138. /* 5. 地图空白处点一下,关闭气泡(这里没气泡,可留空) */
  139. mapTap(e) {},
  140. regionChange(e) {}
  141. }
  142. }
  143. </script>
  144. <style scoped>
  145. .page {
  146. display: flex;
  147. flex-direction: column;
  148. height: 100vh;
  149. background: #f5f5f5;
  150. }
  151. .header {
  152. position: relative;
  153. padding: 20rpx 0;
  154. text-align: center;
  155. background: #fff;
  156. }
  157. .title {
  158. font-size: 36rpx;
  159. font-weight: bold;
  160. color: #333;
  161. }
  162. .list-btn {
  163. position: absolute;
  164. right: 30rpx;
  165. top: 50%;
  166. transform: translateY(-50%);
  167. background: #1296db;
  168. color: #fff;
  169. padding: 8rpx 30rpx;
  170. border-radius: 15rpx;
  171. font-size: 28rpx;
  172. }
  173. .swiper {
  174. width: 100%;
  175. height: 300rpx;
  176. }
  177. .swiper-item {
  178. width: 100%;
  179. height: 100%;
  180. }
  181. .tmap {
  182. flex: 1;
  183. width: 100%;
  184. }
  185. </style>