6 var hex
= '0123456789ABCDEF';
11 d
= Math
.floor(d
/ 16);
13 while (r
.length
< 2) r
= '0' + r
;
17 function gradient(color
) {
18 // First arc (0, PI) in HSV colorspace
19 function f2h(d
) { return d2h(256 * d
); }
22 else if (color
< 1.0/3)
23 return '#FF' + f2h(3 * color
) + '00';
24 else if (color
< 2.0/3)
25 return '#' + f2h(2 - 3 * color
) + 'FF00';
27 return '#00FF' + f2h(3 * color
- 2);
32 function gpx2time(s
) {
33 // 2011-09-24T12:07:53Z
34 if (s
.length
!== 10 + 1 + 8 + 1)
45 speedSplitEnable: function(options
) {
46 L
.Util
.setOptions(this, options
);
47 return this.on('addline', this.speed_split
, this);
50 speedSplitDisable: function() {
51 return this.off('addline', this.speed_split
, this);
54 speed_split: function(e
) {
55 var l
= e
.line
.pop(), ll
= l
.getLatLngs();
56 var chunk
= Math
.floor(ll
.length
/ this.options
.chunks
);
57 if (chunk
< 3) chunk
= 3;
59 for (var i
= 0; i
< ll
.length
; i
+= chunk
) {
61 if (i
+ chunk
> ll
.length
)
62 chunk
= ll
.length
- i
;
63 for (var j
= 0; j
< chunk
; j
++) {
64 if (p
) d
+= p
.distanceTo(ll
[i
+j
]);
66 if (!t
) t
= gpx2time(p
.meta
.time
);
68 p
= ll
[i
+ chunk
- 1];
69 t
= (gpx2time(p
.meta
.time
) - t
) / (3600 * 1000);
70 var speed
= 0.001 * d
/ t
;
71 var color
= gradient(speed
/ this.options
.maxSpeed
);
72 var poly
= new L
.Polyline(ll
.slice(i
, i
+chunk
+1), {color
: color
, weight
: 2, opacity
: 1});
73 poly
.bindPopup('Dist: ' + d
.toFixed() + 'm; Speed: ' + speed
.toFixed(2) + ' km/h');