carPosition.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. var app = getApp()
  2. Page({
  3. data: {
  4. markers: [],
  5. polyline: [],
  6. controls: [],
  7. hasEmptyGrid: false,
  8. bikeId: '',
  9. bike_no: '',
  10. longitude: '',
  11. latitude: '',
  12. startTime: "", //开始时间 默认昨天
  13. endTime: "", //结束时间 默认今天
  14. },
  15. onLoad: function (options) {
  16. let that = this;
  17. that.setData({
  18. bikeId: options.id,
  19. bike_no: options.bike_no,
  20. });
  21. that.getPosition();
  22. that.timeInit();
  23. },
  24. //获取当前车辆信息
  25. getPosition() {
  26. wx.showLoading({
  27. title: '获取位置中...',
  28. })
  29. let that = this;
  30. let data = {
  31. bike_no: that.data.bike_no
  32. }
  33. app.request('bike/position', data, 'GET').then(res => {
  34. wx.hideLoading();
  35. console.log(res);
  36. if (res.statusCode == 200) {
  37. if (res.data[0] == 0) {
  38. wx.showToast({
  39. title: '暂无车辆最后位置信息~',
  40. icon: 'none'
  41. })
  42. wx.getLocation({
  43. type: 'gcj02',
  44. success: function (res) {
  45. var latitude = res.latitude
  46. var longitude = res.longitude
  47. that.setData({
  48. longitude: longitude,
  49. latitude: latitude
  50. })
  51. },
  52. })
  53. } else {
  54. let position = [];
  55. position[0] = {
  56. width: 32,
  57. height: 32,
  58. zIndex: 1111,
  59. iconPath: 'http://resource.weilaibike.com/common/start-location-mark-old.png',
  60. longitude: res.data[0],
  61. latitude: res.data[1]
  62. }
  63. that.setData({
  64. markers: position,
  65. longitude: res.data[0],
  66. latitude: res.data[1]
  67. })
  68. }
  69. }
  70. })
  71. },
  72. //初始化日期
  73. timeInit() {
  74. let that = this;
  75. let start = that.getTime(new Date());
  76. let tom = new Date();
  77. tom.setTime(tom.getTime() + 24 * 60 * 60 * 1000);
  78. let end = that.getTime(tom);
  79. that.setData({
  80. startTime: start,
  81. endTime: end
  82. })
  83. },
  84. //获取年月日 时分秒
  85. getTime(date) {
  86. var year = date.getFullYear();
  87. var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  88. var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate()
  89. var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
  90. var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
  91. var today = year + "-" + month + "-" + day + " " + hours + ":" + minute;
  92. return today;
  93. },
  94. start(e) {
  95. this.setData({
  96. startTime: e.detail.dateString
  97. })
  98. },
  99. end(e) {
  100. this.setData({
  101. endTime: e.detail.dateString
  102. })
  103. },
  104. show: function () {
  105. wx.showLoading({
  106. title: '加载中...',
  107. })
  108. var that = this;
  109. let start = new Date(that.data.startTime).getTime();
  110. let end = new Date(that.data.endTime).getTime();
  111. if (start > end) {
  112. wx.showToast({
  113. title: '开始日期不能大于结束日期',
  114. icon: 'none'
  115. })
  116. } else {
  117. that.setData({
  118. markers: []
  119. })
  120. var data = {
  121. 'bike_no': that.data.bike_no,
  122. 'time_between[0]': that.data.startTime,
  123. 'time_between[1]': that.data.endTime,
  124. }
  125. app.request('bike/bikeLocation', data, 'POST').then(res => {
  126. wx.hideLoading();
  127. if (res.statusCode == 200) {
  128. console.log(res,'车辆位置')
  129. if (res.data == '') {
  130. wx.showToast({
  131. title: '暂无骑行轨迹~',
  132. icon: 'none'
  133. })
  134. } else {
  135. that.setData({
  136. polyline:res.data.all_locations,
  137. markers:res.data.points
  138. })
  139. }
  140. }
  141. })
  142. }
  143. },
  144. hidn() {
  145. let that = this;
  146. that.timeInit();
  147. that.setData({
  148. polyline: [],
  149. markers: '',
  150. })
  151. },
  152. })