How about this?
/* https://www.liedman.net/leaflet-routing-machine/tutorials/integration/ */
.on('routesfound', function(e) {
GeoJsonCode = L.Routing.routeToGeoJson(e.routes[0]);
//console.log(GeoJsonCode);
})
function ExportGPX(){
let outputGpxText;
let sp2 = ' ';
let fileName = 'abcdefg';
let ackName = 'hijklml';
let elevFlag = false;
if(typeof GeoJsonCode !== "undefined"){ // undefined check!
//ファイルの中身をJSON形式に変換する
let track = JSON.parse(GeoJsonCode);
// https://stackoverflow.com/questions/51397890/how-to-use-polyline-decorator-in-leaflet-using-geojson-line-string
track.features
.filter(function (feature) { return feature.geometry.type == "LineString" })
.map(function (feature) {
let textGpx = '<?xml version="1.0" encoding="UTF-8"?>' + '\n' +
'<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" version="1.1" author="signpost - https://signpost.mydns.jp/">' + '\n' +
'<metadata></metadata>' + '\n' +
'<trk>' + '\n' +
'<name>' + trackName + '<name>' + '\n' +
'<desc></desc>' + '\n' +
'<trkseg>' + '\n';
let coordinates = feature.geometry.coordinates;
if (elevFlag) { // 標高が有る場合
coordinates.forEach(function (coordinate, i) {
textGpx = textGpx + sp2 + '<trkpt lat="' + coordinate[1] + '" lon="' + coordinate[0] + '">';
textGpx = textGpx + '<ele>' + coordinate[2] + '</ele>';
textGpx = textGpx + '</trkpt>' + '\n';
})
} else{ // 標高が無い場合
coordinates.forEach(function (coordinate, i) {
textGpx = textGpx + sp2 + '<trkpt lat="' + coordinate[1] + '" lon="' + coordinate[0] + '">';
textGpx = textGpx + '<ele></ele>';
textGpx = textGpx + '</trkpt>' + '\n';
})
}
textGpx = textGpx + '</trkseg></trk>' + '\n' + '</gpx>'
//console.log(textGpx);
outputGPXstyle = textGpx;
})
// Code Export >>>>>
/* https://qiita.com/kerupani129/items/99fd7a768538fcd33420
※ [0].click():jQuery オブジェクトの [0] は HTMLElement オブジェクト。HTMLElement.click() を呼んでいる
https://www.sejuku.net/blog/67735
----------------------------------------------*/
$('<a>', {
href: window.URL.createObjectURL(new Blob([outputGPXstyle], {type : 'text/plain'})),
download: fileName + '.gpx'
})[0].click(); // ※ [0].click()*/
} else {
alert('There is no auth to export');
}
};