appMap.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <view class="realtimeWatch">
  3. <!-- <map
  4. id="map"
  5. :latitude="lat"
  6. :longitude="lng"
  7. >
  8. </map>
  9. <view class="options">
  10. <view class="options-item" @click="showCar">
  11. <image
  12. style="width: 100%; height: 100%"
  13. :src="'https://www.yihaocg.com/mobile/image/location/boxselection.png'"
  14. />
  15. </view>
  16. </view>
  17. <tq-car-user
  18. ref="carChooseRef"
  19. @choosed="getChoosed"
  20. >
  21. </tq-car-user> -->
  22. </view>
  23. </template>
  24. <script>
  25. const img = 'http://tq.5000v.com:8035/img/upload/2023/11/27/1_1_20231127115853A031.png';
  26. export default {
  27. components: {
  28. },
  29. data() {
  30. return {
  31. _mapContext: null,
  32. lat: 23.099994,
  33. lng: 113.324520,
  34. markers: [], // 标注点
  35. }
  36. },
  37. onReady () {
  38. // this.queryLocation()
  39. // 创建map对象
  40. this._mapContext = uni.createMapContext("map", this);
  41. this.cluster();
  42. },
  43. methods: {
  44. // 查询自身位置
  45. queryLocation() {
  46. uni.getLocation({
  47. type: "gcj02",
  48. success: (res) => {
  49. const {latitude,longitude} = res
  50. this.lat = latitude;
  51. this.lng = longitude;
  52. },
  53. fail: (res) => {
  54. console.log('定位失败',JSON.stringify(res));
  55. }
  56. });
  57. },
  58. //
  59. showCar() {
  60. this.$refs.carChooseRef.showDialog()
  61. },
  62. //选中数据
  63. getChoosed(data) {
  64. console.log(data,'data')
  65. },
  66. // 点聚合
  67. cluster() {
  68. // 仅调用初始化,才会触发 on.("markerClusterCreate", (e) => {})
  69. this._mapContext.initMarkerCluster({
  70. enableDefaultStyle: false, // 是否使用默认样式
  71. zoomOnClick: true, // 点击聚合的点,是否改变地图的缩放级别
  72. gridSize: 60, // 聚合计算时网格的像素大小,默认60
  73. complete(res) {
  74. console.log('initMarkerCluster', res)
  75. }
  76. });
  77. this._mapContext.on("markerClusterCreate", (res) => {
  78. console.log("markerClusterCreate", res);
  79. const clusters = res.clusters
  80. const markers = clusters.map(cluster => {
  81. const {
  82. center,
  83. clusterId,
  84. markerIds
  85. } = cluster
  86. return {
  87. ...center,
  88. width: 0,
  89. height: 0,
  90. clusterId, // 必须
  91. label: {
  92. content: markerIds.length + '',
  93. fontSize: 16,
  94. width: 50,
  95. height: 50,
  96. bgColor: '#00A3FA',
  97. borderRadius: 25,
  98. textAlign: 'center',
  99. anchorX: 0,
  100. anchorY: -20,
  101. }
  102. }
  103. })
  104. this._mapContext.addMarkers({
  105. markers,
  106. clear: false,
  107. complete(res) {
  108. console.log('clusterCreate addMarkers', res)
  109. }
  110. })
  111. });
  112. this._mapContext.on('markerClusterClick', (res) => {
  113. console.log('markerClusterClick', res)
  114. })
  115. this.addMarkers();
  116. },
  117. // 添加标记点
  118. addMarkers() {
  119. const positions = [
  120. {
  121. latitude: 23.099994,
  122. longitude: 113.324520,
  123. }, {
  124. latitude: 23.099994,
  125. longitude: 113.322520,
  126. }, {
  127. latitude: 23.099994,
  128. longitude: 113.326520,
  129. }, {
  130. latitude: 23.096994,
  131. longitude: 113.329520,
  132. }
  133. ]
  134. const markers = []
  135. positions.forEach((p, i) => {
  136. markers.push(
  137. Object.assign({}, {
  138. id: i + 1,
  139. iconPath: img,
  140. width: 28,
  141. height: 29,
  142. joinCluster: true, // 指定了该参数才会参与聚合
  143. callout:{
  144. bgColor: "#5AC2EB",
  145. color: "#fff",
  146. content: "客户名称",
  147. display: "ALWAYS",
  148. fontSize: "14",
  149. fontWeight: "bold",
  150. padding: 8,
  151. textAlign: "center"
  152. }
  153. }, p)
  154. )
  155. })
  156. console.log('markers', markers)
  157. this._mapContext.addMarkers({
  158. markers,
  159. clear: false,
  160. complete(res) {
  161. console.log('addMarkers', res)
  162. }
  163. })
  164. },
  165. // 点击图标
  166. handleClickMarker(markerId) {
  167. this.detailsId = this.vehMarkerId[markerId.detail.markerId].id;
  168. this.vehicleDetails = this.vehMarkerId[markerId.detail.markerId].value;
  169. this.details = true;
  170. },
  171. }
  172. }
  173. </script>
  174. <style lang="scss" scoped>
  175. .realtimeWatch {
  176. width: 100%;
  177. height: 100vh;
  178. position: relative;
  179. #map {
  180. width: 750rpx;
  181. height: 100%;
  182. }
  183. .options {
  184. position: absolute;
  185. width: 72rpx;
  186. right: 30rpx;
  187. bottom: 156rpx;
  188. box-shadow: 0px 3px 30px 0px rgba(0, 0, 0, 0.15);
  189. z-index: 9999;
  190. .options-item {
  191. height: 72rpx;
  192. }
  193. }
  194. }
  195. </style>