leaflet-drift-marker
Animated markers for Leaflet 2 that move smoothly to a new position instead of jumping. Click the map to send the marker there, or let it drive the route.
Versions
| Leaflet | leaflet-drift-marker | Install |
|---|---|---|
| 2.x | 3.x (beta, ESM only) | npm install leaflet-drift-marker@next |
| 1.x | 2.x (example) | npm install leaflet-drift-marker@2 |
Install
npm install leaflet-drift-marker@next leaflet@beta
Usage
With a bundler
import { LeafletMap, TileLayer } from "leaflet";
import DriftMarker from "leaflet-drift-marker";
const map = new LeafletMap("map").setView([0, 0], 4);
new TileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(map);
const marker = new DriftMarker([10, 10]).addTo(map);
marker.slideTo([20, 20], {
duration: 2000,
keepAtCenter: true,
});
In the browser without a bundler
Use a module script and an import map so leaflet resolves to Leaflet 2's ESM
build.
<link rel="stylesheet" href="https://unpkg.com/leaflet@2.0.0-beta.5/dist/leaflet.css" />
<script type="importmap">
{
"imports": {
"leaflet": "https://unpkg.com/leaflet@2.0.0-beta.5/dist/leaflet.js",
"leaflet-drift-marker": "https://unpkg.com/leaflet-drift-marker@next/lib/index.js"
}
}
</script>
<div id="map" style="height: 400px"></div>
<script type="module">
import { LeafletMap, TileLayer } from "leaflet";
import DriftMarker from "leaflet-drift-marker";
const map = new LeafletMap("map").setView([0, 0], 4);
new TileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(map);
new DriftMarker([0, 0]).addTo(map).slideTo([10, 10], { duration: 1000 });
</script>
API
DriftMarker accepts everything Leaflet's
Marker does, and
adds:
-
.slideTo(latlng, options): animates the marker tolatlng. Firesmovestartandmoveend.options.duration(number, required): animation length in ms.-
options.keepAtCenter(boolean, optional): pans the map with the marker and locks dragging and zoom-to-point until the slide ends.
.slideCancel(): stops the current slide and restores map interaction.
Calling setLatLng or removing the marker during a slide cancels the slide. A
duration of 0 moves the marker straight to the target.
Upgrading from 2.x
- Upgrade to Leaflet 2 first; 3.x does not run on Leaflet 1.
-
ESM only:
require()and the<script>build (window.DriftMarker) are gone. Useimport. -
The deprecated
slideOptionstype is removed; useSlideOptions.