【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');
};
相关推荐
前端伪大叔5 分钟前
freqtrade智能挂单策略,让你的资金利用率提升 50%+
前端·javascript·后端
江城开朗的豌豆6 分钟前
从“any”战士到类型高手:我的TypeScript进阶心得
前端·javascript·前端框架
红尘散仙26 分钟前
TRNovel王者归来:让小说阅读"声"临其境的终端神器
前端·rust·ui kit
知花实央l31 分钟前
【Web应用安全】SQLmap实战DVWA SQL注入(从环境搭建到爆库,完整步骤+命令解读)
前端·经验分享·sql·学习·安全·1024程序员节
烛阴34 分钟前
为你的Lua代码穿上盔甲:精通错误处理的艺术
前端·lua
专注前端30年1 小时前
Vue CLI与Webpack:区别解析与实战使用指南
前端·vue.js·webpack
余道各努力,千里自同风1 小时前
如何使用 Promise.all() 处理异步并发操作?
开发语言·前端·javascript
营赢盈英1 小时前
How to detect if <html> tag has a class in child Angular component
前端·javascript·css·html·angular.js
Achieve - 前端实验室1 小时前
深入浅出 ES Module
前端·javascript
littleplayer1 小时前
Combine 基本使用指南
前端