#8.0 Geolocation
์ฐ์ ๋ ์จ๋ฅผ ์ป๊ธฐ ์ํด ๋ฏธ๋ฆฌ index.html์ ์ค๋น๋ฅผ ํด์ค๋ค.
<div id="weather">
<span></span>
<span></span>
</div>
<script src="js/weather.js"></script>
function onGeoOk(position){console.log(position);}
function onGeoError(){alert("Can't find you. no Weather for you.");}
navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);
์์ ์ฝ๋๋ก ๋ธ๋ผ์ฐ์ ์์๋ ๋ด ์์น๋ฅผ ๋ฐ์์จ๋ค.
๋ค๋ง getCurrentPosition()์๊ฒ๋ 2๊ฐ์ argument๊ฐ ํ์ํ๋ฐ, ํ๋๋ ๋ชจ๋ ๊ฒ ์ ์ฑ๊ณตํ์ ๋ ์คํ๋ ํจ์(onGeoOk), ๋ค๋ฅธ ํ๋๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ ๋ ์คํ๋ ํจ์(onGeoError)์ด๋ค. ์ฑ๊ณตํ์ ์์๋ ํด๋น ์์น ๊ฐ์ ๋ฐ์์์ผ ํ๋ฏ๋ก onGeoOk์ ์ธ์๋ฅผ ํ๋ ๋ฃ์ด์ค๋ค.
์ ์ด๋ฏธ์ง๋ฅผ ๋ณด๋ฉด ์๊ฒ ๋ฏ์ด, ์ฌ๋ฌ ์ ๋ณด ์ค์์๋ ๋ด ์์น์ ์๋์ ๊ฒฝ๋๋ฅผ ์ ํด์ฃผ๊ณ ์๋ค. ์ด ๊ฐ๋ค์ ์ฐ์ ๋ณ์์ ์ ์ฅํด๋๊ณ , ์ด๋ฅผ ์ด์ฉํด ๋ด ์์น์ ๋ ์จ๋ฅผ ์ป์ด์ฌ ๊ฒ์ด๋ค.
function onGeoOk(position){
const lat = position.coords.latitude;
const lng = position.coords.longitude;
console.log("You live in", lat, lng);
}
ะกurrent weather and forecast - OpenWeatherMap
Access current weather data for any location on Earth including over 200,000 cities! The data is frequently updated based on the global and local weather models, satellites, radars and a vast network of weather stations. how to obtain APIs (subscriptions w
openweathermap.org
์ ์ฌ์ดํธ์ ๋ ์จ API๋ก ๋ด ์ฌ์ดํธ์ ์ถ๋ ฅํ ๊ฒ์ด๋ค.
์ฐ์ ์ด์ฉํ๊ธฐ ์ํด์๋ ์ ์ฌ์ดํธ์ ๊ณ์ ์ ๋ง๋ค์ด๋ฌ์ผ ํ๋ค..
#8.1 Weather API
https://openweathermap.org/current
const API_KEY = "appkey~~~~";
function onGeoOk(position){
const lat = position.coords.latitude;
const lon = position.coords.longitude;
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}`;
fetch(url);
}
fetch(url);๋ก ์ง์ url์ ํธ์ถํ์ง ์๋๋ผ๋ fetch๋ก javascript์์ ๋์ url์ ๋ถ๋ฅด๋๋ก ํด์ค๋ค. ํ์ธ์ ๊ฐ๋ฐ์ ๋๊ตฌ - Network - fetch/xhr ์์ ํ์ธํ ์ ์๋ค.
๊ทธ๋ฌ๋ ์์ธํ ๋ณด๋ฉด ์จ๋๋ฅผ ํ์จ ์จ๋(standard)๋ก ํํํ๊ณ ์๋ค. ์ฐ๋ฆฌ๋ ์ญ์จ ์จ๋๋ฅผ ์๊ณ ์ถ์ผ๋ฏ๋ก metric์ผ๋ก ํด์ผ ํ๋ค.
์๋ฌดํผ ์ฐ๋ฆฌ๊ฐ ์ป๊ณ ์ถ์ ๊ฒ์ ํ์ฌ ์์น์ ํ์ฌ ๋ ์จ์ด๋ฏ๋ก, url ๋ค์ &units=metric์ ๋ถ์ธ๋ค.
fetch๋ฅผ ์ฌ์ฉํ๋ฉด ์๊ฐ์ด ์ข ๊ฑธ๋ฆฌ๊ธฐ ๋๋ฌธ์, response๋ฅผ ๋ฐ์ผ๋ฉด ํด๋น ๊ฐ์ json ํ์์ผ๋ก ๋ฐ์์ค๊ณ , ์ด ์ค ๋ฝ์๋ด๊ณ ์ถ์ data๋ฅผ ์ถ์ถํ๋ค.
function onGeoOk(position) {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`;
fetch(url)
// ์๋ต์ ๋ฐ์ผ๋ฉด json ํ์์ผ๋ก ๋ฐ์์ค๊ณ
.then((response) => response.json())
// ๋ฐ์์ค๋ฉด data๋ฅผ ๊ฐ์ ธ์จ๋ค
.then((data) => {
const city = document.querySelector("#weather span:first-child");
const weather = document.querySelector("#weather span:last-child");
city.innerText = data.name;
weather.innerText = data.weather[0].main;
});
}
๋ง์ฝ ๋ ์จ ๋ฟ๋ง์ด ์๋๋ผ ์จ๋๋ ๊ฐ์ ธ์ค๊ณ ์ถ์ผ๋ฉด weather.innerText๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ฉด ๋๋ค.
weather.innerText = `${data.weather[0].main} / ${data.main.temp}`;
'javascript > ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DAY 13 (0) | 2023.01.09 |
---|---|
DAY 12 (0) | 2023.01.09 |
DAY 11 (0) | 2023.01.05 |
DAY 10 (0) | 2023.01.04 |
DAY 8 (0) | 2023.01.03 |