allGardenMap.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. :enable-satellite="true"
  14. theme="satellite"
  15. show-location
  16. style="width: 100%; height: 100vh;"
  17. ></map>
  18. </view>
  19. </template>
  20. <script setup>
  21. import { ref, onMounted } from 'vue'
  22. const latitude = ref(26.870355)
  23. const longitude = ref(100.239704)
  24. // const latitude = ref(23.584863449735067)
  25. // const longitude = ref(113.61702297075017)
  26. const markers = ref([])
  27. const includePoints = ref([])
  28. const mapCtx = ref(null)
  29. const img = ref('../../../../static/map/point.png')
  30. const layers = [
  31. {
  32. // 添加腾讯地图默认底图(可选)
  33. type: 'vector',
  34. subdomains: ['0', '1', '2'],
  35. url: 'https://{s}.map.qq.com/tile/{z}/{x}/{y}',
  36. zIndex: 0,
  37. },
  38. {
  39. // 叠加天地图影像(WMTS)
  40. type: 'wmts',
  41. layer: 'img',
  42. 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',
  43. // url: 'https://t{s}.tianditu.gov.cn/img_c/wmts?tk=e95115c454a663cd052d96019fd83840',
  44. tileMatrixSet: 'c',
  45. subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
  46. zIndex: 1,
  47. }
  48. ]
  49. onMounted(() => {
  50. // #ifdef MP-WEIXIN
  51. mapCtx.value = uni.createMapContext('mapId')
  52. mapCtx.value.on('markerClusterClick', res => {
  53. console.log('markerClusterClick', res)
  54. })
  55. bindEvent()
  56. // #endif
  57. })
  58. const bindEvent = () => {
  59. // #ifdef MP-WEIXIN
  60. mapCtx.value.initMarkerCluster({
  61. enableDefaultStyle: false,
  62. zoomOnClick: true,
  63. gridSize: 60,
  64. complete(res) {
  65. console.log('initMarkerCluster', res)
  66. }
  67. })
  68. addMarkers()
  69. mapCtx.value.on('markerClusterCreate', res => {
  70. console.log('clusterCreate', res)
  71. const clusters = res.clusters
  72. const newMarkers = clusters.map(cluster => {
  73. const { center, clusterId, markerIds } = cluster
  74. return {
  75. ...center,
  76. width: 0,
  77. height: 0,
  78. clusterId, // 必须
  79. label: {
  80. content: markerIds.length + '',
  81. fontSize: 20,
  82. width: 30,
  83. height: 30,
  84. bgColor: '#FFCB3C',
  85. borderRadius: 30,
  86. textAlign: 'center',
  87. anchorX: 0,
  88. anchorY: -30,
  89. }
  90. }
  91. })
  92. mapCtx.value.addMarkers({
  93. markers: newMarkers,
  94. clear: false,
  95. complete(res) {
  96. console.log('clusterCreate addMarkers', res)
  97. }
  98. })
  99. })
  100. // let xyz = "https://birdseye-img.sysuimars.com/map/lby/{z}/{x}/{y}.png";
  101. // mapCtx.value.addCustomLayer({
  102. // id: 'custom-tile-layer',
  103. // type: 'raster',
  104. // source: {
  105. // type: 'raster',
  106. // tiles: [xyz], // 替换为你的瓦片 URL
  107. // tileSize: 256,
  108. // },
  109. // paint: {},
  110. // zIndex: 99
  111. // });
  112. // const mapCtx2 = uni.createMapContext('mapId', this);
  113. const xyz = "https://birdseye-img.sysuimars.com/map/lby/z/x/y.png";
  114. const demoLayer = 'https://3gimg.qq.com/visual/lbs_gl_demo/image_tiles_layers/z/x_y.png'
  115. // mapCtx.value.addCustomLayer({
  116. // layerId: 'cunstom',
  117. // id: 'custom-tile-layer',
  118. // type: 'raster',
  119. // source: {
  120. // type: 'raster',
  121. // tiles: [demoLayer],
  122. // tileSize: 256,
  123. // },
  124. // paint: {},
  125. // success: (res) => console.log("图层添加成功", res),
  126. // fail: (err) => console.error("图层添加失败", err)
  127. // });
  128. mapCtx.value.addGroundOverlay({
  129. id: 'gaode-tiles',
  130. type: 'raster',
  131. source: {
  132. type: 'raster',
  133. tiles: [
  134. 'https://webst01.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}'
  135. ],
  136. tileSize: 256,
  137. },
  138. paint: {},
  139. success: (res) => {
  140. console.log("高德瓦片加载成功");
  141. },
  142. fail: (err) => {
  143. console.error("加载失败", err);
  144. }
  145. });
  146. // #endif
  147. }
  148. const addMarkers = () => {
  149. const marker = {
  150. id: 1,
  151. iconPath: img.value,
  152. width: 30,
  153. height: 30,
  154. joinCluster: true, // 指定了该参数才会参与聚合
  155. label: {
  156. width: 50,
  157. height: 30,
  158. borderWidth: 1,
  159. borderRadius: 4,
  160. bgColor: '#FFCB3C88',
  161. anchorX: -26,
  162. anchorY: -60,
  163. content: ''
  164. }
  165. }
  166. const positions = [
  167. { latitude: 23.584863449735067, longitude: 113.61702297075017},
  168. { latitude: 23.099994, longitude: 113.324520 },
  169. { latitude: 23.099994, longitude: 113.322520 },
  170. { latitude: 23.099994, longitude: 113.326520 },
  171. { latitude: 23.096994, longitude: 113.329520 }
  172. ]
  173. const newMarkers = []
  174. positions.forEach((p, i) => {
  175. const newMarker = {...marker, ...p}
  176. newMarker.id = i + 1
  177. newMarker.label.content = `果园 ${i + 1}`
  178. newMarkers.push(newMarker)
  179. })
  180. // #ifdef MP-WEIXIN
  181. mapCtx.value.addMarkers({
  182. markers: newMarkers,
  183. clear: false,
  184. complete(res) {
  185. console.log('addMarkers', res)
  186. }
  187. })
  188. // #else
  189. // 非微信平台直接设置 markers
  190. markers.value = markers.value.concat(newMarkers)
  191. // 更新包含的点
  192. includePoints.value = markers.value.map(marker => ({
  193. latitude: marker.latitude,
  194. longitude: marker.longitude
  195. }))
  196. // #endif
  197. }
  198. const removeMarkers = () => {
  199. // #ifdef MP-WEIXIN
  200. mapCtx.value.addMarkers({
  201. clear: true,
  202. markers: []
  203. })
  204. // #else
  205. markers.value = []
  206. // #endif
  207. }
  208. const onMarkerTap = (e) => {
  209. console.log('@@ markertap', e)
  210. }
  211. const onCalloutTap = (e) => {
  212. console.log('@@ onCalloutTap', e)
  213. }
  214. const onLabelTap = (e) => {
  215. console.log('@@ labletap', e)
  216. }
  217. </script>