index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. var $ = require('../../libs/gdconf.js');
  2. var app = getApp()
  3. Page({
  4. data: {
  5. navType: $.navType,
  6. type: '驾车',
  7. indexCurrent: 0,
  8. selected: '',
  9. cache: [],
  10. showMap: false,
  11. showDetail: false,
  12. showBusPaths: false
  13. },
  14. onLoad(opts) {
  15. /******
  16. 请前往高地地图api 获取Web服务 API开发需要的Key
  17. http://lbs.amap.com/api/webservice/guide/create-project/get-key
  18. *******/
  19. if ($.webKey === null) {
  20. $.dialog('缺少高地地图Web服务 API开发需要的Key!请添加您自己的高德地图Key!');
  21. return;
  22. }
  23. //搜索列表
  24. if (opts.fromhistory == 0) {
  25. opts.POIlatitude = Number(opts.POIlocation.split(',')[1]);
  26. opts.POIlongitude = Number(opts.POIlocation.split(',')[0]);
  27. opts.x = 0;
  28. var history = wx.getStorageSync("historyList");
  29. if (history.length == 0) {
  30. wx.setStorageSync("historyList", [opts]);
  31. } else {
  32. var name = [];
  33. history.forEach(function (item) {
  34. name.push(item.name);
  35. });
  36. if (name.indexOf(opts.name) < 0) {
  37. history.unshift(opts);
  38. wx.setStorageSync("historyList", history);
  39. }
  40. }
  41. }
  42. //初始化地图控件
  43. var that = this;
  44. this.mapCtx = wx.createMapContext('myMap');
  45. this.setData(opts);
  46. wx.setNavigationBarTitle({
  47. title: that.data.type + '路线'
  48. });
  49. this.setData({
  50. markers: [{
  51. iconPath: "../../images/gdmap/mapicon_navi_s.png",
  52. id: 0,
  53. latitude: opts.latitude,
  54. longitude: opts.longitude,
  55. width: 23,
  56. height: 33
  57. }, {
  58. iconPath: "../../images/gdmap/mapicon_navi_e.png",
  59. id: 1,
  60. latitude: opts.POIlatitude,
  61. longitude: opts.POIlongitude,
  62. width: 24,
  63. height: 34
  64. }]
  65. });
  66. this.mapCtx.includePoints({
  67. padding: [50, 50, 50, 50],
  68. points: [{
  69. latitude: opts.latitude,
  70. longitude: opts.longitude
  71. }, {
  72. latitude: opts.POIlatitude,
  73. longitude: opts.POIlongitude
  74. }]
  75. });
  76. this.getRoute(this.data.navType, 0);
  77. },
  78. //切换出行方案
  79. changeNavType(e) {
  80. var idx = e.currentTarget.dataset.idx, data = this.data.navType;
  81. if (idx == this.data.selected) {
  82. return;
  83. }
  84. if (idx > 1) {
  85. this.alert();
  86. return;
  87. }
  88. this.setData({ type: data[idx].name, indexCurrent: idx, selected: idx });
  89. wx.setNavigationBarTitle({
  90. title: data[idx].name + '路线'
  91. });
  92. //公交路线
  93. if (idx == 1) {
  94. this.getTransitRoute(0);
  95. } else {
  96. this.getRoute(data, idx)
  97. }
  98. },
  99. alert() {
  100. $.dialog("暂未开发");
  101. },
  102. //驾车
  103. getRoute(data, idx) {
  104. var that = this, temp = this.data.cache;
  105. if (typeof temp[idx] == 'object') {
  106. this.setData(temp[idx].info);
  107. this.setData(temp[idx].polyline);
  108. this.setData(temp[idx].other);
  109. wx.hideLoading();
  110. return;
  111. }
  112. wx.showLoading({ title: 'loading', mask: true });
  113. var origin = this.data.longitude + ',' + this.data.latitude;
  114. var destination = this.data.POIlongitude + ',' + this.data.POIlatitude;
  115. wx.request({
  116. url: 'https://restapi.amap.com/v3/distance',
  117. data: {
  118. key: $.webKey,
  119. origins: origin,
  120. destination: destination
  121. },
  122. success(res) {
  123. var distance = res.data.results[0].distance;
  124. // 驾车规划限时500公里
  125. if (data[idx].name == '驾车') {
  126. if (distance >= 500000) {
  127. wx.hideLoading();
  128. $.dialog("起点终点距离过长,驾车规划限制500公里以内");
  129. return;
  130. }
  131. }
  132. wx.request({
  133. url: data[idx].url,
  134. data: {
  135. key: $.webKey,
  136. origin: origin,
  137. destination: destination
  138. },
  139. success(res) {
  140. var data = res.data.route ? res.data.route : res.data.data;
  141. var path = data.paths[0],
  142. info = { info: data },
  143. polyline = {
  144. polyline: [{
  145. points: $.polyline(data),
  146. color: "#0091ff",
  147. width: 6
  148. }]
  149. },
  150. other = {
  151. distance: $.distance(path.distance),
  152. duration: path.duration ? $.resultFormat(path.duration) : false,
  153. cost: data.taxi_cost ? parseInt(data.taxi_cost) : false,
  154. lights: path.traffic_lights ? path.traffic_lights : false,
  155. showMap: true,
  156. showNav: true,
  157. showDetail: true,
  158. showBusPaths: false
  159. };
  160. that.setData(info);
  161. that.setData(polyline);
  162. that.setData(other);
  163. temp[idx] = {
  164. info: info,
  165. polyline: polyline,
  166. other: other
  167. };
  168. that.setData({
  169. cache: temp
  170. });
  171. wx.hideLoading();
  172. }
  173. });
  174. }
  175. })
  176. },
  177. //公交线路
  178. getTransitRoute(strategy, fromOpts) {
  179. wx.showLoading({ title: 'loading', mask: true });
  180. var that = this, temp = this.data.cache;
  181. if (typeof temp[1] == 'object' && !fromOpts) {
  182. that.setData(temp[1].transits);
  183. that.setData(temp[1].other);
  184. wx.hideLoading();
  185. return;
  186. }
  187. wx.request({
  188. url: 'https://restapi.amap.com/v3/direction/transit/integrated',
  189. data: {
  190. key: $.webKey,
  191. origin: that.data.longitude + ',' + that.data.latitude,
  192. destination: that.data.POIlongitude + ',' + that.data.POIlatitude,
  193. city: that.data.city,
  194. cityd: that.data.cityd,
  195. strategy: strategy,
  196. nightflag: 0
  197. },
  198. success(res) {
  199. if (res.data.errcode) {
  200. wx.hideLoading();
  201. $.dialog(res.data.errdetail);
  202. return;
  203. }
  204. var route = res.data.route, transits = route.transits;
  205. for (var i = 0; i < transits.length; i++) {
  206. var item = transits[i], segments = item.segments;
  207. item.cost = parseInt(item.cost);
  208. item.distance = $.distance(item.distance);
  209. item.walking_distance = $.distance(item.walking_distance);
  210. item.duration = $.resultFormat(item.duration);
  211. item.transport = [];
  212. for (var j = 0; j < segments.length; j++) {
  213. if (segments[j].bus.buslines.length > 0) {
  214. var name = segments[j].bus.buslines[0].name.split('(')[0];
  215. if (j !== 0) {
  216. name = ' → ' + name;
  217. }
  218. item.transport.push(name);
  219. }
  220. if (segments[j].railway.spaces.length > 0) {
  221. var name = segments[j].railway.name;
  222. if (j !== 0) {
  223. name = ' → ' + name;
  224. }
  225. item.transport.push(name);
  226. }
  227. }
  228. }
  229. wx.setStorageSync('transits', transits);
  230. var transits = { transits: transits },
  231. other = {
  232. distance: $.distance(route.distance),
  233. cost: route.taxi_cost ? parseInt(route.taxi_cost) : false,
  234. showMap: false,
  235. showDetail: false,
  236. showNav: false,
  237. showBusPaths: true
  238. };
  239. that.setData(transits);
  240. that.setData(other);
  241. temp[1] = {
  242. transits: transits,
  243. other: other
  244. };
  245. that.setData({
  246. cache: temp
  247. });
  248. wx.hideLoading();
  249. }
  250. })
  251. },
  252. //切换公交出行方案
  253. transferOpts() {
  254. var that = this;
  255. wx.showActionSheet({
  256. itemList: ['最便捷', '最经济', '少换乘', '少步行', '不坐地铁'],
  257. success(res) {
  258. var type = (res.tapIndex == 4) ? (res.tapIndex + 1) : res.tapIndex;
  259. that.getTransitRoute(type, true);
  260. }
  261. })
  262. },
  263. //导航
  264. showNav() {
  265. wx.openLocation({
  266. name: this.data.name,
  267. address: this.data.address,
  268. latitude: Number(this.data.POIlatitude),
  269. longitude: Number(this.data.POIlongitude)
  270. })
  271. },
  272. onError(err) {
  273. app.aldstat.sendEvent('报错',{
  274. 'err': err
  275. });
  276. },
  277. })