1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| const COORDS = "coords"; const API_KEY = "60b305147a7e043321d283d3c83b4fa2"; const weather = document.querySelector(".js-weather");
function getWeather(latitude, longitude) { fetch( `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}&units=metric` ) .then(function (json) { return json.json(); }) .then(function (json) { console.log(json); const temperture = json.main.temp; const place = json.name; weather.innerText = `${temperture}'C @${place}`; });
}
function saveCoords(coordsObj) { localStorage.setItem(COORDS, JSON.stringify(coordsObj)); }
function handleGeoSuccess(position) { console.log("success"); const latitude = position.coords.latitude; const longitude = position.coords.longitude;
const coordsObj = { latitude, longitude, }; saveCoords(coordsObj); getWeather(latitude, longitude); } function handleGeoError() { console.log("error"); } function askForCoords() { navigator.geolocation.getCurrentPosition(handleGeoSuccess); }
function loadCoords() { const loadedCoords = localStorage.getItem(COORDS); if (loadedCoords === null) { askForCoords(); } else { const parseCoords = JSON.parse(loadedCoords); getWeather(parseCoords.latitude, parseCoords.longitude); } }
function init() { loadCoords(); }
init();
|