summaryrefslogtreecommitdiff
path: root/assets/static/vendor/leaflet-elevation/src/handlers/distance.js
blob: fa6fa1df7030d6a4e76d1520fdf5a81ba0c095b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
export function Distance() {

	const _ = L.Control.Elevation.Utils;

	let opts     = this.options;
	let distance = {};

	opts.distanceFactor = opts.imperial ? this.__mileFactor : (opts.distanceFactor || 1); // 1 km = (1000 m)
	distance.label      = opts.imperial ? "mi" : opts.xLabel;

	return {
		name: 'distance',
		required: true,
		attr: 'dist',
		unit: distance.label,
		decimals: 5,
		pointToAttr: (_, i) => (i > 0 ? this._data[i - 1].dist : 0) + (this._data[i].latlng.distanceTo(this._data[i > 0 ? i - 1 : i].latlng) * opts.distanceFactor) / 1000, // convert back km to meters
		// stats: { total: _.iSum },
		onPointAdded: (distance, i) => this.track_info.distance = distance,
		scale: opts.distance && {
			axis    : "x",
			position: "bottom",
			scale   : "x", // this._chart._x,
			labelY  : 25,
			labelX  : () => this._width() + 6,
			ticks   : () => _.clamp(this._chart._xTicks() / 2, [4, +Infinity]),
		},
		grid: opts.distance && {
			axis      : "x",
			position  : "bottom",
			scale     : "x" // this._chart._x,
		},
		tooltip: opts.distance && {
			name: 'x',
			chart: (item) => L._("x: ") + _.round(item[opts.xAttr], opts.decimalsX) + " " + distance.label,
			order: 20
		},
		summary: opts.distance && {
			"totlen"  : {
				label: "Total Length: ",
				value: (track) => (track.distance || 0).toFixed(2) + ' ' + distance.label,
				order: 10
			}
		}
	};
}