Skip to content

TripsLayer

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

NameDescriptionTypeDefault
idLayer IDstring-
autoHighlightWhen this property is true and pickable is also true, the hovered object will be highlighted.booleanfalse
currentTimeCurrent time.number0
dataSource data for the layer.string / IndexAny / AnyArr / Promise<any>''
getColorColor information.string / AnyFunc / number[]'black'
getTimestampsFunction to retrieve timestamp information.AnyFunc-
getPathFunction to retrieve path information.AnyFuncobject => object.path
getWidthWidth information.number / AnyNumberFunc100
highlightColorThe color to blend with the original color of the highlighted object.string / number[][255, 255, 128, 1]
highlightedObjectIndexThe index of the currently highlighted element.number-1
miterLimitApplies when joint-rounded is false.number4
capRoundedWhether the line caps are rounded.booleanfalse
jointRoundedWhether the line joints are rounded.booleanfalse
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
showWhether the layer is visible.booleantrue
trailLengthLength of the trail.number120
widthMaxPixelsMaximum width in pixels.numberNumber.MAX_SAFE_INTEGER
widthMinPixelsMinimum width in pixels.number0
widthScaleA scaling factor applied to the width.number1
widthUnitsUnits for the width: 'pixels' or 'meters'.string ('pixels', 'meters')'meters'

EVENTS

NameDescriptionParameters
onCreatedInitialization complete event-
onMouseMove-{ object: any; coordinates: mapboxgl.LngLat; pixel: [number, number] }
onMouseLeave--
onClick-{ object: any; coordinates: mapboxgl.LngLat; pixel: [number, number] }

METHODS

NameDescriptionDefinition