首页 > 其他分享 >vue 高德地图实现进度条轨迹回放

vue 高德地图实现进度条轨迹回放

时间:2024-07-03 14:42:59浏览次数:1  
标签:info vue val 进度条 AMap parseInt new lineArr 高德

<template>
  <div style="position: relative">
    <div style="position: absolute; right: 10px; top: 10px; z-index: 1">
      <el-button @click="silderInput">开始回放</el-button>
      <el-button @click="pauseAnimation">暂停回放</el-button>
      <el-button @click="resumeAnimation">继续回放</el-button>
      <el-slider v-model="sliderVal" :step="1" @input="sliderChange" @change="silderInput" :max="lineArr.length - 1"
        :min="0"></el-slider>
    </div>
    <div style="position: absolute; left: 50px; bottom: 30px;z-index: 1; width: 95%; height: 20px;  "></div>
    <div id="amapcontainer" style="width: 100%; height: 880px"></div>
  </div>
</template>
import AMapLoader from "@amap/amap-jsapi-loader";
window._AMapSecurityConfig = {
  securityJsCode: "密钥",
};

 data() {
    return {
      map: null, // 高德地图实例
      int: null,
      lineArr: [
        [108.478935, 34.997761],
        [108.478934, 34.997825],
        [108.478912, 34.998549],
        [108.478914, 34.998551],
        [108.478914, 34.998551],
        [108.478998, 34.998555],
        [108.479282, 34.99856],
        [108.479658, 34.998528],
        [108.480151, 34.998453],
        [108.480784, 34.998302],
        [108.480788, 34.998305],
        [108.481149, 34.998184],
        [108.481573, 34.997997],
        [108.481863, 34.997846],
        [108.482072, 34.997718],
        [108.482072, 34.997718],
        [108.482072, 34.997718],
        [108.482362, 34.997718],
        [108.483633, 34.998935],
        [108.48367, 34.998968],
        [108.484648, 34.999861],
      ], // 轨迹
      marker: null,
      polyline: null,
      percentage: 50, // 进度条进度
      sliderVal: 0,
      progressTime: 0,
      markerSpeed: 100, // 初始化速度
      afterData: [],//后面的数据
    };
  },
mounted() {
    // 轨迹经纬度去重,否定有问题;
    const uniqueCoordinates = this.lineArr.filter((item, index, self) => {
      return self.findIndex(t => t[0] === item[0] && t[1] === item[1]) === index;
    });
    this.lineArr = uniqueCoordinates;
    // end
    //DOM初始化完成进行地图初始化
    this.initAMap();
  },

methods: {
    // 地图初始化
    initAMap() {
      AMapLoader.load({
        key: "", // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [
          "AMap.Scale",
          "AMap.ToolBar",
          "AMap.ControlBar",
          "AMap.Geocoder",
          "AMap.Marker",
          "AMap.CitySearch",
          "AMap.Geolocation",
          "AMap.AutoComplete",
          "AMap.InfoWindow",
          "AMap.moveAnimation"
        ], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        // 获取到作为地图容器的DOM元素,创建地图实例
        this.map = new AMap.Map("amapcontainer", {
          //设置地图容器id
          resizeEnable: true,
          viewMode: "3D", // 使用3D视图
          zoomEnable: true, // 地图是否可缩放,默认值为true
          dragEnable: true, // 地图是否可通过鼠标拖拽平移,默认为true
          doubleClickZoom: true, // 地图是否可通过双击鼠标放大地图,默认为true
          zoom: 17, //初始化地图级别
          center: [108.347428, 34.90923], // 初始化中心点坐标 北京
          // mapStyle: "amap://styles/darkblue", // 设置颜色底层
        });
        this.marker = new AMap.Marker({
          position: [108.478935, 34.997761],
          icon: "https://a.amap.com/jsapi_demos/static/demo-center-v2/car.png",
          offset: new AMap.Pixel(-13, -26),
        });
        this.map.add(this.marker);

        // 所有路线轨迹(初始轨迹);
        this.polyline = new AMap.Polyline({
          path: this.lineArr,
          showDir: true,
          strokeColor: "#f90", //线颜色
          // strokeOpacity: 1,     //线透明度
          strokeWeight: 6, //线宽
          // strokeStyle: "solid"  //线样式
        });
        this.map.add(this.polyline);
        // 走过的路径
        this.passedPolyline = new AMap.Polyline({
          strokeColor: "#AF5", //线颜色
          strokeWeight: 6, //线宽
        });
        this.map.add(this.passedPolyline);
        this.map.setFitView(); // 根据覆盖物自适应展示地图
      })
        .catch((e) => {
          console.log(e);
        });
    },
    // 松开手触发
    silderInput() {
      let array = [];
      if (this.afterData.length === 0) {
        array = this.lineArr;
      } else {
        array = this.afterData
      }
      var that = this;
      AMap.plugin("AMap.MoveAnimation", () => {
        that.progressTime = that.formatSeconds(0);
        that.markerSpeed = that.markerSpeed * that.speedCount;//markerSpeed的值是100;
        this.marker.moveAlong(array, {
          duration: 200, //可根据实际采集时间间隔设置速度
          // JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
          autoRotation: true,
        });
        this.marker.on("moving", (e) => {
          // lat:34.998553
          // lng:108.478947
          let lngs = e.passedPath[e.passedPath.length - 1].lng; // 经度 当前所在的位置信息; 
          let lats = e.passedPath[e.passedPath.length - 1].lat; // 纬度
          let info = [];
          info.push('<div style="margin-top: -10px;">');
          info.push("经度 :" + lngs);
          info.push("纬度 :" + lats);
          let infoWindow = new AMap.InfoWindow({
            content: info.join("<br/>"),
            offset: new AMap.Pixel(3, -20),
          });
          infoWindow.open(this.map, e.target.getPosition());
          var index = this.lineArr.findIndex(([lng, lat]) => lng === lngs && lat === lats);
          if (index > 0) {
            this.sliderVal = index;
          }
        });
      });
    },
    sliderChange(val) {
      let beforeData = this.lineArr.slice(0, val + 1);//包含val;
      this.afterData = this.lineArr.slice(val + 1);//不包含val;因为不包含所以不能设置setPostion;
      let info = [];
      info.push('<div style="margin-top: -10px;">');
      info.push("经度 :" + this.lineArr[val][0]);
      info.push("纬度 :" + this.lineArr[val][1]);
      let infoWindow = new AMap.InfoWindow({
        content: info.join("<br/>"),
        offset: new AMap.Pixel(3, -20),
      });
      infoWindow.open(this.map, new AMap.LngLat(this.lineArr[val][0], this.lineArr[val][1]));
      if (val > 0) {//解决车闪烁问题;
        this.marker.setPosition(new AMap.LngLat(this.lineArr[val][0], this.lineArr[val][1]));
      }
      if (beforeData.length > 0) {
        this.passedPolyline.setPath(beforeData);//设置走过的路径;
      }
    },
    monitorInterval() {
      this.int = setInterval(() => {
        this.sliderVal += 1 * this.speedCount;
      }, (TIME_VARIABLE / 100) * 100);
    },
    // 暂停回放
    pauseAnimation() {
      clearInterval(this.int);
      this.marker.pauseMove();
    },
    // 继续回放
    resumeAnimation() {
      this.marker.resumeMove();
    },
    formatSeconds(value) {
      var secondTime = parseInt(value); // 秒
      var minuteTime = 0; // 分
      var hourTime = 0; // 小时
      if (secondTime > 60) {
        minuteTime = parseInt(secondTime / 60);
        secondTime = parseInt(secondTime % 60);
        if (minuteTime > 60) {
          hourTime = parseInt(minuteTime / 60);
          minuteTime = parseInt(minuteTime % 60);
        }
      }
      var result =
        parseInt(secondTime) < 10
          ? "0" + parseInt(secondTime)
          : "" + parseInt(secondTime);
      result =
        parseInt(minuteTime) < 10
          ? "0" + parseInt(minuteTime) + ":" + result
          : "" + parseInt(minuteTime) + ":" + result;
      return result;
    },
  },

效果图:

 

标签:info,vue,val,进度条,AMap,parseInt,new,lineArr,高德
From: https://www.cnblogs.com/xx321/p/18281595

相关文章

  • 计算机毕业设计Thinkphp/Laravel高校学生管理系统m4dtg+vue
    Laravel非常的简洁并且是开源的,Laravel是一个具有表现力、优雅语法的Web应用程序框架.Laravel是构建现代全栈Web应用程序的最佳选择.开发语言:php后端框架:Thinkphp/Laravel前端框架:vue.js服务器:apache数据库:mysql运行环境:phpstudy/wamp/xammp等技术路线如下:1.后......
  • WPF进度条中间写百分比数字
    我发现很多同学把思维固话了,通常我们需要实现的进度条是我在网上看到好多例子,但是都没有我的简单,他们不是重写ProcessBar就是使用模板,可以将TextBlock提取出来啊,灵活一点单独绑定然后一句代码Panel.ZIndex="1"就搞定了<StackPanel><ButtonContent="执行耗时......
  • JAVA妇产科专科电子病历系统源码,前端框架:Vue,ElementUI
    JAVA妇产科专科电子病历系统源码,前端框架:Vue,ElementUI孕产妇健康管理信息管理系统是一种将孕产妇健康管理信息进行集中管理和存储的系统。通过建立该系统,有助于提高孕产妇健康管理的效率和质量,减少医疗事故发生的可能性,管理医疗资源,保证孕产妇得到及时、准确的医疗服务。该系......
  • vue项目子组件使用 dialog 弹框,如何实现父子组件弹框展示状态关联?
    vue项目,子组件使用el-dialog组件,想要实现在父组件可以控制子组件dialog的展示和隐藏,子组件自己可以控制dialog展示和隐藏,该如何实现?1.子组件(DialogComponent.vue)子组件接受一个来自父组件的prop,用来控制dialog的显示状态,并且当子组件内部需要改变dialog状态时,通过......
  • Vue canvas绘制圆形进度条动画加载
    父组件代码:<template> <!--创建--> <divclass="resource-warp-box">  <divclass="center-box">   <divclass="used-source">    <pageTitletitle="已用资源"/>    <div......
  • VuePress 的更多配置
    08.更多配置现在,读者应该对VuePress、主题和插件等有了基本的认识,除了插件,VuePress自身也有很多有用的配置,这里简单说明下。‍‍VuePress的介绍在介绍了VuePress的基本使用、主题和插件的概念之后,我们再来看看官网对于VuePress的介绍,就很好理解了:VuePress由两部分......
  • 掉了两根头发后,我悟了!vue3的scoped原来是这样避免样式污染(下)
    前言在上一篇掉了两根头发后,我悟了!vue3的scoped原来是这样避免样式污染(上)文章中我们讲了使用scoped后,vue是如何给CSS选择器添加对应的属性选择器[data-v-x]。这篇文章我们来接着讲使用了scoped后,vue是如何给html增加自定义属性data-v-x。注:本文中使用的vue版本为3.4.19,@vitejs/......
  • vue 监视属性
    <divid="root"><h2>今天天气很{{info}}</h2><button@click="changeWeather">切换天气</button><hr/><h3>a的值是{{numbers.a}}</h3><button@click="add">点我让a+1</bu......
  • 基于SpringBoot+Vue+uniapp的论文管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • vue2项目的打包以及部署
        打包          当我们写好vue2的项目后,可以通过npmbuild来对项目进行打包npmbuild        打包完成后我们可以看到在当面目录下生成了dis目录,src下的文件都会被打包进这个目录里,当然打包后的文件我们不能直接在浏览器打开,需要进行部署......