Wim Pruiksma
Wim Pruiksma

Reputation: 598

How to animate a custom Leaflet marker along a route?

I am very much new to leaflet and i hope that someone can help me. What im trying to do is adding two markers on the map and make another marker follow the route.

I have found a few plugins that would help but these plugins makes your marker over the map and not follow a specific route. http://ewoken.github.io/Leaflet.MovingMarker/

I know how it is done in google maps but not in leaflet. https://www.youtube.com/watch?v=zbr-F9wVqgU

Upvotes: 4

Views: 12415

Answers (1)

Julien V
Julien V

Reputation: 895

You are close. You choosed the Leaflet plugin and have quite a precise goal. You just have to follow what is explained here.

Let's implement that :

// here is the path (get it from where you want)
var coordinateArray = [ [0,1], [1,1], [1,0] ];
// or for example
var coordinateArray = existingPolyline.getLatLngs();
// here is the line you draw (if you want to see the animated marker path on the map)
var myPolyline = L.polyline(coordinateArray);
myPolyline.addTo(map);
// i don't know if i understood your question correctly
// if you want to put a marker at the beginning and at the end of the path :
var mstart = L.marker(coordinateArray[0]).addTo(map);
var mend = L.marker(coordinateArray[coordinateArray.length - 1]).addTo(map);
// here is the moving marker (6 seconds animation)
var myMovingMarker = L.Marker.movingMarker(coordinateArray, 6000, {
    autostart: false
});
map.addLayer(myMovingMarker);
myMovingMarker.start();

Upvotes: 6

Related Questions