vue使用高德地图制作小车轨迹回放动画简单案例

请添加图片描述
首先在根目录public中的index.html引入高德地图

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=(你的key)&plugin=AMap.MouseTool,AMap.MapType,AMap.Autocomplete,AMap.PlaceSearch,AMap.MarkerClusterer,AMap.Driving,AMap.MoveAnimation"></script>

<template>
     <!-- 地图 -->
     <div id="container" style="width: 100%;height: 100%"></div>
     <!-- 触发按钮 -->
     <div><a-button type="primary" @click="carSearch">搜索</a-button></div>
     <!-- 轨迹回放控制 -->
     <div class="controlBoxButton">
       <div @click="startAnimation()">开始动画</div>
       <div @click="pauseAnimation()">暂停动画</div>
       <div @click="resumeAnimation()">继续动画</div>
       <div @click="stopAnimation()">停止动画</div>
     </div>
     <!-- 标点弹窗 -->
     <div id="mapInfoWindow">
       <div>{{siteData.name}}</div>
       <div>累计停留时间:{{siteData.time}}</div>
       <div>{{siteData.startDate}}{{siteData.uploadTime}}</div>
     </div>
</template>

data(){
  markerIcon:{
    car: require("@/assets/car.png"),
    label: require("@/assets/label.png"),
  },
},
mounted(){
  this.initMap();
},
methods: {
  //map初始化
  initMap() {
    this.map = new AMap.Map('container', {
      resizeEnable: true,
      zoom: 13,//缩放级别
      center: [119.761878, 29.126898],//地图中心点
    }); 
  },
  //小车线路回放
  carSearch() {
    let that =this;
    //处理接口返回数据格式 (格式为[[经度,纬度],[经度,纬度].......])
    this.lineArr = [];
    res为请求接口获得的线路数据
    res.result.points.map((item)=>{
      this.lineArr.push([item.longitude,item.latitude])
    });
    //设置小车icon
    let car = new AMap.Icon({
      image: that.markerIcon.car,//自定义小车图形
      size: new AMap.Size(140, 50),//图标大小
      imageSize: new AMap.Size(80, 50),//图标所用图片大小
      imageOffset: new AMap.Pixel(0, -10)//图标所用图片偏移量
    });
    //设置marker
    let position = [res.result.points[0].longitude,res.result.points[0].latitude];
    this.marker = new AMap.Marker({
      position: position,//小车开始时位置
      icon: car,
      autoRotation: true,//自动旋转
      angle:-90,//旋转角度
      map:that.map
    });
    // 绘制轨迹
    let polyline = new AMap.Polyline({
      map: that.map,
      path: that.lineArr,
      showDir:true,//显示折线箭头
      strokeColor: "#557aa6", //线颜色
      // strokeOpacity: 1, //线透明度
      strokeWeight: 6,  //线宽
      // strokeStyle: "solid"  //线样式
    });
    let passedPolyline = new AMap.Polyline({
      map: that.map,
      strokeColor: "#AF5",
      // strokeOpacity: 1, //线透明度
      strokeWeight: 6,
      // strokeStyle: "solid"  //线样式      
    });
    this.marker.on('moving', function (e) {
      passedPolyline.setPath(e.passedPath);
      // that.map.setCenter(e.target.getPosition(),true) //设置后地图跟随小车移动
    });
    this.initCarSite();//配置线路中的标点位置
    //调整合适位置
    this.map.setFitView();
  },
  //配置线路中的标点
  initCarSite(){
    let that = this;
    //res为请求接口获得的站点数据
    res.result.slowPoints.map((item)=>{
      let icon = new AMap.Icon({
        image: that.markerIcon.label,
        size: new AMap.Size(80, 80),
        imageSize: new AMap.Size(40, 40),
        imageOffset: new AMap.Pixel(-10, 10)
      });
      let marker = new AMap.Marker({
        icon: icon,
        position: [item.longitude, item.latitude],
      });
      that.map.add(marker);
      //设置信息弹窗
      marker.siteData = item;
      AMap.event.addListener(marker, "click", function () {
        that.openInfoWindow(marker)
      });
    })
  },
  //配置信息窗口
  openInfoWindow(marker) {
    let that = this;
    that.siteData = marker.siteData;
    let content = document.getElementById("mapInfoWindow");
    let infoWindow = new AMap.InfoWindow({
      content: content,
      offset: new AMap.Pixel(0, 0)
    });
    infoWindow.on("close");
    infoWindow.open(that.map, marker.getPosition());
  },
  //控制小车的一些事件
  startAnimation(){
    this.marker.moveAlong(this.lineArr,800);//开始动画(800为速度)
  },
  pauseAnimation(){
    this.marker.pauseMove();//暂停动画
  },
  resumeAnimation(){
    this.marker.resumeMove();//继续动画
  },
  stopAnimation(){
    this.marker.stopMove();//停止动画
  },
}
  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Vue 3和高德地图轨迹回放,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Vue 3和高德地图的相关依赖。你可以使用npm或yarn来安装这些依赖。例如,在你的Vue项目中,可以运行以下命令来安装高德地图的依赖: ``` npm install vue-amap ``` 2. 在你的Vue组件中,引入Vue AMap库并注册该组件: ```javascript import VueAMap from 'vue-amap'; export default { ... mounted() { Vue.use(VueAMap); VueAMap.initAMapApiLoader({ key: 'your_amap_key', plugin: ['AMap.Polyline'] }); }, ... } ``` 在上述代码中,你需要将`your_amap_key`替换成你自己的高德地图API密钥。 3. 在模板中添加地图容器和控件: ```html <template> <div> <amap :zoom="13" :center="[lng, lat]"> <amap-polyline :path="path" :visible="true" :style="{ strokeColor: 'red', strokeWeight: 6 }"></amap-polyline> </amap> </div> </template> ``` 在上述代码中,`amap`是地图容器组件,`amap-polyline`是轨迹回放的折线组件。你可以根据需要调整地图的缩放级别和中心点位置,以及折线的样式。 4. 在组件的`data`属性中定义轨迹回放的经纬度数据: ```javascript data() { return { lng: 116.397428, lat: 39.90923, path: [ [116.405289, 39.904987], [116.406089, 39.904987], [116.406289, 39.905087], // 更多经纬度数据... ] }; } ``` 你需要根据实际情况提供正确的经纬度数据。 5. 最后,你可以根据需求实现轨迹回放的逻辑。例如,你可以使用定时器来逐步显示折线上的点,实现轨迹的动态回放效果。 这样,你就可以在Vue 3中使用高德地图实现轨迹回放了。记得根据你的实际需求进行相应的调整和扩展。希望这能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值