Home

Belgian railway schedules Async/Await

loading...


const t=document.getElementById("buttons"),n=document.getElementById("preloader"),e={lat:51.162074,lng:4.99088};function a(t=!0,e=""){n.style.visibility=t?"hidden":"visible",n.querySelector("p").textContent=e}navigator.geolocation?navigator.geolocation.getCurrentPosition(n=>{e.lat=n.coords.latitude,e.lng=n.coords.longitude,async function(){a(!1,"Loading stations ...");const n=await fetch("https://api.irail.be/stations/?format=json&lang=en");if(!n.ok)throw new Error(`An error has occurred: ${n.status}  ${n.statusText}`);let o=await n.json();o=function(t){return t.station.map(t=>{const n=+t.locationY,a=+t.locationX;return{lat:n,lng:a,distance:function(t,n,e,a){const o=(e-t)*Math.PI/180,i=(a-n)*Math.PI/180,r=t*Math.PI/180,s=e*Math.PI/180,c=Math.sin(o/2)*Math.sin(o/2)+Math.sin(i/2)*Math.sin(i/2)*Math.cos(r)*Math.cos(s),l=2*Math.atan2(Math.sqrt(c),Math.sqrt(1-c));return Math.round(6371*l*100)/100}(n,a,e.lat,e.lng),id:t.id,name:t.name}})}(o),o=o.sort((t,n)=>t.distance-n.distance).slice(0,5),console.table(o),function(n){n.forEach(n=>{t.innerHTML+=`<button data-id="${n.id}">${n.name}</button>`}),t.querySelectorAll("button").forEach(t=>{t.addEventListener("click",(function(t){const n=this.dataset.id;document.getElementById("station").textContent=this.textContent,async function(t,n){a(!1,`Loading timetable for ${n}...`);const e=`https://api.irail.be/liveboard/?id=${t}&format=json&lang=en`,o=await fetch(e);let i=(await o.json()).departures.departure;i=function(t){return t.map(t=>{const n=new Date(1e3*+t.time).toTimeString().substring(0,5);let e="",a="border-green";return"0"!==t.delay&&(e="+"+t.delay/60,a="border-red"),{to:t.station,platform:t.platform,delay:e,color:a,departure:n}})}(i),function(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}(i),a()}(n,this.textContent)}))})}(o),t.querySelector("button:first-child").dispatchEvent(new Event("click")),a()}().catch(t=>{a(),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