mapComponent.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view>
  3. <map
  4. id="homeId"
  5. :latitude="latitude"
  6. :longitude="longitude"
  7. @markertap="onMarkerTap"
  8. @callouttap="onCalloutTap"
  9. @labeltap="onLabelTap"
  10. :markers="markers"
  11. :include-points="includePoints"
  12. show-location
  13. style="width: 100%; height: 280rpx; display: block;"
  14. ></map>
  15. </view>
  16. </template>
  17. <script setup>
  18. import { ref, onMounted } from 'vue'
  19. const latitude = ref(23.099994)
  20. const longitude = ref(113.324520)
  21. const markers = ref([])
  22. const includePoints = ref([])
  23. const mapCtx = ref(null)
  24. const img = ref('../../../static/map/point.png')
  25. onMounted(() => {
  26. // #ifdef MP-WEIXIN
  27. uni.nextTick(() => {
  28. mapCtx.value = uni.createMapContext('homeId', this)
  29. console.log('地图上下文:', mapCtx.value) // 确认是否成功获取
  30. if (mapCtx.value) {
  31. bindEvent() // 确保上下文存在再调用
  32. } else {
  33. console.error('地图上下文获取失败')
  34. }
  35. })
  36. // #endif
  37. })
  38. const bindEvent = () => {
  39. // #ifdef MP-WEIXIN
  40. console.log('bindEvent', mapCtx.value )
  41. mapCtx.value.initMarkerCluster({
  42. enableDefaultStyle: false,
  43. zoomOnClick: true,
  44. gridSize: 60,
  45. complete(res) {
  46. console.log('initMarkerCluster', res)
  47. }
  48. })
  49. addMarkers()
  50. mapCtx.value.on('markerClusterCreate', res => {
  51. console.log('clusterCreate', res)
  52. const clusters = res.clusters
  53. const newMarkers = clusters.map(cluster => {
  54. const { center, clusterId, markerIds } = cluster
  55. return {
  56. ...center,
  57. width: 0,
  58. height: 0,
  59. clusterId, // 必须
  60. label: {
  61. content: markerIds.length + '',
  62. fontSize: 20,
  63. width: 60,
  64. height: 60,
  65. bgColor: '#00ff00',
  66. borderRadius: 30,
  67. textAlign: 'center',
  68. anchorX: 0,
  69. anchorY: -30,
  70. }
  71. }
  72. })
  73. mapCtx.value.addMarkers({
  74. markers: newMarkers,
  75. clear: false,
  76. complete(res) {
  77. console.log('clusterCreate addMarkers', res)
  78. }
  79. })
  80. })
  81. // #endif
  82. }
  83. const addMarkers = () => {
  84. const marker = {
  85. id: 1,
  86. iconPath: img.value,
  87. width: 30,
  88. height: 30,
  89. joinCluster: true, // 指定了该参数才会参与聚合
  90. label: {
  91. width: 50,
  92. height: 30,
  93. borderWidth: 1,
  94. borderRadius: 10,
  95. bgColor: '#ffffff',
  96. content: ''
  97. }
  98. }
  99. const positions = [
  100. { latitude: 23.099994, longitude: 113.324520 },
  101. { latitude: 23.099994, longitude: 113.322520 },
  102. { latitude: 23.099994, longitude: 113.326520 },
  103. { latitude: 23.096994, longitude: 113.329520 }
  104. ]
  105. const newMarkers = []
  106. positions.forEach((p, i) => {
  107. const newMarker = {...marker, ...p}
  108. newMarker.id = i + 1
  109. newMarker.label.content = `label ${i + 1}`
  110. newMarkers.push(newMarker)
  111. })
  112. // #ifdef MP-WEIXIN
  113. mapCtx.value.addMarkers({
  114. markers: newMarkers,
  115. clear: false,
  116. complete(res) {
  117. console.log('addMarkers', res)
  118. }
  119. })
  120. // #else
  121. // 非微信平台直接设置 markers
  122. markers.value = markers.value.concat(newMarkers)
  123. // 更新包含的点
  124. includePoints.value = markers.value.map(marker => ({
  125. latitude: marker.latitude,
  126. longitude: marker.longitude
  127. }))
  128. // #endif
  129. }
  130. const removeMarkers = () => {
  131. // #ifdef MP-WEIXIN
  132. mapCtx.value.addMarkers({
  133. clear: true,
  134. markers: []
  135. })
  136. // #else
  137. markers.value = []
  138. // #endif
  139. }
  140. const onMarkerTap = (e) => {
  141. console.log('@@ markertap', e)
  142. }
  143. const onCalloutTap = (e) => {
  144. console.log('@@ onCalloutTap', e)
  145. }
  146. const onLabelTap = (e) => {
  147. console.log('@@ labletap', e)
  148. }
  149. </script>