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>
相关推荐
木子02041 小时前
前端VUE项目启动方式
前端·javascript·vue.js
endingCode2 小时前
45.坑王驾到第九期:Mac安装typescript后tsc命令无效的问题
javascript·macos·typescript
运维-大白同学2 小时前
将django+vue项目发布部署到服务器
服务器·vue.js·django
星星会笑滴3 小时前
vue+node+Express+xlsx+emements-plus实现导入excel,并且将数据保存到数据库
vue.js·excel·express
前端百草阁4 小时前
【TS简单上手,快速入门教程】————适合零基础
javascript·typescript
彭世瑜4 小时前
ts: TypeScript跳过检查/忽略类型检查
前端·javascript·typescript
FØund4044 小时前
antd form.setFieldsValue问题总结
前端·react.js·typescript·html
Backstroke fish4 小时前
Token刷新机制
前端·javascript·vue.js·typescript·vue
小曲程序4 小时前
vue3 封装request请求
java·前端·typescript·vue
临枫5414 小时前
Nuxt3封装网络请求 useFetch & $fetch
前端·javascript·vue.js·typescript