Getting started (1)Display a mapStyles (11)Add a default markerAdd an animated icon to the mapAdd a generated icon to the mapGenerate and add a missing icon to the mapAdd a stretchable image to the mapAdd an icon to the mapDisplay a map with a custom styleRender world copiesDisplay a satellite mapChange a map's styleDisplay a mapLayers (40)Display buildings in 3DExtrude polygons for 3D indoor mappingAdd a 3D modelAdd an animated icon to the mapAdd a generated icon to the mapGenerate and add a missing icon to the mapAdd a stretchable image to the mapAdd an icon to the mapAdjust a layer's opacityAnimate a lineAnimate a series of imagesAnimate a pointChange building color based on zoom levelChange the case of labelsDisplay HTML clusters with custom propertiesCreate and style clustersChange a layer's color with buttonsAdd a custom style layerStyle circles with a data-driven propertyStyle lines with a data-driven propertyDisplay and style rich text labelsAdd a pattern to a polygonAdd a new layer below labelsAdd a GeoJSON lineDraw GeoJSON pointsAdd a GeoJSON polygonCreate a heatmap layerAdd hillshadingAdd an imageDisplay line that crosses 180th meridianCreate a gradient line using an expressionAdd multiple geometries from one GeoJSON sourceStyle ocean depth dataShow and hide layersChange worldview of administrative boundariesUpdate a choropleth layer by zoom levelVariable label placementAdd a videoVisualize population densityUse a fallback imageSources (16)Animate a lineAnimate a series of imagesAnimate a pointAdd a canvas sourceJoin local JSON data with vector tile geometriesAdd hillshadingAdd an imageDisplay line that crosses 180th meridianAdd live realtime dataUpdate a feature in realtimeAdd a raster tile sourceAdd a third party vector tile sourceCreate a time sliderAdd a vector tile sourceAdd a videoAdd a WMS sourceUser interaction (27)Adjust a layer's opacityCustomize camera animationsCenter the map on a clicked symbolChange a layer's color with buttonsAnimate 3D buildings based on ambient soundsDisable map rotationCreate a draggable MarkerCreate a draggable pointFilter features within map viewFilter symbols by text inputFilter symbols by toggling a listCreate a hover effectDisplay a non-interactive mapChange a map's languageMeasure distancesGet coordinates of the mouse pointerHighlight features containing similar dataSelect features around a clicked pointGet features under the mouse pointerRestrict map panning to an areaChange a map's styleCreate a time sliderToggle interactionsChange worldview of administrative boundariesUpdate a choropleth layer by zoom levelHighlight features within a bounding boxFit to the bounds of a LineStringCamera (13)Animate map camera around a pointAnimate a point along a routeCustomize camera animationsCenter the map on a clicked symbolFit a map to a bounding boxSlowly fly to a locationFly to a locationNavigate the map with game-like controlsJump to a series of locationsOffset the vanishing point using paddingPlay map locations as a slideshowFly to a location based on scroll positionSet pitch and bearingControls and overlays (17)Animate a markerChange the default position for attributionAdd custom icons with MarkersDisable scroll zoomView a fullscreen mapLocate the userSwipe between mapsDisplay driving directionsShow drawn polygon areaAdd a geocoderAdd a marker using a place nameDisplay map navigation controlsShow polygon information on clickDisplay a popup on clickDisplay a popup on hoverDisplay a popupAttach a popup to a marker instanceGeocoder (10)Supplement forward geocoding search results from another data sourceAccept coordinates as input to a geocoderLimit geocoder results to a named regionUse a custom render function with a geocoderUse the geocoder without a mapPlace the geocoder input outside the mapUse a custom camera animation with a geocoderLocalize the geocoder to a given languageAdd a geocoderSet a point after Geocoder resultBrowser support (1)Check if Mapbox GL JS is supportedInternationalization support (2)Use locally generated ideographsAdd support for right-to-left scripts

Animate a point along a route

Use Turf to smoothly animate a point along the distance of a line.

<!DOCTYPE html>
<meta charset="utf-8" />
<title>Animate a point along a route</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src=""></script>
<link href="" rel="stylesheet" />
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
.overlay {
position: absolute;
top: 10px;
left: 10px;
.overlay button {
font: 600 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
background-color: #3386c0;
color: #fff;
display: inline-block;
margin: 0;
padding: 10px 20px;
border: none;
cursor: pointer;
border-radius: 3px;
.overlay button:hover {
background-color: #4ea0da;
<div id="map"></div>
<div class="overlay">
<button id="replay">Replay</button>
mapboxgl.accessToken = '<your access token here>';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-96, 37.8],
zoom: 3
// San Francisco
var origin = [-122.414, 37.776];
// Washington DC
var destination = [-77.032, 38.913];
// A simple line from origin to destination.
var route = {
'type': 'FeatureCollection',
'features': [
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': [origin, destination]
// A single point that animates along the route.
// Coordinates are initially set to origin.
var point = {
'type': 'FeatureCollection',
'features': [
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Point',
'coordinates': origin
// Calculate the distance in kilometers between route start/end point.
var lineDistance = turf.lineDistance(route.features[0], 'kilometers');
var arc = [];
// Number of steps to use in the arc and animation, more steps means
// a smoother arc and animation, but too many steps will result in a
// low frame rate
var steps = 500;
// Draw an arc between the `origin` & `destination` of the two points
for (var i = 0; i < lineDistance; i += lineDistance / steps) {
var segment = turf.along(route.features[0], i, 'kilometers');
// Update the route with calculated arc coordinates
route.features[0].geometry.coordinates = arc;
// Used to increment the value of the point measurement against the route.
var counter = 0;
map.on('load', function () {
// Add a source and layer displaying a point which will be animated in a circle.
map.addSource('route', {
'type': 'geojson',
'data': route
map.addSource('point', {
'type': 'geojson',
'data': point
'id': 'route',
'source': 'route',
'type': 'line',
'paint': {
'line-width': 2,
'line-color': '#007cbf'
'id': 'point',
'source': 'point',
'type': 'symbol',
'layout': {
'icon-image': 'airport-15',
'icon-rotate': ['get', 'bearing'],
'icon-rotation-alignment': 'map',
'icon-allow-overlap': true,
'icon-ignore-placement': true
function animate() {
// Update point geometry to a new position based on counter denoting
// the index to access the arc.
point.features[0].geometry.coordinates =
// Calculate the bearing to ensure the icon is rotated to match the route arc
// The bearing is calculate between the current point and the next point, except
// at the end of the arc use the previous point and the current point
point.features[0].properties.bearing = turf.bearing(
counter >= steps ? counter - 1 : counter
counter >= steps ? counter : counter + 1
// Update the source with this new data.
// Request the next frame of animation so long the end has not been reached.
if (counter < steps) {
counter = counter + 1;
.addEventListener('click', function () {
// Set the coordinates of the original point back to origin
point.features[0].geometry.coordinates = origin;
// Update the source layer
// Reset the counter
counter = 0;
// Restart the animation.
// Start the animation.
Was this example helpful?