Skip to content

TrailLayer

Example

Example Source Code
tsx
import React, { useEffect, useRef, useState } from 'react'
import ReactDOM from 'react-dom'
import { MbMap, MbTiandituLayer } from '@mapbox-react/core'
import { MbTrailLayer } from '@mapbox-react/effect-layers'

const App = () => {
  const [mapCenter] = useState([116.17381, 39.92155464])
  const [zoom, setZoom] = useState(9)
  const [pitch, setPitch] = useState(0)
  const mapInst = useRef<any>()

  const [trailLength, setTrailLength] = useState(3)

  const getPath = (data: any) => {
    let prevPt: any
    const points: number[][] = []
    for (let i = 0; i < data.length; i += 2) {
      let pt = [data[i], data[i + 1]]
      if (i > 0) {
        pt = [prevPt[0] + pt[0], prevPt[1] + pt[1]]
      }
      prevPt = pt

      points.push([pt[0] / 1e4, pt[1] / 1e4])
    }
    return points
  }
  const getColor = () => [
    Math.random() * 150 + 100,
    Math.random() * 100 + 28,
    93,
  ]

  return (
    <div className="map-wrapper">
      <MbMap ref={mapInst} center={mapCenter} zoom={zoom} pitch={pitch}>
        <MbTrailLayer
          data="https://mapbox-web.github.io/mapbox-react/json/beijing-bus-lines.json"
          getPath={getPath}
          getColor={getColor}
          getWidth={100}
          speed={3}
          trailLength={trailLength}
          interval={5}
        />
      </MbMap>
    </div>
  )
}

ReactDOM.render(<App />, document.querySelector('#root'))

API

PROPS

NameDescriptionTypeDefault
idLayer IDstring-
speedSpeed of movement/animation.number1
trailLengthLength of the trail (e.g., in number of segments or time units).number10
intervalTime interval between updates/frames (e.g., in milliseconds).number5
dataSource data for the layer.string / IndexAny / AnyArr / Promise<any>''
showWhether the layer is visible.booleantrue
opacityThe opacity of the layer (0-1).number1
pickableWhether the layer responds to mouse events. If false, the component will not emit mouse-related events.booleanfalse
autoHighlightWhen true and pickable is also true, the hovered object will be highlighted.booleanfalse
highlightColorThe color to blend with the original color of the highlighted object.string / number[][255, 255, 128, 1]
highlightedObjectIndexThe index of the highlighted element.number-1
onDataLoadCallback function invoked after data loading is complete.AnyFunc-
dataTransformFunction to transform the data before rendering.AnyFunc-
transitionsTransition settings for the layer.IndexAny{}
getColorColor information.string / AnyFunc / number[]black
getPathPath information.AnyFuncobject => object.path
getWidthWidth information.number / AnyNumberFunc100
miterLimitMiter limit (used when cap-rounded is false).number4
capRoundedWhether the line caps are rounded.booleanfalse
jointRoundedWhether the line joints are rounded.booleanfalse
widthMaxPixelsMaximum width in pixels.numberNumber.MAX_SAFE_INTEGER
widthMinPixelsMinimum width in pixels.number0
widthScaleScaling factor for the width.number1
widthUnitsUnits for the width (pixels or meters).string ('pixels' / 'meters')'meters'

EVENTS

NameDescriptionParameters

METHODS

NameDescriptionDefinition