Animate a Point Along a Route
Animate a point marker along a route using Turf.js for interpolation.
const API_KEY = 'toursprung';
// Route: Innsbruck → Salzburg → Vienna
const route = turf.lineString([
[11.39085, 47.27574],
[13.0550, 47.8095],
[15.4395, 47.0707],
[16.3738, 48.2082]
]);
const lineLength = turf.length(route, { units: 'kilometers' });
const steps = 500;
const point = { type: 'Feature', geometry: { type: 'Point', coordinates: route.geometry.coordinates[0] } };
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
center: [13.5, 47.6],
zoom: 7,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
let step = 0;
let animId;
map.on('load', () => {
map.addSource('route', { type: 'geojson', data: route });
map.addSource('point', { type: 'geojson', data: point });
map.addLayer({
id: 'route',
type: 'line',
source: 'route',
paint: { 'line-color': '#888', 'line-width': 3, 'line-dasharray': [2, 2] }
});
map.addLayer({
id: 'point',
type: 'circle',
source: 'point',
paint: { 'circle-radius': 10, 'circle-color': '#3887be', 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' }
});
function animate() {
const along = turf.along(route, (step / steps) * lineLength, { units: 'kilometers' });
point.geometry.coordinates = along.geometry.coordinates;
map.getSource('point').setData(point);
if (step < steps) {
step++;
animId = requestAnimationFrame(animate);
}
}
animate();
});
document.getElementById('replay').addEventListener('click', () => {
cancelAnimationFrame(animId);
step = 0;
function animate() {
const along = turf.along(route, (step / steps) * lineLength, { units: 'kilometers' });
point.geometry.coordinates = along.geometry.coordinates;
map.getSource('point').setData(point);
if (step < steps) {
step++;
animId = requestAnimationFrame(animate);
}
}
animate();
});<!DOCTYPE html>
<html lang="en">
<head>
<title>Animate a Point Along a Route – Maptoolkit Maps JS</title>
<meta property="og:description" content="Animate a point marker along a route using Turf.js for interpolation." />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.css" />
<script src="https://unpkg.com/@turf/turf@6/turf.min.js"></script>
<style>
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
#map { width: 100%; height: 100%; }
#replay {
position: absolute;
top: 10px;
left: 10px;
padding: 10px 20px;
background: #3887be;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 14px;
}
</style>
</head>
<body>
<div id="map"></div>
<button id="replay">Replay</button>
<script>
const API_KEY = 'toursprung';
// Route: Innsbruck → Salzburg → Vienna
const route = turf.lineString([
[11.39085, 47.27574],
[13.0550, 47.8095],
[15.4395, 47.0707],
[16.3738, 48.2082]
]);
const lineLength = turf.length(route, { units: 'kilometers' });
const steps = 500;
const point = { type: 'Feature', geometry: { type: 'Point', coordinates: route.geometry.coordinates[0] } };
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
center: [13.5, 47.6],
zoom: 7,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
let step = 0;
let animId;
map.on('load', () => {
map.addSource('route', { type: 'geojson', data: route });
map.addSource('point', { type: 'geojson', data: point });
map.addLayer({
id: 'route',
type: 'line',
source: 'route',
paint: { 'line-color': '#888', 'line-width': 3, 'line-dasharray': [2, 2] }
});
map.addLayer({
id: 'point',
type: 'circle',
source: 'point',
paint: { 'circle-radius': 10, 'circle-color': '#3887be', 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' }
});
function animate() {
const along = turf.along(route, (step / steps) * lineLength, { units: 'kilometers' });
point.geometry.coordinates = along.geometry.coordinates;
map.getSource('point').setData(point);
if (step < steps) {
step++;
animId = requestAnimationFrame(animate);
}
}
animate();
});
document.getElementById('replay').addEventListener('click', () => {
cancelAnimationFrame(animId);
step = 0;
function animate() {
const along = turf.along(route, (step / steps) * lineLength, { units: 'kilometers' });
point.geometry.coordinates = along.geometry.coordinates;
map.getSource('point').setData(point);
if (step < steps) {
step++;
animId = requestAnimationFrame(animate);
}
}
animate();
});
</script>
</body>
</html>