ewoken / Leaflet.MovingMarker

A Leaflet plug-in to create moving marker
MIT License
341 stars 176 forks source link

Leaflet.MovingMarker


A Leaflet plug-in to create moving marker. Very useful to represent transportations or other movable things !

Demos

Follow this link: here

Compatibility with Leaflet Versions

Compatible with the latest stable Leaflet version leaflet-0.7.2.

Browser Compatibility

This plugin internally uses window.requestAnimationFrame through the Leaflet function L.Util.requestAnimFrame

Features

Usage

Add this line to your HTML file:

<script type="text/javascript" src="https://github.com/ewoken/Leaflet.MovingMarker/raw/master/MovingMarker.js"></script>

Then add your first MovingMarker:

var myMovingMarker = L.Marker.movingMarker([[48.8567, 2.3508],[50.45, 30.523333]],
                        [20000]).addTo(map);
//...
myMovingMarker.start();

API

Factory

L.movingMarker(<LatLng[]> latlngs, <Number[]> durations [,<Object> options]);

durations:

Note: As Leaftlet's other functions, it also accept them in a simple Array form and simple object form (see Leaflet docs).

Options All the marker's options are available.

Methods

Getter

Note: Marker.getLatLng() still works and give the current position

Setter

Events

Note: Event are not synchrone because of the use of requestAnimationFrame. If you quit the tab where the animation is working, events will be fired when the tab will get back the focus. Events end and loop have the attribute elapsedTime to get the time elapsed since the real end/loop.

How it works

This plugin internally uses window.requestAnimationFrame through the Leaflet function L.Util.requestAnimFrame. When the browser need to repaint, the marker interpolate linearly its position thanks to the elapsed time.

Why do you just not use transitions ?

If your marker moves very slowly (1-2 min or more for one transition) and you zoom in, your marker will be at the wrong place and take a lot of time to be where it has to be. Moreover, you have to do some hacks to get the current position of the marker during the animation.

Future Features

TODO

Tests ;-)

License

MIT License