์๊ฐ๊ณผ ์ฌ์ฉ์์ ์ด๋ฆ, To Do List๋ฅผ ๊ฐ๊ณ ์๋ ํ์ฌ์ momentum์, ์ฌ์ฉ์์ ์์น๋ฅผ ๋ฐ์ ํด๋น ์์น์ ๋ ์จ๋ฅผ ์ถ๋ ฅํ๋๋ก ํ๋ค.
์ฌ์ฉ์์ ์์น๋ navigator.geolocation.getCurrentPosition()
์ ํตํด ๋ฐ์์ค๊ณ , ๋ ์จ๋ openweathermap์ ํตํด ๋ฐ์์๋ค.
navigator๋ ์ฌ์ฉ์ ์์ด์ ํธ (์น์ ๊ฒฝ์ฐ ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ปํจ) ์ ์ํ๋ ์ ์ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ฉฐ, ๋ค์ํ ์์ฑ์ ๋ฐํํ๋ค. ์ฐธ๊ณ ๋ก ํ์ค ์์ฑ์ ์ ๋ถ Read only
๋ก ๋ฐํ๋๋ค.
๊ทธ ์ค ๋์๊ฒ ํ์ํ ๊ฒ์ ์ฌ์ฉ์์ ์์น ์ด๋ฏ๋ก geolocation์ ํตํด ์ฌ์ฉ์์ ์์น๋ฅผ ๋ฐ๋๋ก ํ๋ค.<script src="weather.js"></script>
๋ฅผ ํตํด weather.js
๋ฅผ ๋ถ๋ฌ์ค๋๋ก ํ๊ณ ์์ฑํ๋ค.
init()
์์๋ loadCoords()
๋ฅผ ์คํํ๋๋ก ํ์๊ณ , ํด๋น ํจ์์์๋ const loadedCoords = localStorage.getItem("coords");
๋ฅผ ํตํด LS์์ ์ง์ญ์ ๋ํ ์ ๋ณด๋ฅผ ๋ฐ์์ค๋๋ก ํ์๊ณ , if(loadCoords === null)
์ ํตํด ๋ง์ฝ ์ด๋ฏธ ์ ๋ณด๊ฐ ์๋ค๋ฉด askForCoords()
ํจ์๋ฅผ ์คํํ๋๋ก ํ์๊ณ , ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ ํด๋น ์ง์ญ์ ๋ ์จ๋ฅผ ๋ถ๋ฌ์ค๋๋ก ํ์๋๋ฐ, ์ ๋ณด๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ๋จผ์ ์ดํด๋ณด๋๋ก ํ๊ฒ ๋ค.
์ด ํจ์์์ navigator.geolocation.getCurrentPosition(handleGeoSuccess, handleGeoError)
์ ํตํด ์ฌ์ฉ์์ ํ์ฌ ์์น๋ฅผ ๋ฐํ ํ๋๋ก ํ์๋ค. getCurrentPosition๋ ๋งค๊ฐ ๋ณ์๋ฅผ 3๊ฐ ๋ฐ์ ์ ์๊ณ , success
๋ฅผ ์ ์ธํ ๋ ๋ณ์(error
,option
)์ ํ์๊ฐ ์๋๋ค. success
์ error
๋ ์ฝ๋ฐฑ ํจ์์ธ๋ฐ, success
์ ๊ฒฝ์ฐ๋ GeolocationPosition์ ์ ์ผํ ๋งค๊ฐ ๋ณ์๋ก ๋ฐ๊ณ , error
๋ GeolocationPositionError๋ฅผ ์ ์ผํ ๋งค๊ฐ ๋ณ์๋ก ๋ฐ๋๋ค.option
์์๋ ์ ํ๋์ ์๋ชจ์๊ฐ์ ์ถ๊ฐ์ ์ผ๋ก ์ง์ ํ ์ ์๋ค. ๋์ ๊ฒฝ์ฐ option
์ ์ฌ์ฉํ์ง ์์๊ณ , success
๋ handelGeoSuccess(position)
, error
๋ handleGeoError(err)
๋ก ์ฝ๋ฐฑํ๋๋ก ํ์๋ค. handleGeoError()
์์๋ ์ค๋ฅ ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋๋ก ํ๋ค.
handleGeoSuccess(position)
์์๋ position.coords
๋ฅผ ํตํด GeolocationCoordinates์ ๋ถ๋ฌ์ค๊ณ , position.coords.latitude
, position.coords.longitude
๋ฅผ ๊ฐ๊ฐ latitutde
,longitude
๋ก ์ ์ธํด์ฃผ์๋ค. ์ด latitude
, longitude
๋ฅผ coordsObj
๋ก ์ ์ธํด์ค ํ, ํด๋น ๊ฐ์ฒด๋ฅผ local storage์ ์ ์ฅํ๋ ํจ์ saveCoords(coordsObj)
๋ฅผ ํธ์ถํด์ฃผ์๋ค. ๋ค์์ coordsObj
์ ์ธ์ด๋ค.
const coordsObj = {
latitude,
longitude
};
๋ณดํต์ ์ ์ธ์ด๋ผ๋ฉด, latitude: latitude, longitude: longitude;
์ ๊ฐ์ด ์ ์ธํ๊ฒ ์ง๋ง, javascript์ ํน์ง ์ค ํ๋๋ก, ์ ์ธํ๊ณ ์ ํ๋ key์ value์ ๊ฐ์ด ๊ฐ์ ๊ฒฝ์ฐ, latitude, longitude;
๋ง ํด์ฃผ์ด๋ ๊ฐ์ ์๋ฏธ๋ฅผ ๊ฐ๋๋ค.
coordObj
๋ฅผ ์ธ์๋ก ๊ฐ๋ saveCoords()
๋ ์ด javascript ๊ฐ์ฒด๋ฅผ stringify
ํ์ฌ local storage์ ์ ์ฅํ๋ ๊ธฐ๋ฅ์ ํ๋ค. ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ก ์งํ๋๋ค.
localStorage.setItem("coords", JSON.stringify(coordsObj));
์ง๊ธ๊น์ง์ ๊ณผ์ ์ ๊ฑฐ์น๋ฉด, local storage์ ์๋ ์ฌ์ง๊ณผ ๊ฐ์ด coords
๊ฐ ์ ์ฅ๋๋ค.
ํ์ง๋ง, ๋์ ๊ฒฝ์ฐ ์ฒ์๋ถํฐ ์ ์ฅ์ด ๋ ๊ฒ์ ์๋์๋๋ฐ, ์๋ ์ฌ์ง์ฒ๋ผ ์์น ์ฌ์ฉ ์ฌ๋ถ๋ฅผ ์ง๋ฌธํ๋ ํ์
์ ๋ด์ง๋ง, ์ฌ์ง์ด ํ์ฉํ์ง ์์ผ๋ฉด handleGeoError
๋ ์คํ๋์ง๋ง,
ํ์ฉ์ ๋๋ฅด๋ ๊ฒฝ์ฐ ์งํ๋์ด์ผ ํ coords์ ์ ์ฅ์ ์ด๋ฃจ์ด์ง์ง ์์๋ค. ๋ฐ๋ผ์ ๊ฐ ํจ์์ console.log("(ํจ์์ด๋ฆ) works")
๋ฅผ ํตํด ์ด๋๊น์ง ์งํ์ด ๋๋๊ฐ ํ์ธ์ ํด๋ณด์๊ณ , askForCoords()
๊น์ง ์ ์ ์งํ๋์ง๋ง, "ํ์ฉ"์ ๋๋ฅธ ํ ์งํ๋์ง ์์๋ค. ๊ตฌ๊ธ๋ง์ ํ๋ฉฐ ์ด๊ฒ์ ๊ฒ ์๋ํ์ง๋ง, ์๋๋์ง ์์๊ณ , ํน์๋ ์ถ์ด ํ์ธํ ์ค์ ์ด ๋ต์ด์๋ค.
... ์ด์ ๋ ์ ์ ์์ง๋ง, ํด๋น ์ค์ ์ ํ์ฑํ๋ฅผ ํด์ฃผ์ด๋ ๊ณ์ ํด์ ๋๊ณ , ํ์ํ ๋๋ง๋ค ์ฒดํฌ๋ฅผ ๋ค์ ํด์ค ๋ค ์ฌ์ฉํ๋ค.
ํด๋น ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ํ, ์ฌ์ฉ์์ ์๋์ ๊ฒฝ๋๋ฅผ ์ ์ฅํ์ผ๋, ํด๋น ์๋์ ๊ฒฝ๋์ ๋ ์จ๋ฅผ ํ์ด์ง์ ์ถ๋ ฅํ๋๋ก ํ๋ ์์
์ ํ๋ค. ์ฌ์ฉํ API๋ openweathermap์ด๊ณ , ์ฌ์ฉ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค๋ฅด์ง๋ง ๋์ ๊ฒฝ์ฐ๋ ๋ฌด๋ฃ๋ก ์ด์ฉํ๋ค. Divide๋ฅผ ์ํด loadCoords()
์์ local storage์ "coords"๊ฐ ์๋ ๊ฒฝ์ฐ, ์๋ ๊ฒฝ์ฐ askForCoords()
๋ฅผ ์คํํ๋ ๊ฒ๊ณผ ๋ฌ๋ฆฌ, ์๋ ์ฝ๋๋ฅผ ํตํด getWeather()
์ด ์คํ๋๋๋ก ํ๋ค.
const parsedCoords = JSON.parse(loadedCoords);
getWeather(parsedCoords.latitude,parsedCoords.longitude);
๋ํ, index.html
์ <span class="js-weather"></span>
๋ฅผ ์ถ๊ฐํ์ฌ innerText
๋ฅผ ํตํด ์ํ๋ ๊ฐ์ ์ถ๋ ฅ ๊ฐ๋ฅํ ๋ก ํ์๊ณ , const weather = document.querySelector(".js-weather");
,
const API_KEY = "//๋์ API_KEY//";
๋ฅผ ํตํด weather์ API_KEY๋ฅผ ๋ฏธ๋ฆฌ ์ ์ธํ๋ค.
API๋ฅผ ์ฐ๊ฒฐํ๊ธฐ ์ํด์ getWeather(lat,lng)
์์ ๊ฐ์ฅ ๋จผ์ ํ ๊ฒ์ fetch()
์๋ค. ์ ํํ๋ WindowOrWorkerGlobalScope.fetch()๋ก, ์ธ์๋ก์ ์ ๋ฌ๋ request
๋ฅผ ์ํํ๋ ์ญํ ์ ํ๋ค. ๋ด๊ฐ ์๊ตฌํ request
๋ ๋ค์ ์ฌ์ง๊ณผ ๊ฐ๋ค.
fetch()
๋ฅผ ํตํด request
๋ฅผ ์ ๋ฌํ์ผ๋, ๋ค์์ response
๋ฅผ ์ฝ์ด์ json()
์ ํตํด JSON์ ํํ๋ก ๋ณํํ์ฌ return
ํ์ฌ innerText()
๋ก ์ถ๋ ฅํด์ฃผ๋ฉด ๋๋ค.
ํ์ง๋ง, ๋น๋๊ธฐ ๋ฐฉ์์ผ๋ก ์๋ํ๋ API์ response
๊ฐ ๋๋ฌํ๊ธฐ ์ ์ json()
์ด ์คํ๋๋ค๋ฉด, ์ฝ์ด์ฌ response
๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค. ๋ฐ๋ผ์ then()
์ ํตํด ์งํํ๋๋ฐ, ์ด ํจ์๋ API์ ๋น์ทํ promise๋ฅผ ํตํด ์ฑ๊ณต์ผ๋ก ํด๋น promise
๊ฐ ์๋ฃ๋ ๊ฒฝ์ฐ ์ธ์๋ก ์ ๋ฌ๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ๋๋ก ํ๋ค. ์ถํ YDKJS์์ ์์ธํ ๋ค๋ฃจ๋๋ก ํ๊ฒ ๋ค. ๊ฒฐ๊ตญ, getWeather()
์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑ๋๋ค.
function getWeather(lat, lng) {
fetch(`https://api.openweathermap.org/data/2.5/weather?
lat=${lat}&lon=${lng}&appid=${API_KEY}&units=metric`
).then(function(response){
return response.json();
}).then(function(json){
const temperature = json.main.temp;
const place = json.name;
weather.innerText = `${temperature}ยฐC @ ${place}`;
});
}
fetch()
๊ฐ ์๋ฃ๋๋ฉด, response
๋ฅผ ์ธ์๋ก ํ๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ์ฌ, ํด๋น response
๋ฅผ JSON ํํ๋ก ๋ฐ๊พธ์ด์ฃผ๊ณ , ํด๋น ํจ์๊ฐ ์๋ฃ๋๋ฉด, ํด๋น json
์ ์ธ์๋ก ํ์ฌ temperature
๊ณผ place
๋ฅผ ์ ์ธํด์ฃผ๊ณ , innerText
๋ฅผ ํตํด <span>
์ ๋ด์ฉ์ ์์ ํ๋๋ก ํ์๋ค.