๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

javascript/๋ฐ”๋‹๋ผ JS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ

DAY 14

 

#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);
}

 

 

https://openweathermap.org/

 

ะก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 

 

์นดํ…Œ๊ณ ๋ฆฌ API -  Current & Forecast weather data collection - Current Weather Data

https://openweathermap.org/current

 

 

ํšŒ์›๊ฐ€์ž…์„ ํ•˜๋ฉด, My API keys์—์„œ ๋‚ด key ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

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 ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ•ด๋‹น url์— ๋“ค์–ด๊ฐ€๋ฉด..

๊ทธ๋Ÿฌ๋‚˜ ์ž์„ธํžˆ ๋ณด๋ฉด ์˜จ๋„๋ฅผ ํ™”์”จ ์˜จ๋„(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