routes.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //logs.js
  2. let util = require('../../utils/util.js');
  3. let wechat = require("../../utils/wechat");
  4. let amap = require("../../utils/amap");
  5. Page({
  6. data: {
  7. cindex: "0",
  8. types: ["getDrivingRoute", "getWalkingRoute", "getTransitRoute", "getRidingRoute"],
  9. markers: [],
  10. polyline: [],
  11. distance: '',
  12. cost: '',
  13. transits: [],
  14. city: "",
  15. name: "",
  16. desc: ""
  17. },
  18. onLoad(e) {
  19. let { latitude, longitude, latitude2, longitude2, city, name, desc } = e;
  20. let markers = [
  21. {
  22. iconPath: "/images/mapicon_navi_s.png",
  23. id: 0,
  24. latitude,
  25. longitude,
  26. width: 23,
  27. height: 33
  28. }, {
  29. iconPath: "/images/mapicon_navi_e.png",
  30. id: 1,
  31. latitude: latitude2,
  32. longitude: longitude2,
  33. width: 24,
  34. height: 34
  35. }
  36. ];
  37. this.setData({
  38. latitude, longitude, latitude2, longitude2, markers, city, name, desc
  39. });
  40. this.getRoute();
  41. },
  42. changeType(e) {
  43. let { id } = e.target.dataset;
  44. let { cindex } = this.data;
  45. if (id == cindex) return;
  46. this.setData({ cindex: id });
  47. this.getRoute();
  48. },
  49. getRoute() {
  50. let { latitude, longitude, latitude2, longitude2, types, cindex, city } = this.data;
  51. let type = types[cindex];
  52. let origin = `${longitude},${latitude}`;
  53. let destination = `${longitude2},${latitude2}`;
  54. amap.getRoute(origin, destination, type, city)
  55. .then(d => {
  56. // console.log(d);
  57. this.setRouteData(d, type);
  58. })
  59. .catch(e => {
  60. console.log(e);
  61. })
  62. },
  63. setRouteData(d, type) {
  64. if (type != "getTransitRoute") {
  65. let points = [];
  66. if (d.paths && d.paths[0] && d.paths[0].steps) {
  67. let steps = d.paths[0].steps;
  68. wx.setStorageSync("steps", steps);
  69. steps.forEach(item1 => {
  70. let poLen = item1.polyline.split(';');
  71. poLen.forEach(item2 => {
  72. let obj = {
  73. longitude: parseFloat(item2.split(',')[0]),
  74. latitude: parseFloat(item2.split(',')[1])
  75. }
  76. points.push(obj);
  77. })
  78. })
  79. }
  80. this.setData({
  81. polyline: [{
  82. points: points,
  83. color: "#0091ff",
  84. width: 6
  85. }]
  86. });
  87. }
  88. else {
  89. if (d && d.transits) {
  90. let transits = d.transits;
  91. transits.forEach(item1 => {
  92. let { segments } = item1;
  93. item1.transport = [];
  94. segments.forEach((item2, j) => {
  95. if (item2.bus && item2.bus.buslines && item2.bus.buslines[0] && item2.bus.buslines[0].name) {
  96. let name = item2.bus.buslines[0].name;
  97. if (j !== 0) {
  98. name = '--' + name;
  99. }
  100. item1.transport.push(name);
  101. }
  102. })
  103. })
  104. this.setData({ transits });
  105. }
  106. }
  107. if (type == "getDrivingRoute") {
  108. if (d.paths[0] && d.paths[0].distance) {
  109. this.setData({
  110. distance: d.paths[0].distance + '米'
  111. });
  112. }
  113. if (d.taxi_cost) {
  114. this.setData({
  115. cost: '打车约' + parseInt(d.taxi_cost) + '元'
  116. });
  117. }
  118. }
  119. else if (type == "getWalkingRoute" || type == "getRidingRoute") {
  120. if (d.paths[0] && d.paths[0].distance) {
  121. this.setData({
  122. distance: d.paths[0].distance + '米'
  123. });
  124. }
  125. if (d.paths[0] && d.paths[0].duration) {
  126. this.setData({
  127. cost: parseInt(d.paths[0].duration / 60) + '分钟'
  128. });
  129. }
  130. }
  131. },
  132. goDetail() {
  133. let url = `/pages/info/info`;
  134. wx.navigateTo({ url });
  135. },
  136. nav() {
  137. let { latitude2, longitude2, name, desc } = this.data;
  138. wx.openLocation({
  139. latitude: +latitude2,
  140. longitude: +longitude2,
  141. name,
  142. address: desc
  143. });
  144. }
  145. });