factoryindex4.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view class="page">
  3. <!-- 1. 顶部标题 -->
  4. <view class="header">
  5. <text class="title">{{title}}</text>
  6. </view>
  7. <!-- 2. 轮播图 -->
  8. <swiper
  9. class="swiper"
  10. circular
  11. :indicator-dots="true"
  12. :autoplay="true"
  13. :interval="3000"
  14. :duration="800"
  15. >
  16. <swiper-item v-for="(item, index) in picture" :key="index">
  17. <image :src="item" class="swiper-item" mode="aspectFill" />
  18. </swiper-item>
  19. </swiper>
  20. <!-- 3. 地图 -->
  21. <view class="map-box">
  22. <view @touchend="onTouchEnd" class="map-wrap">
  23. <canvas canvas-id="ydChart" id="ydChart" class="map-canvas" />
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import * as echarts from 'echarts'
  30. export default {
  31. data() {
  32. return {
  33. chart: null,
  34. title:'盐都区闲置厂房分布图',
  35. /* 轮播图示例,换成你的真实图片即可 */
  36. picture:["https://ydwqfw.com.cn/yd_qycpfbH5/bg1.jpg",
  37. "https://ydwqfw.com.cn/yd_qycpfbH5/bg2.jpg",
  38. "https://ydwqfw.com.cn/yd_qycpfbH5/bg3.jpg",
  39. "https://ydwqfw.com.cn/yd_qycpfbH5/bg4.jpg"],
  40. }
  41. },
  42. async onReady() {
  43. const json = await this.loadGeoJSON()
  44. echarts.registerMap('yanduqu', json)
  45. let canvasNode
  46. // #ifdef H5
  47. canvasNode = document.getElementById('ydChart')
  48. // #endif
  49. // #ifndef H5
  50. canvasNode = await new Promise(resolve => {
  51. uni.createSelectorQuery()
  52. .in(this)
  53. .select('#ydChart')
  54. .node(res => resolve(res.node))
  55. .exec()
  56. })
  57. // #endif
  58. this.chart = echarts.init(canvasNode, null, {
  59. width : uni.getSystemInfoSync().windowWidth,
  60. height: uni.getSystemInfoSync().windowHeight * 0.6 // 地图占屏幕 50%
  61. })
  62. this.renderMap()
  63. },
  64. methods: {
  65. loadGeoJSON() {
  66. return new Promise((resolve, reject) => {
  67. uni.request({
  68. url: './static/yandu.json',
  69. method: 'GET',
  70. success: res => resolve(res.data),
  71. fail: reject
  72. })
  73. })
  74. },
  75. goList(parkName) {
  76. // 真正跳转
  77. uni.navigateTo({
  78. url: `/pages/factoryBuildings/factoryBuildingsList?parkid=${parkName}`
  79. })
  80. },
  81. onTouchEnd(e) {
  82. const touch = e.changedTouches[0]
  83. uni.createSelectorQuery()
  84. .in(this)
  85. .select('.map-wrap')
  86. .boundingClientRect(rect => {
  87. const dpr = uni.getSystemInfoSync().pixelRatio // 关键1:拿到 dpr
  88. const xCss = touch.clientX - rect.left // CSS 像素
  89. const yCss = touch.clientY - rect.top
  90. const xCanvas = xCss ; // 关键2:转成 canvas 物理像素
  91. const yCanvas = yCss ;
  92. // 用物理像素判断
  93. const inGeo = this.chart.containPixel('geo', [xCanvas, yCanvas])
  94. if (inGeo) {
  95. // 拿到逻辑坐标(供 emit 用)
  96. const [lng, lat] = this.chart.convertFromPixel('geo', [xCanvas, yCanvas])
  97. // 直接 emit 一个 click 事件,把 name 传出去
  98. // 先用 chart.getOption().series[0].data 里匹配,这里简化:用 convert 回来的坐标反查
  99. const geoModel = this.chart.getModel().getComponent('geo', 0)
  100. const region = geoModel.coordinateSystem.getRegionByCoord([lng, lat])
  101. const name = region ? region.name : ''
  102. if (region) {
  103. this.goList(name)
  104. }
  105. }
  106. })
  107. .exec()
  108. },
  109. renderMap() {
  110. const option = {
  111. geo: [{
  112. map: 'yanduqu',
  113. roam: true,
  114. aspectScale: 1.2,
  115. zoom: 1.3,
  116. itemStyle: {
  117. normal: {
  118. areaColor: {
  119. type: 'linear-gradient',
  120. x: 0,
  121. y: 400,
  122. x2: 0,
  123. y2: 0,
  124. colorStops: [{
  125. offset: 0,
  126. color: 'rgba(37,108,190,0.8)' // 0% 处的颜色
  127. }, {
  128. offset: 1,
  129. color: 'rgba(15,169,195,0.8)' // 50% 处的颜色
  130. }],
  131. global: true // 缺省为 false
  132. },
  133. borderColor: '#4ecee6',
  134. borderWidth: 1
  135. },
  136. emphasis: {
  137. areaColor: {
  138. type: 'linear-gradient',
  139. x: 0,
  140. y: 300,
  141. x2: 0,
  142. y2: 0,
  143. colorStops: [{
  144. offset: 0,
  145. color: 'rgba(37,108,190,1)' // 0% 处的颜色
  146. }, {
  147. offset: 1,
  148. color: 'rgba(15,169,195,1)' // 50% 处的颜色
  149. }],
  150. global: false // 缺省为 false
  151. },
  152. }
  153. },
  154. emphasis: {
  155. itemStyle: { areaColor: '#ffca28' }
  156. },
  157. // 关键:打开标签
  158. label: {
  159. show: true, // 默认显示名称
  160. color: '#fff', // 文字颜色
  161. fontSize: 12, // 字号
  162. fontWeight: 'bold',
  163. }
  164. }],
  165. series: [{ type: 'map', geoIndex: 0, data: [] }]
  166. }
  167. this.chart.setOption(option)
  168. this.chart.on('click', params => {
  169. uni.navigateTo({
  170. url: `/pages/factoryBuildings/factoryBuildingsList?parkid=${params.name}`
  171. })
  172. })
  173. }
  174. }
  175. }
  176. </script>
  177. <style scoped>
  178. .page {
  179. display: flex;
  180. flex-direction: column;
  181. height: 100vh;
  182. background: #f5f5f5;
  183. }
  184. /* 顶部标题 */
  185. .header {
  186. padding: 20rpx 0;
  187. text-align: center;
  188. background: #fff;
  189. }
  190. .title {
  191. font-size: 36rpx;
  192. font-weight: bold;
  193. color: #333;
  194. }
  195. /* 轮播图 */
  196. .swiper {
  197. width: 100%;
  198. height: 300rpx;
  199. }
  200. .swiper-item {
  201. width: 100%;
  202. height: 100%;
  203. }
  204. /* 地图容器 */
  205. .map-box {
  206. flex: 1;
  207. width: 100%;
  208. /* background: #003366; */ /* 任意你想要的深色 */
  209. }
  210. .map-canvas {
  211. width: 100%;
  212. height: 100%;
  213. }
  214. </style>