前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mapboxGL中的航线动画

mapboxGL中的航线动画

作者头像
lzugis
发布2024-05-24 13:15:36
550
发布2024-05-24 13:15:36
举报

概述

借用上篇文章中二阶贝塞尔曲线的生成,本文实现mapboxGL中的航线动画。

效果

实现

1. 初始化地图

代码语言:javascript
复制
const from = [101.797439042302, 36.5937248286007];
const to = [106.9733, 35.217];
const points = new ArcLine(from, to);
const line = new Geometry(points, "LineString")

const style = {
  version: 8,
  sources: {
    XYZTile: {
      type: "raster",
      tiles: [
        "http://webrd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8",
        "http://webrd02.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8",
        "http://webrd03.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8",
        "http://webrd04.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8",
      ],
      tileSize: 256,
    },
    LineVector: {
      type: "geojson",
      data: line,
    },
  },
  layers: [
    {
      id: "XYZTile",
      type: "raster",
      source: "XYZTile",
      minzoom: 0,
      maxzoom: 22,
    },
    {
      id: "LineVector",
      type: "line",
      source: "LineVector",
      paint: {
        "line-color": "#f00",
        "line-width": 3,
      },
    },
  ],
};

const map = new mapboxgl.Map({
  container: "map",
  style,
  attributionControl: false,
  center: [104.74329766269716, 35.80025022526921],
  zoom: 8,
  pitch: 60,
  bearing: getAngle(from, to)
});

2. 添加飞机

代码语言:javascript
复制
map.on('load', () => {
  // 添加动态图标
  map.loadImage('../css/icon.png', function(error, image) {
    if (error) throw error
    map.addImage('flyline-icon', image)
    map.addSource('flyline-point', {
      'type': 'geojson',
      'data': new Geometry(from, 'Point')
    })
    map.addLayer({
      'id': 'flyline-point',
      'source': 'flyline-point',
      'type': 'symbol',
      'layout': {
        'icon-image': 'flyline-icon',
        'icon-size': 0.4,
        'icon-allow-overlap': true,
        'icon-rotation-alignment': 'map',
        'icon-pitch-alignment': 'map',
        'icon-rotate': 0
      }
    })
})

3. 开始动画

代码语言:javascript
复制
const count = 500
const length = turf.length(line)
const interval = length / count
let index = 0
let playFunction = () => {
  if(index > count) {
    clearInterval(playFlag)
  } else {
    const point = turf.along(line, index * interval)
    const pointP = turf.along(line, index * interval * 0.8)
    map.getSource('flyline-point').setData(point)
    const rotate = getAngle(pointP.geometry.coordinates, point.geometry.coordinates)
    map.setLayoutProperty('flyline-point', 'icon-rotate', rotate)
    setView(pointP.geometry.coordinates, rotate + 90)
    index++
  }
}
let playFlag = setInterval(playFunction)
playFunction()

/ / 设置视图
function setView(center, angle) {
  map.setBearing(angle);
  map.setCenter(center);
}

// 计算角度
function getAngle(coords1, coords2) {
  return turf.bearing(
    turf.point(coords1),
    turf.point(coords2)
  ) - 90
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 效果
  • 实现
    • 1. 初始化地图
      • 2. 添加飞机
        • 3. 开始动画
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档