A Leaflet plugin for animating a marker along a polyline.
Check out the demo.
- Leaflet ^1.0.0
npm install leaflet.animatedmarker<script src="https://unpkg.com/leaflet.animatedmarker/src/AnimatedMarker.js"></script>const line = L.polyline([
[40.68510, -73.94136],
[40.68576, -73.94149],
[40.68649, -73.94165]
]);
const animatedMarker = L.animatedMarker(line.getLatLngs());
map.addLayer(animatedMarker);import L from 'leaflet';
import 'leaflet.animatedmarker';
const animatedMarker = L.animatedMarker(line.getLatLngs());TypeScript definitions are included. The module augments Leaflet's types:
import L from 'leaflet';
import 'leaflet.animatedmarker';
const marker: L.AnimatedMarker = L.animatedMarker(line.getLatLngs(), {
distance: 300,
interval: 2000,
autoStart: false,
onEnd: () => console.log('Animation complete!')
});| Option | Type | Default | Description |
|---|---|---|---|
distance |
number |
200 |
Distance in meters to travel per interval |
interval |
number |
1000 |
Time in milliseconds to travel the distance |
autoStart |
boolean |
true |
Start animating immediately when added to the map |
onEnd |
function |
() => {} |
Callback when animation reaches the end |
Plus all standard Leaflet Marker options.
Start the animation from the beginning.
animatedMarker.start();Stop the animation at the current position.
animatedMarker.stop();Set a new path for the marker to follow.
animatedMarker.setLine(newPolyline.getLatLngs());const animatedMarker = L.animatedMarker(line.getLatLngs(), {
distance: 300, // meters
interval: 2000 // milliseconds
});const animatedMarker = L.animatedMarker(line.getLatLngs(), {
autoStart: false
});
// Start when ready
animatedMarker.start();
// Stop after 5 seconds
setTimeout(() => {
animatedMarker.stop();
}, 5000);const myIcon = L.icon({
iconUrl: 'myicon.png'
});
const animatedMarker = L.animatedMarker(line.getLatLngs(), {
icon: myIcon
});const animatedMarker = L.animatedMarker(line.getLatLngs(), {
onEnd: function() {
console.log('Animation finished!');
map.removeLayer(this);
}
});npm installnpm testnpm run test:watch- Fixed: Marker no longer floats to new position when zooming during animation
- Added: TypeScript type definitions
- Added: UMD module support (AMD, CommonJS, browser globals)
- Added: Vitest test suite
- Changed: Modernized to ES6 syntax
- Changed: Renamed
clickableoption tointeractive(Leaflet 1.x naming) - Removed: Legacy fallback for browsers without CSS3 transitions
- Removed: Bower support (deprecated)
- Initial release
MIT License - OpenPlans