【MapBox】实现实时飞行轨迹功能

之前写了一篇MapBox添加带箭头的轨迹线,现在在这个基础之上实现获取到无人机的推送点位数据实时飞行的功能

  1. 首先创建实例,将无人机的图标加载在地图上
javascript 复制代码
const MAP_UAV_FLIGHT_ING = (values, layerKey = '无人机飞行') => {
  ClearUAVMap();

  const map = GET_MAPBOX_MAP();

  map.loadImage(imageIcon.value, function (error, img) {
    if (error) throw error;

    map.addImage('uavIcon', img);
  });

  const allPoints = [];

  allPoints.push(
    turf.point([values?.droneLongitude, values?.droneLatitude], {
      ...values,
      layerKey,
      bearing: values.yaw,
    }),
  );

  // 特征集合
  animatePointGeoJson = turf.featureCollection(allPoints);

  map.addLayer({
    id: 'animatePointLayer',
    type: 'symbol',
    source: {
      type: 'geojson',
      data: animatePointGeoJson,
    },
    layout: {
      'icon-image': 'uavIcon',
      'icon-size': 0.5,
      'icon-rotate': ['get', 'bearing'],
      'icon-rotation-alignment': 'map',
      'icon-allow-overlap': true,
      'icon-ignore-placement': true,
    },
  });
  // counter = counter + 1;
};

备注:

  1. ClearUAVMap()方法是为了清除图层,否则再展示其他无人机轨迹的时候控制台会提示地图上已存在这个图层
javascript 复制代码
// 清除轨迹
export const ClearUAVMap = () => {
  if (map?.hasImage('uavIcon')) map?.removeImage('uavIcon');

  if (map.getLayer('animatePointLayer')) map.removeLayer('animatePointLayer');
  if (map.getSource('animatePointLayer')) map.removeSource('animatePointLayer');

  if (map.getLayer('homePointLayer')) map.removeLayer('homePointLayer');
  if (map.getSource('homePointLayer')) map.removeSource('homePointLayer');
};
  1. 'icon-rotate': 'get', 'bearing',这个数据是添加数据源中指定的属性,bearing表示无人机的方向,这样无人机就会按照所提供的方向进行飞行

  2. 这个时候获取到实时推送的数据只需要获取到该图层的图层源改数据就可以了,不用清除再重新加载

javascript 复制代码
const MAP_ANIMATE_ING = (values, layerKey = '无人机飞行') => {
  const map = GET_MAPBOX_MAP();

  animatePointGeoJson.features[0].geometry.coordinates = [values?.droneLongitude, values?.droneLatitude];
  animatePointGeoJson.features[0].properties = {
    ...values,
    layerKey,
    bearing: values.yaw, //方向
  };

  map.getSource('animatePointLayer').setData(animatePointGeoJson);

  map.moveLayer('animatePointLayer', 'arrowLayer', 'routeLayer');
};
相关推荐
A小码哥14 分钟前
大模型成本怎么降,5 个动作先做前两个
java·前端·spring
PHP是世界上最好的语言26 分钟前
ComPDF预览本地正常,部署后白屏:检查资源、路径和许可
前端
kiros_wang27 分钟前
项目分包方案:模块拆分、跨模块组件与方法引用规范
前端
PedroQue9929 分钟前
unix-utils v0.2.0 发布:modal 全端统一,自绘窗口解决输入框难题
前端·uni-app
Csvn30 分钟前
网络优化
前端
代码猎人33 分钟前
前端怎么跟进生态新技术
前端
海阳皓辰网络科技有限公司34 分钟前
Vue3 冷库货位选择器:三级联动、扫码带出与批次混放校验
前端·vue.js
__sjfzllv___36 分钟前
在职前端Leader学习/转行 AI Agent -DAY81
前端
女生也可以敲代码37 分钟前
div上有红色下划线报错:JSX 元素隐式具有类型 “any“,因为不存在接口 “JSX.IntrinsicElements“。ts(7026)
前端
申君健248641827720240 分钟前
ArkTS 速成指南(写给有 TypeScript / 前端经验的开发者)
前端