| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- // pages/demo/demo.js
- const data = require("./data.js");
- // 引入SDK核心类
- let QQMapWX = require("../../utils/qqmap-wx-jssdk.min");
- // 实例化API核心类
- let qqmapsdk = new QQMapWX({
- key: "Y2BBZ-IHRKU-V42VO-BFQEE-K7252-ZBBSF", // 必填
- });
- Page({
- data: {
- latitude: 31.271729,
- longitude: 121.483641,
- start: {
- address: "上海市虹口区东江湾路444号",
- errMsg: "chooseLocation:ok",
- latitude: 31.271447,
- longitude: 121.480604,
- name: "虹口足球场",
- },
- end: {
- address: "上海市虹口区株洲路258弄",
- errMsg: "chooseLocation:ok",
- latitude: 31.276367,
- longitude: 121.464754,
- name: "意和家园",
- },
- },
- //在Page({})中使用下列代码
- //触发表单提交事件,调用接口
- formSubmit() {
- let { start, end } = this.data;
- if (Object.keys(start).length === 0) {
- wx.showToast({
- title: "请选择起点",
- icon: "none",
- duration: 2000,
- });
- return;
- }
- if (Object.keys(end).length === 0) {
- wx.showToast({
- title: "请选择终点",
- icon: "none",
- duration: 2000,
- });
- return;
- }
- let from = `${start.latitude},${start.longitude}`;
- let to = `${end.latitude},${end.longitude}`;
- //调用距离计算接口
- qqmapsdk.direction({
- mode: "driving", //可选值:'driving'(驾车)、'walking'(步行)、'bicycling'(骑行),不填默认:'driving',可不填
- //from参数不填默认当前地址
- from,
- to,
- success: (res) => {
- let coors = res.result.routes[0].polyline,
- pl = [];
- //坐标解压(返回的点串坐标,通过前向差分进行压缩)
- let kr = 1000000;
- for (let i = 2; i < coors.length; i++) {
- coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
- }
- //将解压后的坐标放入点串数组pl中
- for (let i = 0; i < coors.length; i += 2) {
- pl.push({ latitude: coors[i], longitude: coors[i + 1] });
- }
- //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点
- this.drawPolyline(pl);
- },
- fail: function (error) {
- console.error(error);
- },
- });
- },
- drawPolyline(pl) {
- this.setData({
- latitude: pl[0].latitude,
- longitude: pl[0].longitude,
- polyline: [
- {
- points: pl,
- color: "#FF0000DD",
- width: 4,
- },
- ],
- });
- },
- init() {
- let coors = data.result.routes[0].polyline,
- pl = [];
- //坐标解压(返回的点串坐标,通过前向差分进行压缩)
- let kr = 1000000;
- for (let i = 2; i < coors.length; i++) {
- coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
- }
- //将解压后的坐标放入点串数组pl中
- for (let i = 0; i < coors.length; i += 2) {
- pl.push({ latitude: coors[i], longitude: coors[i + 1] });
- }
- //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点
- this.drawPolyline(pl);
- },
- getStart() {
- wx.chooseLocation({
- success: (start) => {
- console.log(start);
- this.setData({
- start,
- });
- },
- });
- },
- getEnd() {
- wx.chooseLocation({
- success: (end) => {
- console.log(end);
- this.setData({
- end,
- });
- },
- });
- },
- onLoad() {},
- onShow() {},
- });
|