电速宝
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.js 9.9KB

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