mapbox点位动态连线

<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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小满blue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值