allGardenMap.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <view>
  3. <map
  4. id="mapId"
  5. :latitude="latitude"
  6. :longitude="longitude"
  7. :layers="layers"
  8. @markertap="onMarkerTap"
  9. @callouttap="onCalloutTap"
  10. @labeltap="onLabelTap"
  11. :markers="markers"
  12. :include-points="includePoints"
  13. show-location
  14. style="width: 100%; height: 100vh;"
  15. >
  16. <custom-layer
  17. id="tdtLayer"
  18. width="350"
  19. height="800"
  20. is-transparent
  21. type="raster"
  22. :src="getTileUrl"
  23. ></custom-layer>
  24. </map>
  25. </view>
  26. </template>
  27. <script setup>
  28. import { ref, onMounted } from 'vue'
  29. const latitude = ref(23.584863449735067)
  30. const longitude = ref(113.61702297075017)
  31. const markers = ref([])
  32. const includePoints = ref([])
  33. const mapCtx = ref(null)
  34. const img = ref('../../../../static/map/point.png')
  35. const TDT_KEY = 'e95115c454a663cd052d96019fd83840'
  36. const getTileUrl = ({ x, y, z }) => {
  37. const s = Math.floor(Math.random() * 8) // 0-7
  38. return `https://t1.tianditu.gov.cn/img_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILEMATRIX=${z}&TILEROW=${y}&TILECOL=${x}&tk=${TDT_KEY}`
  39. }
  40. // const layers = [
  41. // {
  42. // // 添加腾讯地图默认底图(可选)
  43. // type: 'vector',
  44. // subdomains: ['0', '1', '2'],
  45. // url: 'https://{s}.map.qq.com/tile/{z}/{x}/{y}',
  46. // zIndex: 0,
  47. // },
  48. // {
  49. // // 叠加天地图影像(WMTS)
  50. // type: 'wmts',
  51. // layer: 'img',
  52. // url: 'https://t{s}.tianditu.gov.cn/img_c/wmts?tk=e95115c454a663cd052d96019fd83840&service=wmts&request=GetTile&version=1.0.0&layer=img&style=default&tilematrixset=c&format=tiles',
  53. // // url: 'https://t{s}.tianditu.gov.cn/img_c/wmts?tk=e95115c454a663cd052d96019fd83840',
  54. // tileMatrixSet: 'c',
  55. // subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
  56. // zIndex: 1,
  57. // }
  58. // ]
  59. // 或者使用WMTS方式
  60. const layers = ref([
  61. {
  62. type: 'wmts',
  63. layer: 'img',
  64. url: 'https://t{s}.tianditu.gov.cn/img_c/wmts?tk=e95115c454a663cd052d96019fd83840',
  65. tileMatrixSet: 'c',
  66. subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
  67. zIndex: 99
  68. }
  69. ])
  70. onMounted(() => {
  71. // #ifdef MP-WEIXIN
  72. mapCtx.value = uni.createMapContext('mapId')
  73. mapCtx.value.on('markerClusterClick', res => {
  74. console.log('markerClusterClick', res)
  75. })
  76. bindEvent()
  77. // #endif
  78. })
  79. function toPage(path) {
  80. uni.navigateTo({
  81. url: `/pages/tabBar/home/subPages/${path}`
  82. });
  83. }
  84. const bindEvent = () => {
  85. // #ifdef MP-WEIXIN
  86. mapCtx.value.initMarkerCluster({
  87. enableDefaultStyle: false,
  88. zoomOnClick: true,
  89. gridSize: 60,
  90. complete(res) {
  91. console.log('initMarkerCluster', res)
  92. }
  93. })
  94. addMarkers()
  95. mapCtx.value.on('markerClusterCreate', res => {
  96. console.log('clusterCreate', res)
  97. const clusters = res.clusters
  98. const newMarkers = clusters.map(cluster => {
  99. const { center, clusterId, markerIds } = cluster
  100. return {
  101. ...center,
  102. width: 0,
  103. height: 0,
  104. clusterId, // 必须
  105. label: {
  106. content: markerIds.length + '',
  107. fontSize: 20,
  108. width: 30,
  109. height: 30,
  110. bgColor: '#FFCB3C',
  111. borderRadius: 30,
  112. textAlign: 'center',
  113. anchorX: 0,
  114. anchorY: -30,
  115. }
  116. }
  117. })
  118. mapCtx.value.addMarkers({
  119. markers: newMarkers,
  120. clear: false,
  121. complete(res) {
  122. console.log('clusterCreate addMarkers', res)
  123. }
  124. })
  125. })
  126. // #endif
  127. }
  128. const addMarkers = () => {
  129. const marker = {
  130. id: 1,
  131. iconPath: img.value,
  132. width: 30,
  133. height: 30,
  134. joinCluster: true, // 指定了该参数才会参与聚合
  135. label: {
  136. width: 50,
  137. height: 30,
  138. borderWidth: 1,
  139. borderRadius: 4,
  140. bgColor: '#FFCB3C',
  141. anchorX: -26,
  142. anchorY: -60,
  143. content: ''
  144. }
  145. }
  146. const positions = [
  147. { latitude: 23.584863449735067, longitude: 113.61702297075017},
  148. { latitude: 23.099994, longitude: 113.324520 },
  149. { latitude: 23.099994, longitude: 113.322520 },
  150. { latitude: 23.099994, longitude: 113.326520 },
  151. { latitude: 23.096994, longitude: 113.329520 }
  152. ]
  153. const newMarkers = []
  154. positions.forEach((p, i) => {
  155. const newMarker = {...marker, ...p}
  156. newMarker.id = i + 1
  157. newMarker.label.content = `果园 ${i + 1}`
  158. newMarkers.push(newMarker)
  159. })
  160. // #ifdef MP-WEIXIN
  161. mapCtx.value.addMarkers({
  162. markers: newMarkers,
  163. clear: false,
  164. complete(res) {
  165. console.log('addMarkers', res)
  166. }
  167. })
  168. // #else
  169. // 非微信平台直接设置 markers
  170. markers.value = markers.value.concat(newMarkers)
  171. // 更新包含的点
  172. includePoints.value = markers.value.map(marker => ({
  173. latitude: marker.latitude,
  174. longitude: marker.longitude
  175. }))
  176. // #endif
  177. }
  178. const removeMarkers = () => {
  179. // #ifdef MP-WEIXIN
  180. mapCtx.value.addMarkers({
  181. clear: true,
  182. markers: []
  183. })
  184. // #else
  185. markers.value = []
  186. // #endif
  187. }
  188. const onMarkerTap = (e) => {
  189. console.log('@@ markertap', e)
  190. toPage('gardenMap')
  191. }
  192. const onCalloutTap = (e) => {
  193. console.log('@@ onCalloutTap', e)
  194. }
  195. const onLabelTap = (e) => {
  196. console.log('@@ labletap', e)
  197. }
  198. </script>