Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use defined CRS for distance calculation #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
/nbproject
22 changes: 16 additions & 6 deletions src/AnimatedMarker.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
L.AnimatedMarker = L.Marker.extend({
includes: L.Mixin.Events,
options: {
// meters
distance: 200,
// ms
interval: 1000,
// animate on add?
autoStart: true,
// callback onend
onEnd: function(){},
clickable: false
},

Expand All @@ -26,15 +25,15 @@ L.AnimatedMarker = L.Marker.extend({
for (i=1;i<len;i++) {
var cur = latlngs[i-1],
next = latlngs[i],
dist = cur.distanceTo(next),
dist = this._distance(cur, next),
factor = this.options.distance / dist,
dLat = factor * (next.lat - cur.lat),
dLng = factor * (next.lng - cur.lng);

if (dist > this.options.distance) {
while (dist > this.options.distance) {
cur = new L.LatLng(cur.lat + dLat, cur.lng + dLng);
dist = cur.distanceTo(next);
dist = this._distance(cur, next);
chunkedLatLngs.push(cur);
}
} else {
Expand All @@ -48,6 +47,7 @@ L.AnimatedMarker = L.Marker.extend({

onAdd: function (map) {
L.Marker.prototype.onAdd.call(this, map);
this._map = map;

// Start animating when added to the map
if (this.options.autoStart) {
Expand All @@ -62,7 +62,7 @@ L.AnimatedMarker = L.Marker.extend({

// Normalize the transition speed from vertex to vertex
if (this._i < len) {
speed = this._latlngs[this._i-1].distanceTo(this._latlngs[this._i]) / this.options.distance * this.options.interval;
speed = this._distance(this._latlngs[this._i-1], this._latlngs[this._i]) / this.options.distance * this.options.interval;
}

// Only if CSS3 transitions are supported
Expand All @@ -78,7 +78,9 @@ L.AnimatedMarker = L.Marker.extend({
// Queue up the animation to the next next vertex
this._tid = setTimeout(function(){
if (self._i === len) {
self.options.onEnd.apply(self, Array.prototype.slice.call(arguments));
if (self._icon) { self._icon.style[L.DomUtil.TRANSITION] = ''; }
if (self._shadow) { self._shadow.style[L.DomUtil.TRANSITION] = ''; }
self.fire('animationend');
} else {
self.animate();
}
Expand Down Expand Up @@ -108,6 +110,14 @@ L.AnimatedMarker = L.Marker.extend({
this.options.interval = 30;
}
this._i = 1;
},

_distance: function(cur, next) {
if (this._map.options.crs.distance) {
return this._map.options.crs.distance(cur, next);
} else {
return cur.distanceTo(next);
}
}

});
Expand Down