demo.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // pages/demo/demo.js
  2. const data = require("./data.js");
  3. // 引入SDK核心类
  4. let QQMapWX = require("../../utils/qqmap-wx-jssdk.min");
  5. // 实例化API核心类
  6. let qqmapsdk = new QQMapWX({
  7. key: "Y2BBZ-IHRKU-V42VO-BFQEE-K7252-ZBBSF", // 必填
  8. });
  9. Page({
  10. data: {
  11. latitude: 31.271729,
  12. longitude: 121.483641,
  13. start: {
  14. address: "上海市虹口区东江湾路444号",
  15. errMsg: "chooseLocation:ok",
  16. latitude: 31.271447,
  17. longitude: 121.480604,
  18. name: "虹口足球场",
  19. },
  20. end: {
  21. address: "上海市虹口区株洲路258弄",
  22. errMsg: "chooseLocation:ok",
  23. latitude: 31.276367,
  24. longitude: 121.464754,
  25. name: "意和家园",
  26. },
  27. },
  28. //在Page({})中使用下列代码
  29. //触发表单提交事件,调用接口
  30. formSubmit() {
  31. let { start, end } = this.data;
  32. if (Object.keys(start).length === 0) {
  33. wx.showToast({
  34. title: "请选择起点",
  35. icon: "none",
  36. duration: 2000,
  37. });
  38. return;
  39. }
  40. if (Object.keys(end).length === 0) {
  41. wx.showToast({
  42. title: "请选择终点",
  43. icon: "none",
  44. duration: 2000,
  45. });
  46. return;
  47. }
  48. let from = `${start.latitude},${start.longitude}`;
  49. let to = `${end.latitude},${end.longitude}`;
  50. //调用距离计算接口
  51. qqmapsdk.direction({
  52. mode: "driving", //可选值:'driving'(驾车)、'walking'(步行)、'bicycling'(骑行),不填默认:'driving',可不填
  53. //from参数不填默认当前地址
  54. from,
  55. to,
  56. success: (res) => {
  57. let coors = res.result.routes[0].polyline,
  58. pl = [];
  59. //坐标解压(返回的点串坐标,通过前向差分进行压缩)
  60. let kr = 1000000;
  61. for (let i = 2; i < coors.length; i++) {
  62. coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
  63. }
  64. //将解压后的坐标放入点串数组pl中
  65. for (let i = 0; i < coors.length; i += 2) {
  66. pl.push({ latitude: coors[i], longitude: coors[i + 1] });
  67. }
  68. //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点
  69. this.drawPolyline(pl);
  70. },
  71. fail: function (error) {
  72. console.error(error);
  73. },
  74. });
  75. },
  76. drawPolyline(pl) {
  77. this.setData({
  78. latitude: pl[0].latitude,
  79. longitude: pl[0].longitude,
  80. polyline: [
  81. {
  82. points: pl,
  83. color: "#FF0000DD",
  84. width: 4,
  85. },
  86. ],
  87. });
  88. },
  89. init() {
  90. let coors = data.result.routes[0].polyline,
  91. pl = [];
  92. //坐标解压(返回的点串坐标,通过前向差分进行压缩)
  93. let kr = 1000000;
  94. for (let i = 2; i < coors.length; i++) {
  95. coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
  96. }
  97. //将解压后的坐标放入点串数组pl中
  98. for (let i = 0; i < coors.length; i += 2) {
  99. pl.push({ latitude: coors[i], longitude: coors[i + 1] });
  100. }
  101. //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点
  102. this.drawPolyline(pl);
  103. },
  104. getStart() {
  105. wx.chooseLocation({
  106. success: (start) => {
  107. console.log(start);
  108. this.setData({
  109. start,
  110. });
  111. },
  112. });
  113. },
  114. getEnd() {
  115. wx.chooseLocation({
  116. success: (end) => {
  117. console.log(end);
  118. this.setData({
  119. end,
  120. });
  121. },
  122. });
  123. },
  124. onLoad() {},
  125. onShow() {},
  126. });