Vue中使用高德地图实现多人实时轨迹

Vue中使用高德地图实现多人实时轨迹

思路: 多人实时的思路和单人实时的思路差不多,都是通过不断的向后台请求,进行点标记添加与清除。

首先

引入与配置高德地图可参考本人之前写得博客:https://blog.csdn.net/jinglianglove/article/details/102483714

接下来

直接看代码把 关键就是 this.markers.push(this.marker);

data中的数据


data() {
    return {
      h3tit: "人员轨迹",
      timer: null,
      manyTime:null,
      markerSpeed: 200,
      speedCount: 1,
      pName:'---',
      lineArr: [
        [116.478935, 39.997761],
        [116.478939, 39.997825],
        [116.478912, 39.998549],
        [116.478912, 39.998549],
        [116.478998, 39.998555],
        [116.478998, 39.998555],
        [116.479282, 39.99856],
        [116.479658, 39.998528],
        [116.480151, 39.998453],
        [116.480784, 39.998302],
        [116.480784, 39.998302],
        [116.481149, 39.998184],
        [116.481573, 39.997997],
        [116.481863, 39.997846],
        [116.482072, 39.997718],
        [116.482362, 39.997718],
        [116.483633, 39.998935],
        [116.48367, 39.998968],
        [116.484648, 39.999861]
      ],
      marker: {},
      map: {},
      markers:[],//点的集合  用来清除点标记
      firstArr: [120.11296645567579, 30.290059843858472],
      polyline: {},
      passedPolyline: {},
      flag: false, //轨迹回放操作按钮的出现
      circle: {}, //圈圈
      circle1: {},
      total: 1,
      currentPage: 1,
      pageSize: 10,
      ptableData: [], //列表
      // ptableDataItem: [],
      loading: false,
      card_no: null //卡号用来区分是谁得轨迹运动
    };
  },
      

methods里面方法(关键)

//多人实时封装
    manyPersonWays(){
      if (this.manyTime) {
        clearInterval(this.manyTime);
        this.manyTime = null;
      }
      this.manyTime =setInterval(() => {
        this.removemarker()
        this.manyPersonData()
      }, 2000);
    },
//多人实时数据接口
    manyPersonData(){
      getPersonPosition([("appId"), {
          page: this.currentPage,
          rows: this.pageSize}]).then(res => {
        if(res.statusCode==200){
          if(res.result.length==1){
            return false;
          }else{
             res.result.forEach(item=>{
             this.addPositiion(item);
            })
          }
        }
      });
    },
//添加点标记
    addPositiion(data) {
      this.marker = new AMap.Marker({
        map: this.map,
        position: [Number(data.lng), Number(data.lat)],
        icon: pic,
        offset: new AMap.Pixel(0, 0),
      });
      this.marker.setTitle(`${data.person_name}`);
      this.marker.setLabel({
        offset: new AMap.Pixel(50, 0), //设置文本标注偏移量
        content: `<div class='info'>${data.person_name}</div>`, //设置文本标注内容
        direction: "top" //设置文本标注方位
      });
      this.markers.push(this.marker);
      this.map.setFitView()
    },
   
    //移出点标记 
    removemarker() {
      this.map.remove(this.markers);//清除所有点标记
      this.markers = [];
    },

###最后在切换页面的时候清除定时器

  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
实现多辆小车在高德地图上移动,可以使用 Vue.js 结合高德地图 JavaScript API,为每辆小车创建一个独立的标记,并对每个标记进行移动操作。 以下是一个示例代码: ```html <template> <div> <div id="map" style="width: 800px; height: 600px;"></div> </div> </template> <script> export default { data() { return { map: null, markers: [ { id: 1, lnglat: [116.39, 39.9] }, { id: 2, lnglat: [116.4, 39.91] }, { id: 3, lnglat: [116.41, 39.92] }, // ... ] }; }, mounted() { // 创建地图 this.map = new AMap.Map('map', { zoom: 15, center: [116.39, 39.9] }); // 创建小车标记 this.markers.forEach(markerData => { const marker = new AMap.Marker({ map: this.map, position: markerData.lnglat, icon: new AMap.Icon({ size: new AMap.Size(50, 50), image: 'https://webapi.amap.com/images/car.png', imageSize: new AMap.Size(50, 50) }) }); // 设置小车标记的角度 function setMarkerAngle(angle) { marker.setAngle(angle); } // 设置小车标记的位置 function setMarkerPosition(position) { marker.setPosition(position); } // 获取小车标记的角度 function getAngle(position) { // 根据当前位置和下一个位置计算角度 // ... } // 小车移动的动画 function move() { let index = 0; setInterval(() => { const currentPosition = markerData.lnglat; const nextPosition = this.markers[index].lnglat; const angle = getAngle(currentPosition, nextPosition); setMarkerAngle(angle); setMarkerPosition(nextPosition); index++; if (index >= this.markers.length) { index = 0; } }, 1000); } // 开始小车移动 move(); }); } } </script> ``` 在上述代码,通过遍历 `markers` 数组为每辆小车创建一个独立的标记,并在 `mounted` 钩子函数循环地对每个标记进行移动操作。你可以根据实际需求修改标记的初始位置、路径、移动速度等参数。记得在 `mounted` 钩子函数调用相关方法来初始化地图和小车移动。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值