|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=\, initial-scale=1.0" /> |
| 6 | + <meta http-equiv="X-UA-Compatible" content="ie=edge" /> |
| 7 | + <link |
| 8 | + rel="stylesheet" |
| 9 | + href=" https://unpkg.com/[email protected]/dist/leaflet.css" |
| 10 | + integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" |
| 11 | + crossorigin="" |
| 12 | + /> |
| 13 | + <script |
| 14 | + src=" https://unpkg.com/[email protected]/dist/leaflet.js" |
| 15 | + integrity="sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg==" |
| 16 | + crossorigin="" |
| 17 | + ></script> |
| 18 | + <style> |
| 19 | + #issMap { |
| 20 | + height: 180px; |
| 21 | + } |
| 22 | + </style> |
| 23 | + |
| 24 | + <title>Fetch JSON from API and map lat lon</title> |
| 25 | + </head> |
| 26 | + <body> |
| 27 | + <h1>Where is the ISS?</h1> |
| 28 | + |
| 29 | + <p> |
| 30 | + latitude: <span id="lat"></span>°<br /> |
| 31 | + longitude: <span id="lon"></span>° |
| 32 | + </p> |
| 33 | + |
| 34 | + <div id="issMap"></div> |
| 35 | + |
| 36 | + <script> |
| 37 | + // Making a map and tiles |
| 38 | + const mymap = L.map('issMap').setView([0, 0], 1); |
| 39 | + const attribution = |
| 40 | + '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'; |
| 41 | + |
| 42 | + const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'; |
| 43 | + const tiles = L.tileLayer(tileUrl, { attribution }); |
| 44 | + tiles.addTo(mymap); |
| 45 | + |
| 46 | + // Making a marker with a custom icon |
| 47 | + const issIcon = L.icon({ |
| 48 | + iconUrl: 'iss200.png', |
| 49 | + iconSize: [50, 32], |
| 50 | + iconAnchor: [25, 16] |
| 51 | + }); |
| 52 | + const marker = L.marker([0, 0], { icon: issIcon }).addTo(mymap); |
| 53 | + |
| 54 | + const api_url = 'https://api.wheretheiss.at/v1/satellites/25544'; |
| 55 | + |
| 56 | + let firstTime = true; |
| 57 | + |
| 58 | + async function getISS() { |
| 59 | + const response = await fetch(api_url); |
| 60 | + const data = await response.json(); |
| 61 | + const { latitude, longitude } = data; |
| 62 | + |
| 63 | + marker.setLatLng([latitude, longitude]); |
| 64 | + if (firstTime) { |
| 65 | + mymap.setView([latitude, longitude], 2); |
| 66 | + firstTime = false; |
| 67 | + } |
| 68 | + document.getElementById('lat').textContent = latitude.toFixed(2); |
| 69 | + document.getElementById('lon').textContent = longitude.toFixed(2); |
| 70 | + } |
| 71 | + |
| 72 | + getISS(); |
| 73 | + |
| 74 | + setInterval(getISS, 1000); |
| 75 | + </script> |
| 76 | + </body> |
| 77 | +</html> |
0 commit comments