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

回答4件
あなたの回答
tips
プレビュー