leaflet-elevation
leaflet-elevation copied to clipboard
Time calculation is incorrect when using imperial units
Hi,
I noticed that the time calculation in src/handlers/time.js is incorrect when the imperial option is set to true.
The current code assumes the distance is always in metric units, which leads to an inaccurate time when the distance is displayed in miles.
Here is a more robust fix that resolves the issue by always using the original metric distance for the time calculation.
In src/handlers/time.js, inside the pointToAttr function, I replaced this:
if (i > 0) {
let dx = (this._data[i].dist - this._data[i - 1].dist);
let t0 = this._data[i - 1].time.getTime();
point.meta.time = new Date(t0 + ( dx / this._timeAVGSpeed) * this.options.timeFactor * 1000);
}
With this:
if (i > 0) {
let dx = this._data[i].latlng.distanceTo(this._data[i-1].latlng) / 1000; // distance in km
let t0 = this._data[i - 1].time.getTime();
point.meta.time = new Date(t0 + ( dx / this._timeAVGSpeed) * this.options.timeFactor * 1000);
}
This ensures the time calculation is always accurate, regardless of the display units.
Thanks for the great library!