Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 416
Wim Pruiksma Avatar asked Oct 24 '25 19:10

Wim Pruiksma


1 Answers

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();
like image 166
Julien V Avatar answered Oct 27 '25 10:10

Julien V