carPosition.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.data[0] == 0) {
  37. wx.showToast({
  38. title: '暂无车辆最后位置信息~',
  39. icon: 'none'
  40. })
  41. wx.getLocation({
  42. type: 'gcj02',
  43. success: function (res) {
  44. var latitude = res.latitude
  45. var longitude = res.longitude
  46. that.setData({
  47. longitude: longitude,
  48. latitude: latitude
  49. })
  50. },
  51. })
  52. } else {
  53. let position = [];
  54. position[0] = {
  55. width: 32,
  56. height: 32,
  57. zIndex: 1111,
  58. iconPath: 'http://resource.bike.hanyiyun.com/common/start-location-mark-old.png',
  59. longitude: res.data[0],
  60. latitude: res.data[1]
  61. }
  62. that.setData({
  63. markers: position,
  64. longitude: res.data[0],
  65. latitude: res.data[1]
  66. })
  67. }
  68. })
  69. },
  70. //初始化日期
  71. timeInit() {
  72. let that = this;
  73. let start = new Date();
  74. start.setTime(start.getTime()-24*60*60*1000)
  75. let startTime = that.getTime(start)
  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: startTime,
  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. console.log(res,'车辆位置')
  128. if (res.data == '') {
  129. wx.showToast({
  130. title: '暂无骑行轨迹~',
  131. icon: 'none'
  132. })
  133. } else {
  134. that.setData({
  135. polyline:res.data.all_locations,
  136. markers:res.data.points
  137. })
  138. }
  139. })
  140. }
  141. },
  142. hidn() {
  143. let that = this;
  144. that.timeInit();
  145. that.setData({
  146. polyline: [],
  147. markers: '',
  148. })
  149. },
  150. })