【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');
};
相关推荐
行云流水6262 小时前
uniapp pinia实现数据持久化插件
前端·javascript·uni-app
zhangyao9403302 小时前
uniapp动态修改 顶部导航栏标题和右侧按钮权限显示隐藏
前端·javascript·uni-app
福尔摩斯张4 小时前
Axios源码深度解析:前端请求库设计精髓
c语言·开发语言·前端·数据结构·游戏·排序算法
李牧九丶5 小时前
从零学算法1334
前端·算法
周周爱喝粥呀5 小时前
UI设计原则和Nielsen 的 10 条可用性原则
前端·ui
小云朵爱编程5 小时前
Vue项目Iconify的使用以及自定义图标,封装图标选择器
前端·javascript·vue.js
前端大卫6 小时前
CSS 属性值 initial、unset 和 revert 的解析
前端
shimh_凉茶6 小时前
webpack+vue2打包分析视图插件 webpack-bundle-analyzer
前端·webpack·node.js
P***25396 小时前
JavaScript部署
开发语言·前端·javascript
一只小阿乐6 小时前
react 状态管理mobx中的行为模式
前端·javascript·react.js·mobx·vue开发·react开发