mapbox点位动态连线

javascript 复制代码
<template>
	<div class="trajectory">
		<div id="container" class="trajectory-container"></div>
	</div>
</template>

<script lang="ts" setup>
import { onMounted } from 'vue';
import mapboxgl from 'mapbox-gl';
import imgPoint from '../../../../assets/images/map/point12.png';
import * as turf from '@turf/turf';
const publick = `${import.meta.env.BASE_URL}`;
const coordinates = [
	[104.0668, 30.5728],
	[104.0709, 30.5738],
	[104.067, 30.5748],
	[104.0691, 30.5758],
	[104.0651, 30.5768],
	[104.0691, 30.5798],
	[104.0701, 30.5818],
	[104.0671, 30.5838],
	[104.0671, 30.5808],
];
const pointsGeoJSON = {
	type: 'FeatureCollection',
	features: coordinates.map((coord) => ({
		type: 'Feature',
		geometry: {
			type: 'Point',
			coordinates: coord,
		},
		properties: {}, // 可以在此处添加任何需要的属性
	})),
};
let pointIndex = 0;
let index = 0;
const init = () => {
	mapboxgl.accessToken = null;
	class Cjmapbox extends mapboxgl.Map {}
	// eslint-disable-next-line @typescript-eslint/no-empty-function
	Cjmapbox.prototype.__proto__._authenticate = function () {};
	const map = new mapboxgl.Map({
		container: 'container',
		style: {
			version: 8,
			name: 'BlankMap',
			glyphs: `${publick}lib/glyphs/{fontstack}/{range}.pbf`,
			sources: {},
			layers: [
				{
					id: 'background',
					type: 'background',
					paint: {
						'background-color': '#08294A',
					} /* 背景颜色 */,
				},
			],
		},
		center: [104.0668, 30.5728], // Set the initial center of the map
		zoom: 14,
	});
	map.on('load', () => {
		map.addSource('DZDT_Vector_BZB', {
			type: 'raster',
			tiles: [
				'http://t3.tianditu.com/DataServer?T=vec_w&tk=915de993ea6873664830bf5d8217723c&x={x}&y={y}&l={z}',
			],
			tileSize: 256,
		});
		map.addLayer({
			id: 'DZDT_Vector_BZB',
			type: 'raster',
			source: 'DZDT_Vector_BZB',
			paint: {},
		});
		map.addSource('point', {
			type: 'geojson',
			data: pointsGeoJSON,
		});

		// 添加点图层
		map.addLayer({
			id: 'pointLayer',
			type: 'circle',
			source: 'point',
			paint: {
				'circle-radius': 8,
				'circle-color': '#B42222',
			},
		});
		map.addSource('line', {
			type: 'geojson',
			data: {
				type: 'FeatureCollection',
				features: [],
			},
		});

		// 添加线条图层
		map.addLayer({
			id: 'lineLayer',
			type: 'line',
			source: 'line',
			layout: {
				'line-join': 'round',
				'line-cap': 'round',
			},
			paint: {
				'line-color': '#4682B4',
				'line-width': 3,
			},
		});
		animateLine();
		function animateLine() {
			if (index < coordinates.length) {
				// 更新线条数据
				const currentLine = {
					type: 'Feature',
					geometry: {
						type: 'LineString',
						coordinates: coordinates.slice(0, index + 1),
					},
				};

				// 更新点数据
				// const currentPoint = {
				// 	type: 'Feature',
				// 	geometry: {
				// 		type: 'Point',
				// 		coordinates: coordinates[index],
				// 	},
				// };

				// 更新地图上的数据源
				map.getSource('line').setData(currentLine);
				// map.getSource('point').setData({
				// 	type: 'FeatureCollection',
				// 	features: [currentPoint],
				// });

				index++;
				setTimeout(animateLine, 100); // 设置延迟1秒
			}
		}
		setInterval(() => {
			if (map.getLayer('lineLayer')) {
				map.removeLayer('lineLayer');
				map.removeSource('line');
			}

			map.addSource('line', {
				type: 'geojson',
				data: {
					type: 'FeatureCollection',
					features: [],
				},
			});

			// 添加线条图层
			map.addLayer({
				id: 'lineLayer',
				type: 'line',
				source: 'line',
				layout: {
					'line-join': 'round',
					'line-cap': 'round',
				},
				paint: {
					'line-color': '#4682B4',
					'line-width': 3,
				},
			});
			index = 0;
			// 开始动画绘制轨迹
			animateLine();
		}, 2000);
	});
};
onMounted(() => {
	init();
});
</script>
<style lang="scss" scoped>
.trajectory {
	width: 100%;
	height: 100%;
	position: relative;
	&-container {
		width: 100%;
		height: 100%;
	}
}
</style>
相关推荐
英俊潇洒美少年5 小时前
Vue 生产环境打包:SourceMap、压缩、混淆、加密全解 + 最佳实践
前端·javascript·vue.js
她说人狗殊途8 小时前
基于 vue-cli 创建
前端·javascript·vue.js
一 乐11 小时前
疫苗发布和接种预约|基于Java+vue疫苗发布和接种预约系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·疫苗发布和接种预约系统系统
喵个咪12 小时前
统一范式:中后台Admin项目标准化API分层开发方案(Vue/React通用)
前端·vue.js·react.js
UaoN12 小时前
Vibe Coding 时代,为什么 Tailwind + Shadcn/ui 正在成为现代前端的默认答案
react.js·typescript
喵个咪12 小时前
拒绝过度封装!GoWind Admin:基于Element Plus重塑中后台CRUD开发范式
前端·vue.js·typescript
angerdream13 小时前
Android手把手编写儿童手机远程监控App之WebRtc切换摄像头与桌面共享
vue.js
喵个咪15 小时前
吃透后台权限系统:从架构设计到 Vue3/React 双框架完整落地
前端·vue.js·react.js
hdsoft_huge15 小时前
全开源数字孪生系统搭建方案:全套技术文档
vue.js·开源·node.js·echarts·webstorm
学且思15 小时前
Vue3 Patch 算法深度解析:从原理到源码实现
前端·vue.js