mapboxGL轨迹展示与播放

news/2024/11/24 2:20:17/

概述

历史轨迹回放是GIS很常见的一个功能,本文结合turf.js实现轨迹的展示与播放动画。

效果

福昕截屏20211220220418936.PNG

实现功能

  • 轨迹的展示;
  • 轨迹的方向的箭头展示;
  • 随着轨迹播放的小车,并调整方向与轨迹方向一致;
  • 已播放路径的展示;
  • 多条轨迹线的同时播放展示;

实现

const icon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAWCAMAAAC13D+jAAADAFBMVEWs1eL/0T7+zTv+lwD81FfXqSZNNA/84nXSixXJt5kjS2z/uCn8rBVxz+yn09/mtymmvMavxrX9sxn/zTLnxmrJ9v/+uzNarMOHqsb1vGb9ynuDwdK7ycn7t0r85WpMpLuHrLn/4kDFmiRUaYL9xDT/0THlgAH01XH9rSz/1ULHqWP723N3ucv+2D/7wWeyoVTbs1S02uWP1OzIp1OidDBNjKe46vyYtKjit2n/zSzmmzncvWlzTAf/3UFrtMgwg6+4rJb+ogGtkVr/swiSxtr/5j76xG7VsVOQrKxXXFnx0Wimwuola5T84Fztx5fglxw5PDPftVBXu9r9v0f/rgmWn5a3l1adbhP/0Tmftcbltljqu4WDdl3w6eGs5fmepJj2qSupiD2wvLN7pbXTtF//98f4pC321m+rwsT63Wv9+vZXam/Cq4zFsIz/2zVxe3X+xC1rwtqgdhvomjDtwXr/qwL/0mS/pGH31GPtvHn/0y3m07vByZXUqVf/0y+smGv/80X900r/1D293OY1JgrnqVnx49LM5Ov5uFT/0Cr0oi/69vP9/PpkWCxrmbr/1znz1XjTrk7YtVt1udPdul+64u6bn5D/5i4aJSj/z3i4hSfm/v/322ry///vnir/0CzbjSTVrWSw2d328ej933r2v2j/1C//0C/kvjC/kkGukkCBmJeu2OK+spbZ6vDT/f+hutbCfgrz3HXxrTPFlkvtkATzhwDnp03RzLvvs0d3kZ6SsKj/yh6MsrmRqbD/2Y7zqSL/5n3/ryb/1CvlwmOOu8D5wlpoodNqvtz/+VCoiE1ZveCg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iqjAwECvI+4GIDBRZMKkKVOmWPP3/tE5EXQ6iPdT0UldX7m7cnerbjZu62ZYzPz6jBOP0mpzc/OAfMHdICD4u0UwCQgyLijnZ1/5Z2r/bsUbzSbVjwU3hFJlHwOzWDVzqbOQyer4+GlsMQ+mAcH9tTFsD9c6AAFnrtPnD2wcReEF3Nzchw5p8Ox/Gg3OyLGVO2VlNTQ00sTEEjRAIIE7+MCBBDBQ2azvLsZU1tHhBgJLH7dB8j4C9GCydsgtRi8uAEXmog5wB7NZAAAAAElFTkSuQmCC'
const arrow = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAnElEQVQ4T63TsQ0CMQyF4f/NgMQQ0CBR0FIx190cFIiWhhFoKdgEiRUeSoF0gO8cjkub+Evs2OLPpc9422dgBRwlNZn/BtjeApdOUJsh0QuuwKYWiYAFcAKWHaSR1EbpfAHlkO0ICdMJgV+QXmAAmUu6v9IZA8wkPVKgtg7TF7H25t4UbN+A9ahGmqqVD8AO2GdzUF45+I3ZJJb9JxbwRhEhB66xAAAAAElFTkSuQmCC'class AnimationRoute {constructor(map, json, play = true) {this._map = mapthis._json = jsonthis._play = playthis.init()}init() {const that = thisthat._index = 0that._count = 1500that._step = turf.length(that._json) / that._countthat._flag = 0that._playId = 'play-' + Date.now()// 添加路径图层that._map.addSource(that._playId, {type: 'geojson',data: that._json})that._map.addLayer({id: that._playId,type: 'line',source: that._playId,'layout': {'line-cap': 'round','line-join': 'round'},'paint': {'line-color': '#aaaaaa','line-width': 10}})// 添加已播放路径that._map.addSource(that._playId + '-played', {type: 'geojson',data: that._json})that._map.addLayer({id: that._playId + '-played',type: 'line',source: that._playId + '-played','layout': {'line-cap': 'round','line-join': 'round'},'paint': {'line-color': '#09801a','line-width': 10}})// 添加路径上的箭头that._map.loadImage(arrow, function(error, image) {if (error) throw errorthat._map.addImage(that._playId + '-arrow', image)that._map.addLayer({'id': that._playId + '-arrow','source': that._playId,'type': 'symbol','layout': {'symbol-placement': 'line','symbol-spacing': 50,'icon-image': that._playId + '-arrow','icon-size': 0.6,'icon-allow-overlap': true}})})// 添加动态图标that._map.loadImage(icon, function(error, image) {if (error) throw errorthat._map.addImage(that._playId + '-icon', image)that._map.addSource(that._playId + '-point', {'type': 'geojson','data': that._getDataByCoords()})that._map.addLayer({'id': that._playId + '-point','source': that._playId + '-point','type': 'symbol','layout': {'icon-image': that._playId + '-icon','icon-size': 0.75,'icon-allow-overlap': true,'icon-rotation-alignment': 'map','icon-pitch-alignment': 'map','icon-rotate': 50}})that._animatePath()})}_animatePath() {if(this._index > this._count) {window.cancelAnimationFrame(this._flag)} else {const coords = turf.along(this._json, this._step * this._index).geometry.coordinates// 已播放的线const start = turf.along(this._json, 0).geometry.coordinatesthis._map.getSource(this._playId + '-played').setData(turf.lineSlice(start, coords, this._json))// 车的图标位置this._map.getSource(this._playId + '-point').setData(this._getDataByCoords(coords))// 计算旋转角度const nextIndex = this._index === this._count ? this._count - 1 : this._index + 1const coordsNext = turf.along(this._json, this._step * nextIndex).geometry.coordinateslet angle = turf.bearing(turf.point(coords),turf.point(coordsNext)) - 90if(this._index === this._count) angle += 180this._map.setLayoutProperty(this._playId + '-point', 'icon-rotate', angle)this._index++if(this._play) this._flag = requestAnimationFrame(() => {this._animatePath()})}}_getDataByCoords(coords) {if(!coords || coords.length !== 2) return nullreturn turf.point(coords, {'label': this._formatDistance(this._step * this._index)})}_formatDistance(dis) {if(dis < 1) {dis = dis * 1000return dis.toFixed(0) + '米'} else {return dis.toFixed(2) + '千米'}}destory() {window.cancelAnimationFrame(this._flag)if(this._map.getSource(this._playId + '-point')) {this._map.removeLayer(this._playId + '-point')// this._map.removeLayer(this._playId + '-label')this._map.removeSource(this._playId + '-point')}if(this._map.getSource(this._playId)) {this._map.removeLayer(this._playId)this._map.removeSource(this._playId)}}
}

测试调用代码:

const route1 = {'type':'Feature','properties':{},'geometry':{'type':'LineString','coordinates':[[106.669,22.5785],[106.6374,22.5974],[106.6206,22.608],[106.6037,22.5553],[106.5784,22.4858],[106.5595,22.4373],[106.5637,22.3804],[106.5827,22.3298],[106.6543,22.313],[106.6859,22.2561],[106.7006,22.195],[106.688,22.1613],[106.6943,22.0897],[106.6964,22.018],[106.6838,21.9717],[106.7386,21.9864],[106.7554,22.0138],[106.8334,21.9759],[106.9008,21.9738],[106.9261,21.9422],[106.9767,21.9316],[107.0209,21.9485],[107.0609,21.919],[107.0125,21.8705],[107.0104,21.8305],[107.0609,21.8031],[107.1031,21.7862],[107.1473,21.7483],[107.2063,21.7125],[107.2611,21.6935],[107.2927,21.7251]]}}
new AnimationRoute(map, route1)

说明:如果为多个轨迹同时展示,多次调用new AnimationRoute即可。


http://www.ppmy.cn/news/168625.html

相关文章

app闪退之-- GL error: Out of memory!

客户应用运行一段时间后闪退: 日志 2021-09-16 10:35:16.194 2328-2328/? I/Choreographer: Skipped 55 frames! The application may be doing too much work on its main thread. 2021-09-16 10:35:16.207 223-394/? E/BufferQueueProducer: [Toast] dequeueBuffer: Buf…

OpenGL 太阳系行星拾取例子(GL_SELECT) VS2008 + glut实现

太阳系&#xff1a;Solar System 以太阳&#xff08;Sun&#xff09;为中心&#xff0c;由内到外分别是&#xff1a; 水星&#xff08;Mercury&#xff09; 金星&#xff08;Venus&#xff09; 地球&#xff08;Earth&#xff09; 火星&#xff08;Mars&#xff09; 木星&#…

android 内存泄露-抓出重要函数-GL_OUT_OF_MEMORY-GL error: Out of memory!OpenGLRenderer

一般log有错误的内存泄露提示“GL error: Out of memory!”"GL_OUT_OF_MEMORY",我们就需要使用工具去一步一步的获取哪些模块类里面的方法出了问题,然后一个一个去尝试找出问题,以下是个人经历: 问题点:蓝牙传输多个文件,引发蓝牙报停,log打印crash: OpenGL…

echarts+echarts-gl vue2制作3D地图+下钻功能+标记点功能,解决dblclick事件失效问题,解决地图下钻后边框不更新保留问题

目录 先看实现效果&#xff1a;​编辑 步骤一 安装echarts和echarts-gl 步骤二 设置地图容器 在methods中设置初始化地图方法并在mounted中调用 在methods中设置初始化地图方法 在mounted中调用 打开页面效果&#xff1a;​编辑 步骤三 1、给地图添加双击事件dblcli…

华硕FX63VD/FX503VD键盘按键失灵

键盘失灵&#xff0c;开机键能用&#xff0c;有背光&#xff0c;大小写等指示灯切换正常且按键失灵 点击运行&#xff0c;等两分钟重刷程序给键盘芯片就好了 https://www.52pojie.cn/thread-1753087-1-1.html https://download.csdn.net/download/weixin_39444707/87525497

调试STM32+EMMC+GL3227E(固件1857,1858,1859)遇到的问题

声明&#xff1a; 记录下我自己的调试过程&#xff0c;一是给自己看&#xff0c;记录点滴&#xff1b;二也是分享给大家&#xff0c;在碰到类似的问题时&#xff0c;提供些许的思路。 问题描述&#xff1a; 使用如题的结构&#xff0c;STM32将数据存储在EMMC上&#xff0c;电脑…

光照的个人推导过程与GL实现

目录 1、前提知识 1.1、GL的绘图过程&#xff1a; 1.2、点积的规则和作用&#xff1a; 1.3、normalize在方向处理上的作用 2、光照控制的理论基础 2.1、自由的实现&#xff1a; 2.2、带有方向性的光——基于dot product的实现 最终效果演示如下&#xff1a; 3、关键代…

GLSL #define GL_SPIRV 100说明

GLSL #define GL_SPIRV 100说明 版权 hankern https://blog.csdn.net/hankern/article/details/90690297 Standard, Portable Intermediate Representation - V (SPIR-V) OpenGL 4.6的最大变化就是 支持SPIR-V&#xff0c;一种用于GPU通用计算和图形学的中间语言&#xff0c;…