轨迹线路图层(TripsLayer)
示例
示例源码
tsx
import React, { useEffect, useRef, useState } from 'react'
import ReactDOM from 'react-dom'
import { MbMap, MbTiandituLayer } from '@mapbox-react/core'
import { MbTripsLayer } from '@mapbox-react/deck-layers'
const App = () => {
const [mapCenter] = useState([-73.9, 40.7])
const [zoom, setZoom] = useState(10)
const [pitch, setPitch] = useState(20)
const mapInst = useRef<any>()
const data = 'https://mapbox-web.github.io/mapbox-react/json/trips.json'
const [width, setWidth] = useState(100)
const [currentTime, setCurrentTime] = useState(0)
const loop = 1900
const animate = (timestamp: number) => {
setCurrentTime((timestamp / 10) % loop)
requestAnimationFrame(animate)
}
useEffect(() => {
animate(0)
}, [])
return (
<div className="map-wrapper">
<MbMap ref={mapInst} center={mapCenter} zoom={zoom} pitch={pitch}>
<MbTripsLayer
data={data}
getWidth={width}
getPath={(d) => d.path}
getTimestamps={(d) => d.timestamps}
trailLength={200}
currentTime={currentTime}
/>
</MbMap>
</div>
)
}
ReactDOM.render(<App />, document.querySelector('#root'))
API
PROPS
名称 | 描述 | 类型 | 默认值 |
---|---|---|---|
id | 图层Id | string | - |
autoHighlight | 本属性为true且pickable为true时,则会将鼠标悬浮选中的对象设为高亮 | boolean | false |
currentTime | 当前时间 | number | 0 |
data | 图层源数据 | string / IndexAny / AnyArr / Promise<any> | '' |
getColor | 颜色信息 | string / AnyFunc / number[] | black |
getTimestamps | 时间片信息 | AnyFunc | - |
getPath | 路径信息 | AnyFunc | object => object.path |
getWidth | 宽度信息 | number / AnyNumberFunc | 100 |
highlightColor | 要与高亮对象原始颜色进行混合(blend)的颜色值 | string / number[] | [255, 255, 128, 1] |
highlightedObjectIndex | 高亮元素序号 | number | -1 |
miterLimit | rounded为false时生效 | number | 4 |
capRounded | - | boolean | false |
jointRounded | - | boolean | false |
opacity | 透明度 | number | 1 |
pickable | 图层是否会响应鼠标事件,若为false,则组件不会emit鼠标相关事件 | boolean | false |
show | 是否显示 | boolean | true |
trailLength | - | number | 120 |
widthMaxPixels | - | number | Number.MAX_SAFE_INTEGER |
widthMinPixels | - | number | 0 |
widthScale | - | number | 1 |
widthUnits | - | string(pixels / meters) | meters |
EVENTS
名称 | 描述 | 参数 |
---|---|---|
onCreated | 初始化完成事件 | - |
onMouseMove | - | { object: any; coordinates: mapboxgl.LngLat; pixel: [number, number] } |
onMouseLeave | - | - |
onClick | - | { object: any; coordinates: mapboxgl.LngLat; pixel: [number, number] } |
METHODS
名称 | 描述 | 定义 |
---|