mapbox动态路径

mapbox动态路径

class AddRunningRoute{
	declare map: any
	declare geojsonData: any
	declare sourceId: string
	declare lineRunningBackground: string
	declare lineRunningDashed: string
	declare timer: any
	
	declare isAnimation: boolean
	
	declare dashArraySequence: Array<any>
	
	declare step: number
	
	declare lineColor: string
	
	declare lineWidth: number
	
	constructor(option: {
		map:any,
		isAnimation: boolean,
		geojsonData: any,
		lineColor?: string,
		lineWidth?: number
	}) {
		
		this.map = option.map
		this.geojsonData = option.geojsonData
		
		this.isAnimation = option.isAnimation
		
		this.lineColor = option?.lineColor || 'rgba(0,255,255,1)'
		this.lineWidth = option?.lineWidth || 6
		
		this.sourceId = 'line-running' + Date.now()
		this.lineRunningBackground = 'line-running-background' + Date.now()
		this.lineRunningDashed = 'layer-running-dashed' + Date.now()
		
		this.step = 10
		
		this.dashArraySequence = [
			[0, 4, 3],
			[0.5, 4, 2.5],
			[1, 4, 2],
			[1.5, 4, 1.5],
			[2, 4, 1],
			[2.5, 4, 0.5],
			[3, 4, 0],
			[0, 0.5, 3, 3.5],
			[0, 1, 3, 3],
			[0, 1.5, 3, 2.5],
			[0, 2, 3, 2],
			[0, 2.5, 3, 1.5],
			[0, 3, 3, 1],
			[0, 3.5, 3, 0.5]
		]
		
		this.init()
	}
	
	private init(){
		this.map.addSource(this.sourceId, {
			type: 'geojson',
			data: this.geojsonData
		})
		
		this.map.addLayer({
			type: 'line',
			source: this.sourceId,
			id: this.lineRunningBackground,
			paint: {
				'line-color': this.lineColor,
				'line-width': this.lineWidth,
				'line-opacity': 1
			}
		})
		
		
		if(this.isAnimation){
			this.map.addLayer({
				type: 'line',
				source: this.sourceId,
				id: this.lineRunningDashed,
				paint: {
					'line-color': '#fff',
					'line-width': this.lineWidth,
					'line-dasharray': [0, 4, 3]
				}
			})
			
			this.animate()
		}
		
	}
	
	private animate(){
		const animateDashArray = (timestamp: number)=> {
			
			const newStep = parseInt(
				String((timestamp / 50) % this.dashArraySequence.length), 10
			)
			
			if (newStep !== this.step) {
				this.map.setPaintProperty(
					this.lineRunningDashed,
					'line-dasharray',
					this.dashArraySequence[this.step]
				)
				this.step = newStep
			}
			
			this.timer = requestAnimationFrame(animateDashArray)
		}
		animateDashArray(0)
	}
	
	on(callback){
		if(this.isAnimation){
			this.map.on('click',this.lineRunningDashed,(e)=>{
				callback(e)
				
				const coordinates = e.features[0].geometry.coordinates
				
				this.map.panTo(coordinates)
				
			})
		}else {
			this.map.on('click',this.lineRunningBackground,(e)=>{
				callback(e)
				
				const coordinates = e.features[0].geometry.coordinates
				
				this.map.panTo(coordinates)
				
			})
		}
		
	}
	
	destroy(){
		if(this.timer){
			cancelAnimationFrame(this.timer)
		}
		if(this.map?.getSource(this.sourceId)){
			this.map.removeLayer(this.lineRunningBackground)
			if(this.isAnimation){
				this.map.removeLayer(this.lineRunningDashed)
			}
			this.map.removeSource(this.sourceId)
		}
	}
}

export default AddRunningRoute

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mapbox提供了一个叫做Mapbox Directions的服务,可以用来进行路径规划。你可以通过该服务的API来进行自定义导航路径规划。 以下是一个简单的示例代码,展示如何在Mapbox地图上自定义路径规划: ``` // 初始化地图 mapboxgl.accessToken = 'YOUR_ACCESS_TOKEN'; var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v11', center: [-77.04, 38.907], zoom: 11 }); // 添加导航控件 var nav = new mapboxgl.NavigationControl(); map.addControl(nav, 'top-left'); // 自定义路径规划 var url = 'https://api.mapbox.com/directions/v5/mapbox/driving/'; var origin = [-77.036, 38.897]; var destination = [-77.009, 38.889]; var waypoints = [ [-77.054, 38.889], [-77.049, 38.891], [-77.045, 38.888] ]; var accessToken = mapboxgl.accessToken; var xhr = new XMLHttpRequest(); xhr.open('GET', url + origin.join(',') + ';' + waypoints.map(function(point) { return point.join(','); }).join(';') + ';' + destination.join(',') + '?access_token=' + accessToken, true); xhr.onload = function() { if (xhr.status === 200) { // 在地图上绘制路径 var data = JSON.parse(xhr.responseText); var route = data.routes[0].geometry; map.addLayer({ "id": "route", "type": "line", "source": { "type": "geojson", "data": { "type": "Feature", "properties": {}, "geometry": route } }, "layout": { "line-join": "round", "line-cap": "round" }, "paint": { "line-color": "#888", "line-width": 8 } }); } else { console.log('Request failed. Returned status of ' + xhr.status); } }; xhr.send(); ``` 需要注意的是,这里的`YOUR_ACCESS_TOKEN`需要替换为你自己的Mapbox Access Token。另外,该示例中的路径规划请求使用了XMLHttpRequest对象,你也可以使用fetch或其他方式来发送请求。在请求返回后,我们需要解析返回的数据,并在地图上添加一条线来表示路径。你可以通过修改该示例中的起点、终点、途经点等参数来自定义路径规划。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值