mapcheck.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <template>
  2. <view class="container">
  3. <map id="map" :longitude="longitude" :latitude="latitude" :scale="scale" :markers="markers" :circles="circles">
  4. <!-- 地图高度设置为80vh -->
  5. <view class="info-container"> <!-- 位置信息和按钮的容器 -->
  6. <view class="location-info">
  7. 当前位置: {{ address }}
  8. </view>
  9. <button @click="checkIn" class="sign-button">实时签到</button>
  10. </view>
  11. </map>
  12. </view>
  13. </template>
  14. <script>
  15. // 引入高德地图SDK
  16. const amapFile = require('../../util/amap-wx.js');
  17. import {
  18. savePosition
  19. } from '../../../api/driver'
  20. import {
  21. getTenantCacheList
  22. } from '../../../utils/util.js'
  23. export default {
  24. data() {
  25. return {
  26. longitude: 116.397128, // 默认经度
  27. latitude: 39.916527, // 默认纬度
  28. datalongitude:null,
  29. datalatitude:null,
  30. markers: [],
  31. amapPlugin: null,
  32. address: "",
  33. secondLocation: {
  34. longitude: 120.05860616181181,
  35. latitude: 30.363726744840633
  36. }, // 第二个定位点
  37. circles: [],
  38. scale: null
  39. };
  40. },
  41. onLoad() {
  42. this.initMap();
  43. },
  44. methods: {
  45. initMap() {
  46. // 使用API Key初始化高德地图
  47. this.amapPlugin = new amapFile.AMapWX({
  48. key: 'a641d2c10959a1a1832cc46462c4858c'
  49. });
  50. // 获取当前位置信息
  51. this.amapPlugin.getRegeo({
  52. success: (data) => {
  53. console.log(data[0]);
  54. this.datalongitude = data[0].longitude;
  55. this.datalatitude = data[0].latitude;
  56. this.address = data[0].name
  57. this.markers = [{
  58. id: 1,
  59. longitude: this.secondLocation.longitude,
  60. latitude: this.secondLocation.latitude,
  61. width: 20,
  62. height: 39,
  63. iconPath: '/substaticpages/static/czzn-img/map-marker.png', // 第一个标记的图标
  64. },
  65. {
  66. id: 2,
  67. longitude: data[0].longitude,
  68. latitude: data[0].latitude,
  69. width: 30,
  70. height: 30,
  71. iconPath: '/substaticpages/static/czzn-img/map-car.png', // 第一个标记的图标
  72. }
  73. ];
  74. this.circles = [{
  75. latitude: this.secondLocation.latitude, // 圆心纬度
  76. longitude: this.secondLocation.longitude, // 圆心经度
  77. color:"#497ef93a",
  78. fillColor: '#497ef93a', // 圆形填充颜色
  79. radius: 1000, // 半径为1000米
  80. }]
  81. this.fitMapToMarkers()
  82. },
  83. fail: (err) => {
  84. console.error(err);
  85. }
  86. });
  87. },
  88. fitMapToMarkers() {
  89. // 一个简单的方法来估算合适的缩放级别,这取决于两个位置的距离
  90. let distance = this.getDistance(this.datalatitude, this.datalongitude, this.secondLocation.latitude, this
  91. .secondLocation.longitude);
  92. // 计算两个位置的中心点
  93. let minLat = Math.min(this.datalatitude, this.secondLocation.latitude);
  94. let maxLat = Math.max(this.datalatitude, this.secondLocation.latitude);
  95. let minLng = Math.min(this.datalongitude, this.secondLocation.longitude);
  96. let maxLng = Math.max(this.datalongitude, this.secondLocation.longitude);
  97. // 调整地图中心点
  98. this.latitude = (minLat + maxLat) / 2;
  99. this.longitude = (minLng + maxLng) / 2;
  100. // 基于距离计算缩放级别,这里的逻辑可能需要根据你的具体需求调整
  101. },
  102. checkIn() {
  103. this.calculateDistance(this.latitude, this.longitude, this.secondLocation.latitude, this
  104. .secondLocation.longitude);
  105. },
  106. calculateDistance(lat1, lng1, lat2, lng2) {
  107. this.amapPlugin.getWalkingRoute({
  108. origin: `${lng1},${lat1}`,
  109. destination: `${lng2},${lat2}`,
  110. success: (res) => {
  111. if (res.paths && res.paths[0] && res.paths[0].distance) {
  112. // 获取距离
  113. const distance = res.paths[0].distance;
  114. // 检查距离是否超过1000米
  115. if (distance > 1000) {
  116. uni.showModal({
  117. // title: '提示',
  118. content: "签到失败:您距离目标位置超过一千米。",
  119. showCancel: false,
  120. confirmText: this.$t('settings.confirmText'),
  121. success: function(r) {
  122. if (r.confirm) {
  123. }
  124. }
  125. })
  126. } else {
  127. let vehicledata = uni.getStorageSync("currentVehicleInfo")
  128. let tenantOption = getTenantCacheList();
  129. console.log(tenantOption);
  130. let data = {
  131. longitude: this.datalongitude,
  132. latitude: this.datalatitude,
  133. positionName: this.address,
  134. vehicle:vehicledata.vehicle,
  135. driver:vehicledata.driver,
  136. tenantId:tenantOption[0].value
  137. }
  138. savePosition(data).then(res => {
  139. console.log(res);
  140. if (res.statusCode == 200) {
  141. if (res.data.code == "0") {
  142. uni.showModal({
  143. // title: '提示',
  144. content: "签到成功",
  145. showCancel: false,
  146. confirmText: this.$t('settings.confirmText'),
  147. success: function(r) {
  148. }
  149. })
  150. }
  151. }
  152. })
  153. }
  154. }
  155. },
  156. fail: (error) => {
  157. console.error("计算距离失败", error);
  158. }
  159. });
  160. },
  161. getDistance(lat1, lng1, lat2, lng2) {
  162. this.amapPlugin.getWalkingRoute({
  163. origin: `${lng1},${lat1}`,
  164. destination: `${lng2},${lat2}`,
  165. success: (res) => {
  166. if (res.paths && res.paths[0] && res.paths[0].distance) {
  167. // 获取距离
  168. let distance = res.paths[0].distance;
  169. console.log(distance);
  170. if (distance > 10000) {
  171. this.scale = 11
  172. } else if (distance > 5000) {
  173. this.scale = 12
  174. } else if (distance > 1000) {
  175. this.scale = 13
  176. } else {
  177. this.scale = 14
  178. }
  179. // 调整缩放级别,这里的缩放级别是示例,可能需要根据实际情况调整
  180. }
  181. },
  182. fail: (error) => {
  183. console.error("计算距离失败", error);
  184. }
  185. });
  186. }
  187. },
  188. };
  189. </script>
  190. <style>
  191. .container {
  192. height: 100%;
  193. }
  194. #map {
  195. width: 100%;
  196. height: 100vh;
  197. }
  198. .info-container {
  199. box-sizing: border-box;
  200. width: calc(100% - 136rpx);
  201. background-color: #fff;
  202. border-radius: 24rpx ;
  203. /* 占据剩余的20%的高度 */
  204. display: flex;
  205. flex-direction: column;
  206. justify-content: center;
  207. align-items: center;
  208. position: fixed;
  209. bottom: 74rpx;
  210. left: 68rpx;
  211. padding: 30rpx;
  212. box-shadow: 0px 0px 29px 3px rgba(37,58,104,0.24);
  213. }
  214. .location-info {
  215. margin-bottom: 10px;
  216. /* 位置信息和按钮之间的间距 */
  217. }
  218. .sign-button{
  219. width: 100%;
  220. border-radius: 10rpx;
  221. line-height: 68rpx;
  222. background-color: #497FF9;
  223. }
  224. .car-number{
  225. background-color: #fff;
  226. }
  227. </style>