回答編集履歴

1

たとえば

2025/01/25 06:27

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア37445

test CHANGED
@@ -1 +1,155 @@
1
1
  おそらく、`addElevation`関数で`coordinates`配列に非同期に追加している要素を待ち切れていないのだと思います。どこかで`Promise`を受け取るなどをするしかなさそうですね。
2
+
3
+ ### たとえば
4
+
5
+ ```html
6
+ <!DOCTYPE html>
7
+ <html lang="ja">
8
+
9
+ <head>
10
+ <meta charset="UTF-8">
11
+ <meta name="viewport" content="width=device-width,initial-scale=1.0, ,user-scalable=no">
12
+ </head>
13
+
14
+ <body>
15
+ <button class="work" onclick="exportGPX()">
16
+ <i class="fa-solid fa-print">exportGPX</i>
17
+ </button>
18
+ <script type="text/javascript" src="https://maps.gsi.go.jp/js.lib/leaflet-1.2.0/leaflet.js"></script>
19
+ <script type="text/javascript" src="https://maps.gsi.go.jp/js.lib/jquery/jquery-1.11.1.min.js"></script>
20
+ <script>
21
+ var track = {
22
+ "type": "FeatureCollection",
23
+ "features": [
24
+ {
25
+ "type": "Feature",
26
+ "properties": {
27
+ "id": "waypoints",
28
+ "names": "|"
29
+ },
30
+ "geometry": {
31
+ "type": "MultiPoint",
32
+ "coordinates": [[140.834125, 38.310143], [140.860454, 38.222667]]
33
+ }
34
+ },
35
+ {
36
+ "type": "Feature",
37
+ "properties": {
38
+ "id": "line"
39
+ },
40
+ "geometry": {
41
+ "type": "LineString",
42
+ "coordinates": [
43
+ [140.83413, 38.31014], [140.83424, 38.30989], [140.86038, 38.22176], [140.86069, 38.22186], [140.86045, 38.22267], [140.86045, 38.22267]
44
+ ]
45
+ }
46
+ }]
47
+ }
48
+
49
+ var outputGPXstyle = '<?xml version="1.0" encoding="UTF-8"?>'
50
+ '<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/">'
51
+ '<metadata>'
52
+ '<trk>'
53
+ '<name>trackName'
54
+ '<desc>'
55
+ '<trkseg>'
56
+ '<trkpt lat="38.31014" lon="140.83413"><ele>標高値'
57
+ '<trkpt lat="38.30989" lon="140.83424"><ele>標高値'
58
+ '............'
59
+ '<trkpt lat="38.22267" lon="140.86045"><ele>標高値'
60
+ ''
61
+ ''
62
+
63
+ //----------------------------------------------------------------------------
64
+ function addElevation(track) {
65
+ // Elevation add >>>>>>>>>>>>>>>>>>>>>>
66
+ return Promise.all(track.features
67
+ .filter(function (feature) { return feature.geometry.type == "LineString" })
68
+ .map(function (feature) {
69
+ let coordinates = feature.geometry.coordinates;
70
+ return Promise.all(coordinates.map(function (coordinate, i) {
71
+ lat = coordinate[1];
72
+ lng = coordinate[0];
73
+ //console.log(lat + ',' + lng);
74
+ // 地理院地図サーバから標高を求める
75
+ // http://maps.gsi.go.jp/development/elevation_s.html
76
+ // https://2ndart.hatenablog.com/entry/2021/04/29/143307
77
+ var src = 'https://cyberjapandata2.gsi.go.jp/general/dem/scripts/getelevation.php?lon=' + lng + '&lat=' + lat;
78
+ return fetch(src)
79
+ .then((response) => {
80
+ return response.text();
81
+ })
82
+ .then((text) => {
83
+ var results = JSON.parse(text);
84
+ // Math.round() 関数は、引数として与えた数を四捨五入
85
+ var popStr = Math.round(results.elevation);
86
+ ///console.log(popStr);
87
+ coordinates[i].push(popStr);
88
+ })
89
+ }))
90
+ }))
91
+ };
92
+ //----------------------------------------------------------------------------
93
+ function exportGPX() {
94
+ let outputGpxText;
95
+ let sp2 = ' ';
96
+
97
+ addElevation(track).then(function () {
98
+ // routeline >>>>>
99
+ // https://stackoverflow.com/questions/51397890/how-to-use-polyline-decorator-in-leaflet-using-geojson-line-string
100
+ track.features
101
+ .filter(function (feature) { return feature.geometry.type == "LineString" })
102
+ .map(function (feature) {
103
+
104
+ let textGpx = '<?xml version="1.0" encoding="UTF-8"?>' + '\n' +
105
+ '<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' +
106
+ '<metadata>' + '\n' +
107
+ '<trk>' + '\n' +
108
+ '<name>' + 'trackName' + '' + '\n' +
109
+ '<desc>' + '\n' +
110
+ '<trkseg>' + '\n';
111
+ //------kokokara----
112
+ let coordinates = feature.geometry.coordinates;
113
+ console.log(coordinates); //<<<<<<< [1]ここでは3要素有る
114
+ coordinates[2].push('tuikasita');
115
+
116
+ coordinates.forEach(function (coordinate, i) {
117
+ textGpx = textGpx + sp2 + '<trkpt lat="' + coordinate[1] + '" lon="' + coordinate[0] + '">';
118
+
119
+ console.log(coordinate[0] + ' ' + coordinate[1] + ' ' + coordinate[2]);
120
+ // >>>>>>>>>>>>[2] ここでは3要素がundefinedになる
121
+
122
+ // データに標高が無い場合
123
+ if (coordinate.length == 2) {
124
+ textGpx = textGpx + '<ele>';
125
+ // データに標高が有る場合
126
+ } else if (coordinate.length == 3) {
127
+ textGpx = textGpx + '<ele>' + coordinate[2] + '';
128
+ }
129
+ textGpx = textGpx + '' + '\n';
130
+ })
131
+ textGpx = textGpx + '' + '\n' + ''
132
+
133
+ console.log(textGpx);
134
+ let outputGPXstyle = textGpx;
135
+
136
+ })
137
+
138
+ // Code Export >>>>>
139
+ /* https://qiita.com/kerupani129/items/99fd7a768538fcd33420
140
+ ※ [0].click():jQuery オブジェクトの [0] は HTMLElement オブジェクト。HTMLElement.click() を呼んでいる
141
+ https://www.sejuku.net/blog/67735
142
+ ----------------------------------------------*/
143
+ /*$('<a>', {
144
+ href: window.URL.createObjectURL(new Blob([outputGPXstyle], {type : 'text/plain'})),
145
+ download: 'fileName' + '.gpx'
146
+ })[0].click(); // ※ [0].click()*/
147
+
148
+ })
149
+ };
150
+ </script>
151
+ </body>
152
+
153
+ </html>
154
+ ```
155
+