Vue中的高德轨迹回放

高德地图实现轨迹回放的方式之一:使用高德地图官方api中UI组件库中的轨迹展示。

通过轨迹展示创建巡航器,实现轨迹回放,下面展示HTML以及Vue的高德轨迹回放,可以点击下方链接直接进入高德开发平台!

高德开放平台

HTML版高德轨迹回放

进入页面后点击开发支持→地图js API

-1

进入地图JS API后点击示例中心,进入界面后拉到底下找到轨迹回放

-2

进入后打开轨迹回放如下图中已有示例,模拟小车的行驶轨迹。其模拟的轨迹为右侧框图的小车模拟位置点, 将自己获取的经纬度点替代上面提到的小车模拟位置点即可验证自己设备输出的GPS位置的准确性。(注意:此页面代码为HTML格式

-3

Vue版高德轨迹回放

代码

  1. <template>
  2.      <div>
  3.      <div id=“container”></div>
  4.      <div class=“input-card”>
  5.      <h4>轨迹回放控制</h4>
  6.      <div class=“input-item”>
  7.      <input type=“button” class=“btn” value=“开始动画” id=“start” @click=“startAnimation()” />
  8.      <input type=“button” class=“btn” value=“暂停动画” id=“pause” @click=“pauseAnimation()” />
  9.      </div>
  10.      <div class=“input-item”>
  11.      <input type=“button” class=“btn” value=“继续动画” id=“resume” @click=“resumeAnimation()” />
  12.      <input type=“button” class=“btn” value=“停止动画” id=“stop” @click=“stopAnimation()” />
  13.      </div>
  14.      </div>
  15.      </div>
  16. </template>
  1. <script type=“text/Javascript” src=“https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值”></script>
  2. <script>
  3.      //请求路径
  4.      //import {
  5.          //playbacklist,
  6.      //} from “@/api/obd/playback”;
  7.      export default {
  8.      mounted() {
  9.      this.initMap();
  10.      },
  11.      beforeDestroy() {
  12.      this.map && this.map.destroy();
  13.      },
  14.      data() {
  15.      return {
  16.      map: null,
  17.      marker: null,
  18.      lineArr: [
  19.      [116.478935, 39.997761],
  20.      [116.478939, 39.997825],
  21.      [116.478912, 39.998549],
  22.      [116.478912, 39.998549],
  23.      [116.478998, 39.998555],
  24.      [116.478998, 39.998555],
  25.      [116.479282, 39.99856],
  26.      [116.479658, 39.998528],
  27.      [116.480151, 39.998453],
  28.      [116.480784, 39.998302],
  29.      [116.480784, 39.998302],
  30.      [116.481149, 39.998184],
  31.      [116.481573, 39.997997],
  32.      [116.481863, 39.997846],
  33.      [116.482072, 39.997718],
  34.      [116.482362, 39.997718],
  35.      [116.483633, 39.998935],
  36.      [116.48367, 39.998968],
  37.      [116.484648, 39.999861]
  38.      ]
  39.      };
  40.      },
  41.      methods: {
  42.      initMap() {
  43.      this.map = new AMap.Map(“container”, {
  44.      resizeEnable: true,
  45.      center: [116.397428, 39.90923],
  46.      zoom: 17
  47.      });
  48.      this.marker = new AMap.Marker({
  49.      map: this.map,
  50.      position: [116.478935, 39.997761],
  51.      icon: “https://webapi.amap.com/images/car.png”,
  52.      offset: new AMap.Pixel(-26, 15),
  53.      autoRotation: true,
  54.      angle: 90
  55.      });
  56.      // 绘制轨迹
  57.      let polyline = new AMap.Polyline({
  58.      map: this.map,
  59.      path: this.lineArr,
  60.      showDir: true,
  61.      strokeColor: “#28F”, //线颜色
  62.      // strokeOpacity: 1, //线透明度
  63.      strokeWeight: 6 //线宽
  64.      // strokeStyle: “solid” //线样式
  65.      });
  66.      let passedPolyline = new AMap.Polyline({
  67.      map: this.map,
  68.      // path: this.lineArr,
  69.      strokeColor: “#AF5”, //线颜色
  70.      // strokeOpacity: 1, //线透明度
  71.      strokeWeight: 6 //线宽
  72.      // strokeStyle: “solid” //线样式
  73.      });
  74.      this.marker.on(“moving”, function (e) {
  75.      passedPolyline.setPath(e.passedPath);
  76.      });
  77.      this.map.setFitView();
  78.      },
  79.      startAnimation() {
  80.      this.marker.moveAlong(this.lineArr, 200);
  81.      },
  82.      pauseAnimation() {
  83.      this.marker.pauseMove();
  84.      },
  85.      resumeAnimation() {
  86.      this.marker.resumeMove();
  87.      },
  88.      stopAnimation() {
  89.      this.marker.stopMove();
  90.      }
  91.      }
  92.      };
  93. </script>
  1. <style lang=“less” scoped>
  2.      // @import url(‘https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css’);
  3.      #container {
  4.      height: 1000px;
  5.      width: 100%;
  6.      }
  7.      .inputcard .btn {
  8.      marginright: 1.2rem;
  9.      width: 9rem;
  10.      }
  11.      .inputcard .btn:lastchild {
  12.      marginright: 0;
  13.      }
  14.      .btn {
  15.      display: inlineblock;
  16.      fontweight: 400;
  17.      textalign: center;
  18.      whitespace: nowrap;
  19.      verticalalign: middle;
  20.      webkituserselect: none;
  21.      mozuserselect: none;
  22.      msuserselect: none;
  23.      userselect: none;
  24.      border: 1px solid transparent;
  25.      transition: color .15s easeinout, backgroundcolor .15s easeinout, bordercolor .15s easeinout, boxshadow .15s easeinout;
  26.      backgroundcolor: transparent;
  27.      backgroundimage: none;
  28.      color: #25A5F7;
  29.      bordercolor: #25A5F7;
  30.      padding: .25rem .5rem;
  31.      lineheight: 1.5;
  32.      borderradius: 1rem;
  33.      webkitappearance: button;
  34.      cursor:pointer;
  35.      }
  36.      .inputitem {
  37.      position: relative;
  38.      display: msFlexbox;
  39.      display: flex;
  40.      msflexwrap: wrap;
  41.      flexwrap: wrap;
  42.      msflexalign: center;
  43.      alignitems: center;
  44.      width: 100%;
  45.      height: 3rem;
  46.      }
  47.      .input-card {
  48.      display: flex;
  49.      flexdirection: column;
  50.      minwidth: 0;
  51.      wordwrap: breakword;
  52.      backgroundcolor: #fff;
  53.      backgroundclip: borderbox;
  54.      borderradius: .25rem;
  55.      width: 22rem;
  56.      borderwidth: 0;
  57.      borderradius: 0.4rem;
  58.      boxshadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
  59.      position: fixed;
  60.      bottom: 1rem;
  61.      right: 1rem;
  62.      msflex: 1 1 auto;
  63.      flex: 1 1 auto;
  64.      padding: 0.75rem 1.25rem;
  65.      }
  66. </style>

常见问题

启动时会遇到Module not found: Error: Can’t resolve ‘less-loader’ in ‘文件位置’报错

原因是因为 less 、 less-loader模块未安装,但在中进行使用

解决方法:npm install –save-dev less-loader less

直接安装可能会存在版本太高问题的报错,进行npm run dev时项目无法启动

解决方法:npm install less-loader@5.0.0 -D 可在版本位置选择合适的版本

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

标签

发表评论