合伙人运营小程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. Page({
  2. data: {
  3. // 地图初始中心坐标(北京市中心)
  4. longitude: 116.404,
  5. latitude: 39.915,
  6. scale: 14,
  7. markers: [],
  8. polyline: [],
  9. startLocation: {},
  10. endLocation: {},
  11. showTripInfo: true,
  12. distance: 0,
  13. estimatedPrice: 0,
  14. showCallButton: true,
  15. showDriverInfo: false,
  16. carsNearby: 0,
  17. // 司机信息
  18. driverAvatar: 'https://esos-iot.bjdexn.cn/myminio/project/a0d245bf00c6493d949c3be82a6d2b98.png',
  19. driverName: '王师傅',
  20. carNumber: '京A88888',
  21. estimatedArrival: 5
  22. },
  23. onLoad() {
  24. // 初始化地图
  25. this.initMap();
  26. // 加载附近车辆数据
  27. this.loadNearbyCars();
  28. },
  29. // 初始化地图,获取用户位置
  30. initMap() {
  31. const that = this;
  32. // 获取用户当前位置
  33. wx.getLocation({
  34. type: 'gcj02', // 国测局坐标系,微信地图使用该坐标系
  35. success(res) {
  36. const longitude = res.longitude;
  37. const latitude = res.latitude;
  38. // 更新地图中心位置
  39. that.setData({
  40. longitude,
  41. latitude,
  42. startLocation: {
  43. longitude,
  44. latitude,
  45. name: '我的位置'
  46. },
  47. // 添加起点标记
  48. markers: [{
  49. id: 0,
  50. longitude,
  51. latitude,
  52. iconPath: 'https://esos-iot.bjdexn.cn/myminio/project/a0d245bf00c6493d949c3be82a6d2b98.png',
  53. width: 40,
  54. height: 40,
  55. anchor: {x: 0.5, y: 1}
  56. }]
  57. });
  58. // 逆地址解析,获取位置名称
  59. that.reverseGeocoder(longitude, latitude, 'start');
  60. },
  61. fail() {
  62. wx.showToast({
  63. title: '无法获取位置,请检查权限',
  64. icon: 'none'
  65. });
  66. }
  67. });
  68. },
  69. // 逆地址解析,将经纬度转换为地址名称
  70. reverseGeocoder(longitude, latitude, type) {
  71. wx.request({
  72. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=VRGBZ-ZFRHB-SKIUP-NHHJ3-TXGJT-ZIFG3`,
  73. success(res) {
  74. if (res.data.status === 0) {
  75. const address = res.data.result.address;
  76. if (type === 'start') {
  77. this.setData({
  78. 'startLocation.name': address
  79. });
  80. } else if (type === 'end') {
  81. this.setData({
  82. 'endLocation.name': address
  83. });
  84. }
  85. }
  86. },
  87. fail() {
  88. wx.showToast({
  89. title: '地址解析失败',
  90. icon: 'none'
  91. });
  92. }
  93. });
  94. },
  95. // 选择起点位置
  96. chooseStartLocation() {
  97. this.chooseLocation('start');
  98. },
  99. // 选择终点位置
  100. chooseEndLocation() {
  101. this.chooseLocation('end');
  102. },
  103. // 选择位置通用方法
  104. chooseLocation(type) {
  105. console.log(type);
  106. const that = this;
  107. wx.chooseLocation({
  108. success(res) {
  109. console.log(res);
  110. const locationData = {
  111. longitude: res.longitude,
  112. latitude: res.latitude,
  113. name: res.name
  114. };
  115. // 更新对应位置信息
  116. if (type === 'start') {
  117. that.setData({
  118. 'startLocation': locationData
  119. });
  120. } else if (type === 'end') {
  121. that.setData({
  122. 'endLocation': locationData
  123. });
  124. }
  125. // 更新标记和路线
  126. that.updateMarkersAndRoute();
  127. },
  128. fail() {
  129. wx.showToast({
  130. title: '选择位置失败',
  131. icon: 'none'
  132. });
  133. }
  134. });
  135. },
  136. // 更新地图标记和路线
  137. updateMarkersAndRoute() {
  138. const { startLocation, endLocation } = this.data;
  139. // 如果起点和终点都存在
  140. if (startLocation.longitude && endLocation.longitude) {
  141. // 更新标记
  142. const markers = [
  143. {
  144. id: 0,
  145. longitude: startLocation.longitude,
  146. latitude: startLocation.latitude,
  147. iconPath: 'https://esos-iot.bjdexn.cn/myminio/project/a0d245bf00c6493d949c3be82a6d2b98.png',
  148. width: 40,
  149. height: 40,
  150. anchor: {x: 0.5, y: 1}
  151. },
  152. {
  153. id: 1,
  154. longitude: endLocation.longitude,
  155. latitude: endLocation.latitude,
  156. iconPath: 'https://esos-iot.bjdexn.cn/myminio/project/9efa1691f71a48b6ae20648c0a2dae56.png',
  157. width: 40,
  158. height: 40,
  159. anchor: {x: 0.5, y: 1}
  160. }
  161. ];
  162. // 绘制路线
  163. const polyline = [{
  164. points: [
  165. {
  166. longitude: startLocation.longitude,
  167. latitude: startLocation.latitude
  168. },
  169. {
  170. longitude: endLocation.longitude,
  171. latitude: endLocation.latitude
  172. }
  173. ],
  174. color: "#2C85FF",
  175. width: 6,
  176. dottedLine: false
  177. }];
  178. // 计算距离和费用
  179. const distance = this.calculateDistance(
  180. startLocation.latitude, startLocation.longitude,
  181. endLocation.latitude, endLocation.longitude
  182. );
  183. // 简单的费用计算模型:起步价13元,超过3公里后每公里2.3元
  184. let price = 13;
  185. if (distance > 3) {
  186. price += (distance - 3) * 2.3;
  187. }
  188. this.setData({
  189. markers,
  190. polyline,
  191. distance: distance.toFixed(1),
  192. estimatedPrice: Math.round(price),
  193. showTripInfo: true,
  194. showCallButton: true
  195. });
  196. } else if (startLocation.longitude) {
  197. // 只有起点
  198. this.setData({
  199. markers: [{
  200. id: 0,
  201. longitude: startLocation.longitude,
  202. latitude: startLocation.latitude,
  203. iconPath: 'https://esos-iot.bjdexn.cn/myminio/project/a0d245bf00c6493d949c3be82a6d2b98.png',
  204. width: 40,
  205. height: 40,
  206. anchor: {x: 0.5, y: 1}
  207. }],
  208. polyline: [],
  209. showTripInfo: false,
  210. showCallButton: false
  211. });
  212. }
  213. },
  214. // 计算两点之间的距离(公里)
  215. calculateDistance(lat1, lon1, lat2, lon2) {
  216. const R = 6371; // 地球半径(公里)
  217. const dLat = this.deg2rad(lat2 - lat1);
  218. const dLon = this.deg2rad(lon2 - lon1);
  219. const a =
  220. Math.sin(dLat/2) * Math.sin(dLat/2) +
  221. Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) *
  222. Math.sin(dLon/2) * Math.sin(dLon/2);
  223. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  224. const distance = R * c;
  225. return distance;
  226. },
  227. // 角度转弧度
  228. deg2rad(deg) {
  229. return deg * (Math.PI/180);
  230. },
  231. // 地图区域变化时触发
  232. onRegionChange(e) {
  233. // 可以在这里处理地图移动后的逻辑,如重新加载附近车辆
  234. if (e.type === 'end' && e.causedBy === 'drag') {
  235. this.loadNearbyCars();
  236. }
  237. },
  238. // 加载附近车辆
  239. loadNearbyCars() {
  240. const { longitude, latitude } = this.data;
  241. // 模拟获取附近车辆数据
  242. // 实际应用中应该调用后端API获取真实数据
  243. const cars = [];
  244. const count = Math.floor(Math.random() * 10) + 1; // 1-10辆随机
  245. for (let i = 0; i < count; i++) {
  246. // 在当前位置附近随机生成车辆位置
  247. const offsetLon = (Math.random() - 0.5) * 0.02;
  248. const offsetLat = (Math.random() - 0.5) * 0.02;
  249. cars.push({
  250. id: 100 + i,
  251. longitude: longitude + offsetLon,
  252. latitude: latitude + offsetLat,
  253. iconPath: 'https://esos-iot.bjdexn.cn/myminio/project/a0d245bf00c6493d949c3be82a6d2b98.png',
  254. width: 30,
  255. height: 30,
  256. anchor: {x: 0.5, y: 0.5}
  257. });
  258. }
  259. // 将车辆标记添加到现有标记中
  260. const updatedMarkers = [...this.data.markers];
  261. // 先移除之前的车辆标记
  262. const filteredMarkers = updatedMarkers.filter(marker => marker.id < 100);
  263. // 添加新的车辆标记
  264. filteredMarkers.push(...cars);
  265. this.setData({
  266. markers: filteredMarkers,
  267. carsNearby: count
  268. });
  269. },
  270. // 呼叫车辆
  271. callCar() {
  272. wx.showLoading({
  273. title: '正在呼叫车辆...',
  274. mask: true
  275. });
  276. // 模拟呼叫过程
  277. setTimeout(() => {
  278. wx.hideLoading();
  279. // 显示司机信息
  280. this.setData({
  281. showDriverInfo: true,
  282. showCallButton: false
  283. });
  284. // 模拟司机接近动画
  285. this.simulateDriverApproach();
  286. }, 2000);
  287. },
  288. // 模拟司机接近
  289. simulateDriverApproach() {
  290. const { startLocation, endLocation, markers } = this.data;
  291. let driverMarker = markers.find(marker => marker.id === 999);
  292. // 如果司机标记不存在,则创建一个
  293. if (!driverMarker) {
  294. // 从终点附近开始
  295. driverMarker = {
  296. id: 999,
  297. longitude: endLocation.longitude + (Math.random() - 0.5) * 0.01,
  298. latitude: endLocation.latitude + (Math.random() - 0.5) * 0.01,
  299. iconPath: 'https://esos-iot.bjdexn.cn/myminio/project/a0d245bf00c6493d949c3be82a6d2b98.png',
  300. width: 40,
  301. height: 40,
  302. anchor: {x: 0.5, y: 0.5}
  303. };
  304. this.setData({
  305. markers: [...markers, driverMarker]
  306. });
  307. }
  308. // 计算司机到起点的每一步
  309. const steps = 20; // 分20步接近
  310. const lonStep = (startLocation.longitude - driverMarker.longitude) / steps;
  311. const latStep = (startLocation.latitude - driverMarker.latitude) / steps;
  312. let currentStep = 0;
  313. // 更新司机位置的定时器
  314. const timer = setInterval(() => {
  315. currentStep++;
  316. if (currentStep >= steps) {
  317. clearInterval(timer);
  318. // 到达起点,更新到达时间
  319. this.setData({
  320. estimatedArrival: 0
  321. });
  322. return;
  323. }
  324. // 更新司机标记位置
  325. const updatedMarkers = this.data.markers.map(marker => {
  326. if (marker.id === 999) {
  327. return {
  328. ...marker,
  329. longitude: marker.longitude + lonStep,
  330. latitude: marker.latitude + latStep
  331. };
  332. }
  333. return marker;
  334. });
  335. // 更新预计到达时间
  336. const newArrivalTime = Math.max(0, 5 - Math.floor(currentStep / 4));
  337. this.setData({
  338. markers: updatedMarkers,
  339. estimatedArrival: newArrivalTime
  340. });
  341. }, 500);
  342. },
  343. // 取消订单
  344. cancelOrder() {
  345. // 移除司机标记
  346. const updatedMarkers = this.data.markers.filter(marker => marker.id !== 999);
  347. this.setData({
  348. showDriverInfo: false,
  349. showCallButton: true,
  350. markers: updatedMarkers
  351. });
  352. }
  353. });