2018-08-28 16:50:57 +02:00
|
|
|
import FileSaver from 'file-saver';
|
|
|
|
import createGPX from 'gps-to-gpx';
|
|
|
|
|
|
|
|
import { VERSION } from '@/constants';
|
2018-08-28 20:36:36 +02:00
|
|
|
import { formatDate } from '@/tools/date';
|
2018-08-28 16:50:57 +02:00
|
|
|
|
|
|
|
export default function (activityName, locationGPX) {
|
|
|
|
const courseKey = 'heading';
|
|
|
|
const eleKey = 'elevation';
|
|
|
|
const hdopKey = 'hdop';
|
|
|
|
const speedKey = 'speed';
|
|
|
|
const vdopKey = 'vdop';
|
|
|
|
|
|
|
|
const waypoints = [];
|
|
|
|
locationGPX.forEach((item) => {
|
|
|
|
const waypoint = Object.assign({}, item, { timestamp: new Date(item.timestamp) });
|
|
|
|
[courseKey, eleKey, hdopKey, speedKey, vdopKey].forEach((key) => {
|
|
|
|
if (waypoint[key] === null || waypoint[key] === undefined) {
|
|
|
|
delete waypoint[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
waypoints.push(waypoint);
|
|
|
|
});
|
|
|
|
const gpx = createGPX(waypoints, {
|
|
|
|
activityName,
|
|
|
|
creator: `Cycl'Assist v${VERSION}`,
|
|
|
|
courseKey,
|
|
|
|
eleKey,
|
|
|
|
hdopKey,
|
|
|
|
latKey: 'latitude',
|
|
|
|
lonKey: 'longitude',
|
|
|
|
speedKey,
|
|
|
|
startTime: waypoints[0].timestamp,
|
|
|
|
timeKey: 'timestamp',
|
|
|
|
vdopKey,
|
|
|
|
});
|
|
|
|
FileSaver.saveAs(
|
|
|
|
new Blob([gpx], { type: 'application/gpx+xml;charset=utf-8' }),
|
2018-08-28 20:36:36 +02:00
|
|
|
`cyclassist_${formatDate(new Date(), 'YYYY-MM-DD_HH-mm_ddd')}.gpx`,
|
2018-08-28 16:50:57 +02:00
|
|
|
);
|
|
|
|
}
|