Time Series and Trip Animation

Use TripsLayer on a Mapbox map to visualize timestamped vehicle, ship, or aircraft trips.

🎯 Introduction

By the end of this article, you will be able to prepare timestamped paths and use TripsLayer to show animated trips with trails on a Mapbox map.

🧱 TripsLayer data structure

Each trip needs at least path and timestamps, with corresponding entries in the two arrays:

const trips = [{
    path: [
        [116.32, 39.87],
        [116.37, 39.89],
        [116.42, 39.91],
        [116.48, 39.95],
    ],
    timestamps: [0, 10, 20, 30],
}];

path is a sequence of longitude/latitude coordinates, and timestamps gives the time value for each position. Time values need not be actual dates, but must maintain a comparable order.

🛠 Create a trip layer

const layer = new TripsLayer({
    id: 'vehicle-trips',
    data: trips,
    getPath: (trip) => trip.path,
    getTimestamps: (trip) => trip.timestamps,
    currentTime: 20,
    trailLength: 12,
    getColor: [16, 185, 129],
    widthMinPixels: 5,
});

overlay.setProps({ layers: [layer] });

currentTime determines the playback position, while trailLength determines how much of the trail remains visible. Mapbox continues to handle city roads and the camera; TripsLayer only visualizes trips.

Continuously update the time on TripsLayer to animate trips over the Mapbox basemap:

▶️ Play, pause, and the time window

The core of the animation is changing currentTime and updating the overlay's layers:

let currentTime = 0;

function updateAnimation() {
    currentTime += 0.25;
    overlay.setProps({
        layers: [new TripsLayer({
            id: 'vehicle-trips',
            data: trips,
            getPath: (trip) => trip.path,
            getTimestamps: (trip) => trip.timestamps,
            currentTime,
            trailLength: 12,
        })],
    });
}

In production, page state manages playback buttons, the timeline, and speed selection. Cancel requestAnimationFrame when leaving the page to avoid updating an overlay that has already unmounted.

🪤 Common time data problems

  • Different lengths for path and timestamps misalign positions and times.
  • Time ranges that are too large or too small make playback speed difficult to control.
  • Convert trip coordinates first if they are not WGS84.
  • An overly long trailLength makes trips hard to distinguish; adjust it alongside playback speed.

🧾 Summary

  • TripsLayer represents moving trips with paths and timestamps.
  • currentTime controls playback position; trailLength controls trail length.
  • Animation updates only deck.gl layers without recreating the Mapbox map.
  • Page state manages playback controls; cancel animation frames on unmount.

❓ Questions and answers

Q1: Must timestamps contain date strings?

No. Comparable numeric time values are sufficient. Actual dates can first be converted to relative seconds.

Q2: Why is the trip not moving?

Check that currentTime is continuously updated and that path and timestamps correspond correctly.

Q3: Does map zoom affect trip animation?

It does not change animation time. Changes to the Mapbox camera only change the viewing area; TripsLayer keeps its own time scale.

🧪 Exercises

  1. Change trailLength to 5 and observe the shorter trail.
  2. Add a second route to trips and use a different color.

🎉 You can now visualize spatial trips as they change over time.