demo.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.228725,
  12. longitude: 121.475186,
  13. start: {
  14. address: "上海市黄浦区人民大道185号",
  15. errMsg: "chooseLocation:ok",
  16. latitude: 31.228725,
  17. longitude: 121.475186,
  18. name: "上海人民广场",
  19. },
  20. end: {
  21. address: "上海市虹口区东江湾路444号",
  22. errMsg: "chooseLocation:ok",
  23. latitude: 31.271447,
  24. longitude: 121.480604,
  25. name: "虹口足球场",
  26. },
  27. points: [],
  28. markers: [],
  29. isRecording: false,
  30. timeInterval: 10,
  31. },
  32. //在Page({})中使用下列代码
  33. //触发表单提交事件,调用接口
  34. formSubmit() {
  35. let { start, end } = this.data;
  36. if (Object.keys(start).length === 0) {
  37. wx.showToast({
  38. title: "请选择起点",
  39. icon: "none",
  40. duration: 2000,
  41. });
  42. return;
  43. }
  44. if (Object.keys(end).length === 0) {
  45. wx.showToast({
  46. title: "请选择终点",
  47. icon: "none",
  48. duration: 2000,
  49. });
  50. return;
  51. }
  52. let from = `${start.latitude},${start.longitude}`;
  53. let to = `${end.latitude},${end.longitude}`;
  54. //调用距离计算接口
  55. qqmapsdk.direction({
  56. mode: "driving", //可选值:'driving'(驾车)、'walking'(步行)、'bicycling'(骑行),不填默认:'driving',可不填
  57. //from参数不填默认当前地址
  58. from,
  59. to,
  60. success: (res) => {
  61. let coors = res.result.routes[0].polyline,
  62. pl = [];
  63. //坐标解压(返回的点串坐标,通过前向差分进行压缩)
  64. let kr = 1000000;
  65. for (let i = 2; i < coors.length; i++) {
  66. coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
  67. }
  68. //将解压后的坐标放入点串数组pl中
  69. for (let i = 0; i < coors.length; i += 2) {
  70. pl.push({ latitude: coors[i], longitude: coors[i + 1] });
  71. }
  72. //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点
  73. this.drawPolyline(pl);
  74. },
  75. fail: function (error) {
  76. console.error(error);
  77. },
  78. });
  79. },
  80. drawPolyline(pl) {
  81. wx.showLoading({
  82. title: "Loading...",
  83. });
  84. let t = setTimeout(() => {
  85. this.setData({
  86. points: [
  87. {
  88. latitude: pl[0].latitude,
  89. longitude: pl[0].longitude,
  90. },
  91. {
  92. latitude: pl.at(-1).latitude,
  93. longitude: pl.at(-1).longitude,
  94. },
  95. ],
  96. polyline: [
  97. {
  98. points: pl,
  99. color: "#FF0000DD",
  100. width: 4,
  101. },
  102. ],
  103. markers: [
  104. {
  105. latitude: pl[0].latitude,
  106. longitude: pl[0].longitude,
  107. iconPath: "../../images/start1.png",
  108. height: 30,
  109. width: 30,
  110. anchor: {
  111. x: 0.5,
  112. y: 1,
  113. },
  114. },
  115. {
  116. latitude: pl.at(-1).latitude,
  117. longitude: pl.at(-1).longitude,
  118. iconPath: "../../images/end1.png",
  119. height: 30,
  120. width: 30,
  121. anchor: {
  122. x: 0.5,
  123. y: 1,
  124. },
  125. },
  126. ],
  127. });
  128. wx.hideLoading();
  129. clearTimeout(t);
  130. }, 1000);
  131. },
  132. init() {
  133. let coors = data.result.routes[0].polyline,
  134. pl = [];
  135. //坐标解压(返回的点串坐标,通过前向差分进行压缩)
  136. let kr = 1000000;
  137. for (let i = 2; i < coors.length; i++) {
  138. coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
  139. }
  140. //将解压后的坐标放入点串数组pl中
  141. for (let i = 0; i < coors.length; i += 2) {
  142. pl.push({ latitude: coors[i], longitude: coors[i + 1] });
  143. }
  144. //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点
  145. this.drawPolyline(pl);
  146. },
  147. getStart() {
  148. wx.chooseLocation({
  149. success: (start) => {
  150. console.log(start);
  151. this.setData({
  152. start,
  153. });
  154. },
  155. });
  156. },
  157. getEnd() {
  158. wx.chooseLocation({
  159. success: (end) => {
  160. console.log(end);
  161. this.setData({
  162. end,
  163. });
  164. },
  165. });
  166. },
  167. getChangedLocation({ latitude, longitude }) {
  168. let currentTime = new Date().getTime();
  169. let oldLocation = wx.getStorageSync("oldLocation");
  170. let oldTime = wx.getStorageSync("oldTime");
  171. let newLocation = latitude + "" + longitude;
  172. if (
  173. oldLocation != newLocation &&
  174. currentTime - oldTime > this.data.timeInterval * 1000
  175. ) {
  176. wx.setStorageSync("oldLocation", newLocation);
  177. wx.setStorageSync("oldTime", currentTime);
  178. this.data.markers.push({
  179. latitude,
  180. longitude,
  181. iconPath: "../../images/circle-blue.png",
  182. height: 12,
  183. width: 12,
  184. });
  185. this.setData({
  186. markers: this.data.markers,
  187. });
  188. }
  189. },
  190. start() {
  191. if (this.data.isRecording) {
  192. wx.showModal({
  193. title: "结束记录?",
  194. content: "",
  195. complete: ({ confirm }) => {
  196. if (confirm) {
  197. wx.offLocationChange(this.getChangedLocation);
  198. wx.setKeepScreenOn({ keepScreenOn: false });
  199. this.setData({
  200. isRecording: false,
  201. });
  202. } else {
  203. }
  204. },
  205. });
  206. } else {
  207. wx.setKeepScreenOn({ keepScreenOn: true });
  208. wx.startLocationUpdate({
  209. complete: (res) => {
  210. wx.onLocationChange(this.getChangedLocation);
  211. },
  212. });
  213. wx.showToast({
  214. icon: "none",
  215. title: "开始记录...",
  216. });
  217. this.setData({
  218. isRecording: true,
  219. });
  220. }
  221. },
  222. onLoad() {
  223. // this.init();
  224. },
  225. onShow() {},
  226. });