Belgian railway schedules promises
loading...
const t=document.getElementById("buttons"),e=document.getElementById("preloader");let n=[];const o={lat:51.162074,lng:4.99088};navigator.geolocation?navigator.geolocation.getCurrentPosition(a=>{o.lat=a.coords.latitude,o.lng=a.coords.longitude,e.style.visibility="visible",e.querySelector("p").textContent="Loading stations ...",fetch("https://api.irail.be/stations/?format=json&lang=en").then(t=>t.json()).then(t=>(console.log("unordered list of railway stations",t),e.querySelector("p").textContent="Find closest station ...",t.station.map(t=>{const e=+t.locationY,n=+t.locationX;return{lat:e,lng:n,distance:function(t,e,n,o){const a=(n-t)*Math.PI/180,i=(o-e)*Math.PI/180,s=t*Math.PI/180,l=n*Math.PI/180,r=Math.sin(a/2)*Math.sin(a/2)+Math.sin(i/2)*Math.sin(i/2)*Math.cos(s)*Math.cos(l),c=2*Math.atan2(Math.sqrt(r),Math.sqrt(1-r));return Math.round(6371*c*100)/100}(e,n,o.lat,o.lng),id:t.id,name:t.name}}))).then(t=>{const e=t.sort((t,e)=>t.distance-e.distance);return console.log("ordered list of railway stations",e),e}).then(o=>{n=o.slice(0,5),console.table(n),n.forEach(e=>{t.innerHTML+=`<button data-id="${e.id}">${e.name}</button>`}),t.querySelectorAll("button").forEach(t=>{t.addEventListener("click",(function(t){const n=this.dataset.id;document.getElementById("station").textContent=this.textContent,function(t,n){e.style.visibility="visible",e.querySelector("p").textContent=`Loading timetable for ${n}...`,fetch(`https://api.irail.be/liveboard/?id=${t}&format=json&lang=en`).then(t=>t.json()).then(t=>{const e=t.departures.departure;return console.log("timetable",e),e.map(t=>{const e=new Date(1e3*+t.time).toTimeString().substring(0,5);let n="",o="border-green";return"0"!==t.delay&&(n="+"+t.delay/60,o="border-red"),{to:t.station,platform:t.platform,delay:n,color:o,departure:e}})}).then(t=>{let n="";t.forEach(t=>{n+=`\n <div class="${t.color}">\n <span>${t.departure}</span>\n <span>${t.delay}</span>\n <span>${t.platform}</span>\n <span>${t.to}</span>\n </div> \n `}),document.getElementById("timetable").innerHTML=n,e.style.visibility="hidden"}).catch(t=>alert(t))}(n,this.textContent)}))}),t.querySelector("button:first-child").dispatchEvent(new Event("click")),e.style.visibility="hidden"}).catch(t=>{console.log(t)})},t=>{alert("Unable to retrieve your location")},{enableHighAccuracy:!0,timeout:3e3}):alert("Geolocation is not supported by this browser");
//# sourceMappingURL=irail.js.map