gardenMap.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <view class="garden-page">
  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. show-location
  15. style="width: 100%; height: 100vh;"
  16. ></map>
  17. <view class="top-mask"></view>
  18. <view class="garden-info">
  19. <view class="panel-title">
  20. <view class="title-l">
  21. <image class="garden-img" :src="`${config.BASIC_IMG}home/garden.png`" mode=""></image>
  22. <view class="title-info">
  23. <view class="title-garden">
  24. 从化荔枝博览园
  25. </view>
  26. <view class="btn-second">
  27. 有味指数
  28. <text>4.5分</text>
  29. </view>
  30. </view>
  31. </view>
  32. <view class="title-r">
  33. <text class="add-text">进店</text>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script setup>
  40. import { ref, onMounted } from 'vue'
  41. import config from "@/api/config.js"
  42. const latitude = ref(23.584863449735067)
  43. const longitude = ref(113.61702297075017)
  44. const markers = ref([])
  45. const includePoints = ref([])
  46. const mapCtx = ref(null)
  47. const img = ref('../../../../static/map/tree.png')
  48. // const layers = [
  49. // {
  50. // // 添加腾讯地图默认底图(可选)
  51. // type: 'vector',
  52. // subdomains: ['0', '1', '2'],
  53. // url: 'https://{s}.map.qq.com/tile/{z}/{x}/{y}',
  54. // zIndex: 0,
  55. // },
  56. // {
  57. // // 叠加天地图影像(WMTS)
  58. // type: 'wmts',
  59. // layer: 'img',
  60. // 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',
  61. // // url: 'https://t{s}.tianditu.gov.cn/img_c/wmts?tk=e95115c454a663cd052d96019fd83840',
  62. // tileMatrixSet: 'c',
  63. // subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
  64. // zIndex: 1,
  65. // }
  66. // ]
  67. const tiandituKey = 'e95115c454a663cd052d96019fd83840'
  68. const layers = ref([
  69. {
  70. // 腾讯地图矢量底图
  71. type: 'vector',
  72. subdomains: ['0', '1', '2'],
  73. url: 'https://{s}.map.qq.com/tile/{z}/{x}/{y}',
  74. zIndex: 0
  75. },
  76. {
  77. // 天地图影像底图
  78. type: 'raster',
  79. tileSize: 256,
  80. tiles: [
  81. `https://t0.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=${tiandituKey}`,
  82. `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=${tiandituKey}`,
  83. `https://t2.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=${tiandituKey}`,
  84. `https://t3.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=${tiandituKey}`,
  85. `https://t4.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=${tiandituKey}`,
  86. `https://t5.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=${tiandituKey}`,
  87. `https://t6.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=${tiandituKey}`,
  88. `https://t7.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=${tiandituKey}`
  89. ],
  90. zIndex: 1
  91. }
  92. ])
  93. onMounted(() => {
  94. // #ifdef MP-WEIXIN
  95. mapCtx.value = uni.createMapContext('mapId')
  96. mapCtx.value.on('markerClusterClick', res => {
  97. console.log('markerClusterClick', res)
  98. })
  99. setTimeout(() => {
  100. mapCtx.value.getMapProperties({
  101. success: (res) => {
  102. console.log('地图属性:', res)
  103. }
  104. })
  105. }, 1000)
  106. bindEvent()
  107. // #endif
  108. })
  109. const bindEvent = () => {
  110. // #ifdef MP-WEIXIN
  111. mapCtx.value.initMarkerCluster({
  112. enableDefaultStyle: false,
  113. zoomOnClick: true,
  114. gridSize: 60,
  115. complete(res) {
  116. console.log('initMarkerCluster', res)
  117. }
  118. })
  119. addMarkers()
  120. mapCtx.value.on('markerClusterCreate', res => {
  121. console.log('clusterCreate', res)
  122. const clusters = res.clusters
  123. const newMarkers = clusters.map(cluster => {
  124. const { center, clusterId, markerIds } = cluster
  125. return {
  126. ...center,
  127. width: 0,
  128. height: 0,
  129. clusterId, // 必须
  130. label: {
  131. content: markerIds.length + '',
  132. fontSize: 20,
  133. // width: 30,
  134. height: 30,
  135. color: "#fff",
  136. bgColor: '#FFCB3C',
  137. borderRadius: 30,
  138. textAlign: 'center',
  139. anchorX: 0,
  140. anchorY: -30,
  141. }
  142. }
  143. })
  144. mapCtx.value.addMarkers({
  145. markers: newMarkers,
  146. clear: false,
  147. complete(res) {
  148. console.log('clusterCreate addMarkers', res)
  149. }
  150. })
  151. })
  152. // #endif
  153. }
  154. const addMarkers = () => {
  155. const marker = {
  156. id: 1,
  157. iconPath: img.value,
  158. width: 40,
  159. height: 40,
  160. joinCluster: true, // 指定了该参数才会参与聚合
  161. label: {
  162. // width: 50,
  163. height: 30,
  164. color: "#fff",
  165. // borderWidth: 1,
  166. // borderRadius: 4,
  167. // bgColor: '#FFCB3C',
  168. anchorX: -26,
  169. anchorY: -60,
  170. content: ''
  171. }
  172. }
  173. const positions = [
  174. { latitude: 23.584863449735067, longitude: 113.61702297075017},
  175. { latitude: 23.099994, longitude: 113.324520 },
  176. { latitude: 23.099994, longitude: 113.322520 },
  177. { latitude: 23.099994, longitude: 113.326520 },
  178. { latitude: 23.096994, longitude: 113.329520 }
  179. ]
  180. const newMarkers = []
  181. positions.forEach((p, i) => {
  182. const newMarker = {...marker, ...p}
  183. newMarker.id = i + 1
  184. newMarker.label.content = `果树名字 ${i + 1}`
  185. newMarkers.push(newMarker)
  186. })
  187. // #ifdef MP-WEIXIN
  188. mapCtx.value.addMarkers({
  189. markers: newMarkers,
  190. clear: false,
  191. complete(res) {
  192. console.log('addMarkers', res)
  193. }
  194. })
  195. // #else
  196. // 非微信平台直接设置 markers
  197. markers.value = markers.value.concat(newMarkers)
  198. // 更新包含的点
  199. includePoints.value = markers.value.map(marker => ({
  200. latitude: marker.latitude,
  201. longitude: marker.longitude
  202. }))
  203. // #endif
  204. }
  205. const removeMarkers = () => {
  206. // #ifdef MP-WEIXIN
  207. mapCtx.value.addMarkers({
  208. clear: true,
  209. markers: []
  210. })
  211. // #else
  212. markers.value = []
  213. // #endif
  214. }
  215. const onMarkerTap = (e) => {
  216. console.log('@@ markertap', e)
  217. }
  218. const onCalloutTap = (e) => {
  219. console.log('@@ onCalloutTap', e)
  220. }
  221. const onLabelTap = (e) => {
  222. console.log('@@ labletap', e)
  223. }
  224. </script>
  225. <style lang="scss" scoped>
  226. .garden-page {
  227. height: 100vh;
  228. width: 100%;
  229. position: relative;
  230. .top-mask {
  231. position: absolute;
  232. z-index: 1;
  233. top: 0%;
  234. left: 0%;
  235. width: 100%;
  236. height: 464rpx;
  237. pointer-events: none;
  238. background: linear-gradient(0deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.5));
  239. }
  240. .garden-info {
  241. position: absolute;
  242. z-index: 2;
  243. top: 0%;
  244. left: 0%;
  245. width: 100%;
  246. .panel-title {
  247. display: flex;
  248. align-items: center;
  249. justify-content: space-between;
  250. padding: 24rpx;
  251. .title-l {
  252. flex: 1;
  253. display: flex;
  254. align-items: center;
  255. .garden-img {
  256. width: 100rpx;
  257. height: 100rpx;
  258. border-radius: 10rpx;
  259. }
  260. .title-info {
  261. padding-left: 20rpx;
  262. .title-garden {
  263. color: #fff;
  264. font-size: 28rpx;
  265. font-weight: 600;
  266. padding-bottom: 10rpx;
  267. }
  268. .btn-second {
  269. padding: 0 30rpx;
  270. border-radius: 40rpx;
  271. font-size: 24rpx;
  272. color: #F3C11D;
  273. border: 2rpx solid #F3C11D;
  274. background: rgba(255, 217, 94, 0.28);
  275. height: 42rpx;
  276. line-height: 40rpx;
  277. }
  278. }
  279. }
  280. .title-r {
  281. display: flex;
  282. font-size: 24rpx;
  283. padding: 8rpx 22rpx;
  284. background: #FFFFFF;
  285. border-radius: 30rpx;
  286. .add-text {
  287. padding-left: 6rpx;
  288. font-weight: 600;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. </style>