Ver Fonte

新增 地图轨迹规划

wzg há 2 anos atrás
pai
commit
885d87465a

+ 30 - 35
miniprogram/app.js

@@ -1,56 +1,51 @@
-import {
-  wxSetSessionKey
-} from "./utils/wxUtils"
+import { wxSetSessionKey } from "./utils/wxUtils";
 App({
   onLaunch: function () {
-    const updateManager = wx.getUpdateManager()
+    const updateManager = wx.getUpdateManager();
 
     updateManager.onCheckForUpdate(function (res) {
       // 请求完新版本信息的回调
-    })
+    });
 
     updateManager.onUpdateReady(function () {
       wx.showModal({
-        title: '更新提示',
-        content: '新版本已经准备好,是否重启应用?',
+        title: "更新提示",
+        content: "新版本已经准备好,是否重启应用?",
         success: function (res) {
           if (res.confirm) {
             // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
-            updateManager.applyUpdate()
+            updateManager.applyUpdate();
           }
-        }
-      })
-    })
+        },
+      });
+    });
 
     updateManager.onUpdateFailed(function () {
       // 新版本下载失败
-    })
-
+    });
 
     wx.cloud.init({
-      traceUser: true
-    })
-    this.globalData = {}
+      traceUser: true,
+    });
+    this.globalData = {};
   },
   onShow() {
-    this.checkEnvVersion()
+    this.checkEnvVersion();
   },
   checkEnvVersion() {
-    let v = wx.getAccountInfoSync()
-    let appVersion = wx.getStorageSync('appVersion')
-    let {
-      envVersion
-    } = v.miniProgram
-    if (appVersion != envVersion) {
-      wx.clearStorage({
-        success: (res) => {
-          wx.setStorageSync('appVersion', envVersion)
-          wx.redirectTo({
-            url: '/pages/index/index',
-          })
-        },
-      })
-    }
-    wxSetSessionKey()
-  }
-})
+    let v = wx.getAccountInfoSync();
+    let appVersion = wx.getStorageSync("appVersion");
+    let { envVersion } = v.miniProgram;
+    // if (appVersion != envVersion) {
+    //   wx.clearStorage({
+    //     success: (res) => {
+    //       wx.setStorageSync("appVersion", envVersion);
+    //       wx.redirectTo({
+    //         url: "/pages/index/index",
+    //       });
+    //     },
+    //   });
+    // }
+    wxSetSessionKey();
+  },
+});

+ 10 - 1
miniprogram/app.json

@@ -13,7 +13,13 @@
     "pages/securityCheck/securityCheck",
     "pages/train/train"
   ],
-  "requiredPrivateInfos": ["getLocation", "chooseLocation"],
+  "requiredPrivateInfos": [
+    "getLocation",
+    "chooseLocation",
+    "startLocationUpdateBackground",
+    "onLocationChange",
+    "startLocationUpdate"
+  ],
   "window": {
     "backgroundColor": "#F6F6F6",
     "backgroundTextStyle": "light",
@@ -50,6 +56,9 @@
     "scope.userLocation": {
       "desc": "你的位置信息将用于小程序位置接口的效果展示"
     },
+    "scope.userLocationBackground": {
+      "desc": "你的位置信息将用于小程序位置接口的效果展示"
+    },
     "scope.chooseLocation": {
       "desc": "你的位置信息将用于小程序位置接口的效果展示"
     }

BIN
miniprogram/images/circle-blue.png


BIN
miniprogram/images/currentLocation.png


BIN
miniprogram/images/end.png


BIN
miniprogram/images/end1.png


BIN
miniprogram/images/start.png


BIN
miniprogram/images/start1.png


+ 459 - 139
miniprogram/pages/demo/data.js

@@ -1,201 +1,521 @@
 module.exports = {
   status: 0,
   message: "Success",
-  request_id: "b891b5d8af1543cf879fdfe6837c5104",
+  request_id: "c5a09e864ed24cb68d8076d1f8dcca26",
   result: {
     routes: [
       {
         mode: "DRIVING",
-        distance: 2587,
-        duration: 8,
-        traffic_light_count: 7,
+        distance: 13488,
+        duration: 15,
+        traffic_light_count: 0,
         toll: 0,
         restriction: { status: 1 },
         polyline: [
-          31.27177,
-          121.479892,
-          113,
-          85,
-          196,
-          217,
+          34.706028,
+          113.71177,
+          9,
+          290,
           0,
           0,
-          -10,
-          -236,
-          3,
-          -334,
-          4,
-          -220,
+          -636,
+          9,
+          -2067,
+          -5,
+          -1512,
+          11,
+          0,
+          0,
+          23,
+          -686,
+          -4,
+          -887,
           0,
           0,
-          409,
+          -14,
+          -408,
+          -62,
+          -212,
+          -21,
+          -98,
+          0,
+          0,
+          -119,
+          -291,
+          -30,
+          -1635,
+          0,
+          0,
+          -14,
+          -38,
+          -16,
+          -18,
+          -36,
+          -23,
+          -42,
+          -14,
+          -46,
+          4,
+          -40,
           22,
-          409,
-          51,
-          354,
-          61,
-          248,
-          59,
-          569,
-          190,
-          172,
-          72,
-          455,
-          208,
-          416,
-          224,
+          -48,
+          71,
+          66,
+          3040,
+          2,
+          234,
+          -20,
+          774,
           0,
           0,
-          187,
-          -143,
-          53,
-          -242,
-          151,
-          -514,
-          98,
-          -326,
+          -52,
+          329,
+          -96,
+          1256,
+          -225,
+          2561,
+          -15,
+          405,
+          45,
+          546,
+          -49,
+          695,
+          -231,
+          2692,
+          -67,
+          1069,
+          -39,
+          1092,
+          -7,
+          1178,
+          4,
+          350,
           61,
-          -167,
-          65,
-          -171,
-          119,
-          -266,
+          2227,
+          52,
+          1138,
+          49,
+          740,
+          91,
+          1059,
+          181,
+          1542,
           0,
           0,
+          111,
+          709,
+          1106,
+          7673,
           188,
-          -154,
-          358,
-          -650,
-          527,
-          -904,
-          -17,
-          -121,
+          1202,
+          231,
+          1215,
+          340,
+          1606,
+          148,
+          756,
+          149,
+          861,
+          148,
+          980,
+          98,
+          781,
           89,
-          -123,
-          193,
-          -298,
-          420,
-          -680,
-          210,
-          -295,
-          322,
-          -735,
+          874,
+          78,
+          1004,
+          64,
+          1389,
+          360,
+          13355,
+          -12,
+          674,
+          14,
+          638,
+          135,
+          5227,
+          11,
+          382,
+          16,
+          257,
+          0,
+          0,
+          -48,
+          116,
+          -11,
+          51,
+          -12,
+          237,
+          -10,
+          725,
+          -46,
+          767,
+          -119,
+          1479,
+          -15,
+          316,
+          0,
+          0,
+          26,
+          161,
+          45,
+          445,
+          68,
+          483,
+          37,
+          181,
+          59,
+          225,
+          96,
+          289,
+          94,
+          250,
+          105,
+          244,
+          118,
+          250,
+          68,
+          130,
+          120,
+          192,
+          172,
+          254,
+          114,
           157,
-          -418,
-          1138,
-          -3295,
-          235,
-          -635,
-          140,
-          -423,
+          119,
+          148,
+          131,
+          146,
+          288,
+          280,
+          131,
+          112,
+          175,
+          137,
+          156,
+          111,
+          134,
+          85,
+          180,
+          100,
+          158,
+          79,
+          205,
+          89,
+          165,
+          63,
+          240,
+          75,
+          266,
+          62,
+          1392,
+          199,
+          594,
+          100,
+          469,
+          106,
+          312,
+          105,
+          746,
+          171,
+          135,
+          5,
+          107,
+          -10,
+          132,
+          -31,
+          428,
+          127,
+          1320,
+          355,
+          569,
+          161,
+          397,
+          100,
+          1032,
+          276,
+          529,
+          130,
+          497,
+          109,
+          291,
           74,
-          -259,
-          53,
-          -306,
-          29,
-          -283,
-          332,
-          -3836,
+          5515,
+          1490,
+          397,
+          112,
+          890,
+          286,
+          489,
+          139,
+          3182,
+          858,
+          591,
+          149,
+          554,
+          101,
+          2725,
+          731,
+          971,
+          242,
+          657,
+          136,
+          759,
+          119,
+          183,
+          19,
+          892,
+          45,
+          411,
+          1,
+          658,
+          -33,
+          249,
+          -24,
+          645,
+          -81,
+          1132,
+          -198,
+          3951,
+          -762,
+          459,
+          -73,
+          328,
+          -43,
+          1173,
+          -212,
+          653,
+          -89,
+          485,
+          -49,
+          810,
+          -60,
+          270,
+          -28,
           0,
           0,
-          -162,
-          -93,
-          -236,
-          -9,
-          -193,
+          416,
+          84,
+          140,
+          9,
+          807,
+          -68,
+          1050,
+          -54,
+          392,
+          12,
+          119,
           25,
-          -172,
-          46,
-          -67,
-          32,
-          -64,
-          50,
-          -355,
-          81,
-          -531,
-          101,
-          -1046,
-          186,
-          -775,
-          151,
+          56,
+          -15,
           0,
           0,
+          1457,
+          -164,
+          535,
+          -42,
+          584,
+          -33,
+          895,
+          -39,
+          426,
+          -5,
+          408,
+          110,
+          375,
+          148,
+          188,
+          59,
+          60,
+          6,
+          63,
+          -4,
+          217,
+          -50,
+          84,
+          -48,
+          87,
+          -75,
+          65,
+          -85,
+          61,
+          -105,
+          42,
+          -93,
+          28,
+          -81,
+          94,
+          -411,
+          20,
+          -290,
+          21,
+          -113,
+          29,
+          -107,
+          32,
+          -85,
+          68,
+          -128,
+          39,
           -61,
-          -253,
-          -35,
-          -87,
-          -121,
-          -256,
-          -100,
-          -269,
+          52,
+          -62,
+          119,
+          -112,
+          102,
+          -70,
+          102,
+          -44,
+          131,
+          -38,
+          127,
+          -20,
+          230,
+          -19,
+          802,
+          -22,
+          453,
+          -26,
+          1067,
+          -96,
+          49,
+          22,
+          45,
+          5,
+          434,
+          -29,
+          165,
+          -36,
+          0,
+          0,
+          29,
+          -18,
+          22,
+          -32,
+          32,
+          -66,
         ],
         steps: [
           {
-            instruction: "沿内部道路行驶44米,左转",
-            polyline_idx: [0, 5],
-            road_name: "内部道路",
-            dir_desc: "",
-            distance: 44,
-            act_desc: "左转",
+            instruction: "向东行驶26米,右转然后立即向右前方行驶",
+            polyline_idx: [0, 3],
+            road_name: "",
+            dir_desc: "东",
+            distance: 26,
+            act_desc: "右转",
+            accessorial_desc: "然后立即向右前方行驶",
+          },
+          {
+            instruction: "沿南台路向南行驶467米,右转",
+            polyline_idx: [4, 11],
+            road_name: "南台路",
+            dir_desc: "南",
+            distance: 467,
+            act_desc: "右转",
             accessorial_desc: "",
           },
           {
-            instruction: "沿内部道路向西行驶75米,右转",
-            polyline_idx: [6, 13],
-            road_name: "内部道路",
+            instruction: "沿南三环辅路向西行驶144米,",
+            polyline_idx: [12, 17],
+            road_name: "南三环辅路",
             dir_desc: "西",
-            distance: 75,
-            act_desc: "右转",
+            distance: 144,
+            act_desc: "",
             accessorial_desc: "",
           },
           {
-            instruction: "沿东江湾路行驶349米,左转",
-            polyline_idx: [14, 31],
-            road_name: "东江湾路",
-            dir_desc: "",
-            distance: 349,
-            act_desc: "左转",
+            instruction: "沿南三环辅路向西行驶67米,偏左转进入主路",
+            polyline_idx: [18, 25],
+            road_name: "南三环辅路",
+            dir_desc: "西",
+            distance: 67,
+            act_desc: "偏左转",
+            accessorial_desc: "进入主路",
+          },
+          {
+            instruction: "沿南三环向西行驶179米,",
+            polyline_idx: [26, 31],
+            road_name: "南三环",
+            dir_desc: "西",
+            distance: 179,
+            act_desc: "",
             accessorial_desc: "",
           },
           {
-            instruction: "沿广中路行驶197米,偏右转",
-            polyline_idx: [32, 47],
-            road_name: "广中路",
-            dir_desc: "",
-            distance: 197,
+            instruction: "沿南三环向东行驶405米,偏右转上高架",
+            polyline_idx: [32, 53],
+            road_name: "南三环",
+            dir_desc: "",
+            distance: 405,
             act_desc: "偏右转",
+            accessorial_desc: "上高架",
+          },
+          {
+            instruction: "沿南三环向东行驶1.7公里,直行",
+            polyline_idx: [54, 87],
+            road_name: "南三环",
+            dir_desc: "东",
+            distance: 1736,
+            act_desc: "直行",
             accessorial_desc: "",
           },
           {
-            instruction: "沿广中路行驶1.4公里,左转",
-            polyline_idx: [48, 83],
-            road_name: "广中路",
+            instruction: "沿南三环向东行驶3.7公里,偏右转进入匝道",
+            polyline_idx: [88, 125],
+            road_name: "南三环",
+            dir_desc: "东",
+            distance: 3656,
+            act_desc: "偏右转",
+            accessorial_desc: "进入匝道",
+          },
+          {
+            instruction: "沿东三环行驶340米,偏左转",
+            polyline_idx: [126, 141],
+            road_name: "东三环",
             dir_desc: "",
-            distance: 1400,
-            act_desc: "左转",
+            distance: 340,
+            act_desc: "左转",
             accessorial_desc: "",
           },
           {
-            instruction: "沿株洲路向南行驶408米,右转然后立即左转",
-            polyline_idx: [84, 105],
-            road_name: "株洲路",
-            dir_desc: "南",
-            distance: 408,
-            act_desc: "右转",
-            accessorial_desc: "然后立即左转",
+            instruction: "沿东三环向北行驶4.9公里,偏右转不要下坡",
+            polyline_idx: [142, 279],
+            road_name: "东三环",
+            dir_desc: "北",
+            distance: 4916,
+            act_desc: "偏右转",
+            accessorial_desc: "不要下坡",
+          },
+          {
+            instruction: "沿东三环向北行驶332米,直行进入匝道",
+            polyline_idx: [280, 295],
+            road_name: "东三环",
+            dir_desc: "北",
+            distance: 332,
+            act_desc: "直行",
+            accessorial_desc: "进入匝道",
+          },
+          {
+            instruction: "沿东三环出口行驶1.2公里,",
+            polyline_idx: [296, 373],
+            road_name: "东三环出口",
+            dir_desc: "",
+            distance: 1202,
+            act_desc: "",
+            accessorial_desc: "",
           },
           {
-            instruction: "沿内部道路行驶89米,",
-            polyline_idx: [106, 115],
-            road_name: "内部道路",
+            instruction: "行驶14米,",
+            polyline_idx: [374, 381],
+            road_name: "",
             dir_desc: "",
-            distance: 89,
+            distance: 14,
             act_desc: "",
             accessorial_desc: "",
           },
         ],
         tags: [],
-        taxi_fare: { fare: 18 },
+        taxi_fare: { fare: 27 },
       },
     ],
   },

+ 120 - 21
miniprogram/pages/demo/demo.js

@@ -9,22 +9,26 @@ let qqmapsdk = new QQMapWX({
 });
 Page({
   data: {
-    latitude: 31.271729,
-    longitude: 121.483641,
+    latitude: 31.228725,
+    longitude: 121.475186,
     start: {
+      address: "上海市黄浦区人民大道185号",
+      errMsg: "chooseLocation:ok",
+      latitude: 31.228725,
+      longitude: 121.475186,
+      name: "上海人民广场",
+    },
+    end: {
       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: "意和家园",
-    },
+    points: [],
+    markers: [],
+    isRecording: false,
+    timeInterval: 10,
   },
   //在Page({})中使用下列代码
   //触发表单提交事件,调用接口
@@ -75,17 +79,56 @@ Page({
     });
   },
   drawPolyline(pl) {
-    this.setData({
-      latitude: pl[0].latitude,
-      longitude: pl[0].longitude,
-      polyline: [
-        {
-          points: pl,
-          color: "#FF0000DD",
-          width: 4,
-        },
-      ],
+    wx.showLoading({
+      title: "Loading...",
     });
+    let t = setTimeout(() => {
+      this.setData({
+        points: [
+          {
+            latitude: pl[0].latitude,
+            longitude: pl[0].longitude,
+          },
+          {
+            latitude: pl.at(-1).latitude,
+            longitude: pl.at(-1).longitude,
+          },
+        ],
+        polyline: [
+          {
+            points: pl,
+            color: "#FF0000DD",
+            width: 4,
+          },
+        ],
+        markers: [
+          {
+            latitude: pl[0].latitude,
+            longitude: pl[0].longitude,
+            iconPath: "../../images/start1.png",
+            height: 30,
+            width: 30,
+            anchor: {
+              x: 0.5,
+              y: 1,
+            },
+          },
+          {
+            latitude: pl.at(-1).latitude,
+            longitude: pl.at(-1).longitude,
+            iconPath: "../../images/end1.png",
+            height: 30,
+            width: 30,
+            anchor: {
+              x: 0.5,
+              y: 1,
+            },
+          },
+        ],
+      });
+      wx.hideLoading();
+      clearTimeout(t);
+    }, 1000);
   },
   init() {
     let coors = data.result.routes[0].polyline,
@@ -116,13 +159,69 @@ Page({
     wx.chooseLocation({
       success: (end) => {
         console.log(end);
-
         this.setData({
           end,
         });
       },
     });
   },
-  onLoad() {},
+  getChangedLocation({ latitude, longitude }) {
+    let currentTime = new Date().getTime();
+    let oldLocation = wx.getStorageSync("oldLocation");
+    let oldTime = wx.getStorageSync("oldTime");
+    let newLocation = latitude + "" + longitude;
+    if (
+      oldLocation != newLocation &&
+      currentTime - oldTime > this.data.timeInterval * 1000
+    ) {
+      wx.setStorageSync("oldLocation", newLocation);
+      wx.setStorageSync("oldTime", currentTime);
+      this.data.markers.push({
+        latitude,
+        longitude,
+        iconPath: "../../images/circle-blue.png",
+        height: 12,
+        width: 12,
+      });
+      this.setData({
+        markers: this.data.markers,
+      });
+    }
+  },
+  start() {
+    if (this.data.isRecording) {
+      wx.showModal({
+        title: "结束记录?",
+        content: "",
+        complete: ({ confirm }) => {
+          if (confirm) {
+            wx.offLocationChange(this.getChangedLocation);
+            wx.setKeepScreenOn({ keepScreenOn: false });
+            this.setData({
+              isRecording: false,
+            });
+          } else {
+          }
+        },
+      });
+    } else {
+      wx.setKeepScreenOn({ keepScreenOn: true });
+      wx.startLocationUpdate({
+        complete: (res) => {
+          wx.onLocationChange(this.getChangedLocation);
+        },
+      });
+      wx.showToast({
+        icon: "none",
+        title: "开始记录...",
+      });
+      this.setData({
+        isRecording: true,
+      });
+    }
+  },
+  onLoad() {
+    // this.init();
+  },
   onShow() {},
 });

+ 9 - 3
miniprogram/pages/demo/demo.wxml

@@ -1,5 +1,5 @@
 <!--地图容器-->
-<map id="myMap" style="width: 100%; height: 300px;margin-bottom: 30rpx;" longitude="{{longitude}}" latitude="{{latitude}}" scale='14' polyline="{{polyline}}" show-location>
+<map id="myMap" style="width: 100%; height: 300px;margin-bottom: 30rpx;" longitude="{{longitude}}" latitude="{{latitude}}" scale='14' polyline="{{polyline}}" include-points='{{points}}' show-location markers="{{markers}}">
 </map>
 <view class="card">
   <view class="df aic jcsa mb30" style="width: 100%;">
@@ -10,7 +10,13 @@
     <view>终点:</view>
     <view class="ipt" bind:tap="getEnd">{{end.name||'选择终点位置'}}</view>
   </view>
+  <view class="df aic jcsa mb30">
+    <view>时间间隔(秒):</view>
+    <input class="ipt" type="number" model:value="{{timeInterval}}" style="border: 1px solid grey;" />
+  </view>
   <!--提交表单数据-->
-  <!-- <button class="mb20" type="primary" bind:tap="formSubmit">路线规划</button> -->
-  <button type="primary" bind:tap="init">Demo</button>
+  <button class="mb20" type="primary" bind:tap="start">{{isRecording?'停止记录':'开始记录'}}</button>
+  <!-- <button class="mb20" type="primary" bind:tap="init">路线规划</button> -->
+  <button class="mb20" type="primary" bind:tap="formSubmit">路线规划</button>
+  <!-- <button type="primary" bind:tap="getMyLocation">getMyLocation</button> -->
 </view>