๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/Nomad Coders

[TIL] ๐Ÿ’ก Day 11 - JS๋กœ ์‚ฌ์šฉ์ž์˜ ์œ„์น˜ ์ •๋ณด์™€ ํ•ด๋‹น ์œ„์น˜์˜ ๋‚ ์”จ ํ‘œ์‹œํ•˜๊ธฐ

by vividmin 2022. 5. 2.
320x100

โœ” ์ด ํฌ์ŠคํŒ…์€ ๋…ธ๋งˆ๋“œ์ฝ”๋” - "๋ฐ”๋‹๋ผ JS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ๋ฅผ ๋“ค์œผ๋ฉด์„œ ๊นƒํ—ˆ๋ธŒ์— ๊ธฐ๋กํ•œ ํ•„๊ธฐ๋ฅผ ์˜ฎ๊ธด ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

๋ชฉํ‘œ : ๐ŸŒŽ์‚ฌ์šฉ์ž์˜ ์œ„์น˜ ์ •๋ณด์™€ โ›…ํ•ด๋‹น ์œ„์น˜์˜ ๋‚ ์”จ ํ‘œ์‹œํ•˜๊ธฐ



๐ŸŒฑ User์˜ ์œ„๋„(latitude) , ๊ฒฝ๋„(longitude) ๊ตฌํ•˜๊ธฐ

๐Ÿ‘‰ navigator ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉ์ž์˜ ์œ„์น˜๋ฅผ ์•Œ์•„๋‚ด๋Š” ์ฝ”๋“œ ์ž‘์„ฑ

  • mdn ๋งํฌ
  • JavaScript๊ฐ€ position์„ ํ†ตํ•ด์„œ user์˜ ์œ„์น˜๋ฅผ ์ „๋‹ฌํ•ด์คŒ.
  • position์€ object์ด๊ณ , ์œ„๋„, ๊ฒฝ๋„ ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.
  • position( )ํ•จ์ˆ˜๋Š” 2๊ฐœ์˜ ์ธ์ž๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
    • ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋์„ ๋•Œ์˜ ํ•จ์ˆ˜ (onGeoOk)
    • ์‹คํ–‰ ์‹คํŒจํ–ˆ์„ ๋•Œ์˜ ํ•จ์ˆ˜ (onGeoError)
    navigator.geolocation.getCurrentPosition(์‹คํ–‰โญ•ํ•จ์ˆ˜, ์‹คํ–‰โŒํ•จ์ˆ˜)
  • ์‹คํ–‰ ๐Ÿ™†๐Ÿป‍โ™€๏ธํ•จ์ˆ˜ (onGeoOk), ์‹คํ–‰๐Ÿ™…๐Ÿป‍โ™€๏ธ ํ•จ์ˆ˜ (onGeoError)๋กœ ์„ค์ •
  • ์˜ˆ์‹œ ์ฝ”๋“œ
    function onGeoOk(position){
        const lat = position.coords.latitude;
        const lng = position.coords.longitude;
        console.log("You live in", lat, lng);
    }

    function onGeoError() {
        alert("Can't find you. No weather for you.");
    }

    navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);



๐ŸŒฑ OpenweatherApi ์‚ฌ์šฉ ์ค€๋น„

๐Ÿ‘‰ OpenweatherApi ์ด์šฉํ•ด์„œ user์˜ ํ˜„์žฌ ์œ„์น˜, ๋‚ ์”จ Data ๋ฐ›์•„์˜ค๋„๋ก ์ค€๋น„ํ•˜๊ธฐ

  • https://openweathermap.org
  • ํšŒ์›๊ฐ€์ž… ํ›„ ๊ฐœ์ธ Api key ๋ฐœ๊ธ‰๋ฐ›๊ธฐ
  • https://openweathermap.org/api
  • ๋กœ๊ทธ์ธ ํ›„ api doc ๋ชฉ๋ก์œผ๋กœ ์ด๋™
  • Current Weather Data api๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์น˜, ๋‚ ์”จ, ์˜จ๋„ Data ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
  • ๐Ÿ’ก Tip!
    • JSON ๋ฐ์ดํ„ฐ๋ฅผ ํŽธํ•˜๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•
    • ํฌ๋กฌ ํ™•์žฅ ํ”„๋กœ๊ทธ๋žจ JSON Viewer ์„ค์น˜




๐ŸŒฑ OpenweatherApi ์‚ฌ์šฉํ•˜๊ธฐ

๐Ÿ‘‰ User์˜ ํ˜„์žฌ ์œ„์น˜, ๋‚ ์”จ Data ๋ฐ›์•„์˜ค๊ธฐ

  https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}
  • ์ด ์ฃผ์†Œ์— ์œ„์—์„œ ๊ตฌํ•œ lat, lon, api key๋ฅผ ๋„ฃ์€ ํ›„ ํฌ๋กฌ ์ฃผ์†Œ์ฐฝ์— ๊ฒ€์ƒ‰ํ•ด๋ณธ ๋’ค ๊ฐ’์ด ์ž˜ ๋‚˜์˜จ๋‹ค๋ฉด ๋‹ค์Œ ๊ณผ์ •์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด์ œ url์—์„œ ํ•„์š”ํ•œ ๊ฐ’์„ get ํ•ด๋ณผ ๊ฒƒ์ด๋‹ค.
    • ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ฃผ์†Œ๋กœ ์ด๋™ํ•ด์„œ ์‘๋‹ตํ•˜์—ฌ ๊ฐ’์„ ๋ณด์—ฌ์ค€๋‹ค.
    • javaScript๋Š” ๋งํฌ๋ฅผ ๋ˆ„๋ฅด๋Š” ๋Œ€์‹  fetch๋ฅผ ์‚ฌ์šฉํ•ด์„œ url์„ call ํ•ด์ค€๋‹ค.
  • ์–ป๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ
    • ์šฐ๋ฆฌ๊ฐ€ ์žˆ๋Š” ์žฅ์†Œ์˜ ์ด๋ฆ„ (์„œ์šธ, ๋ถ€์‚ฐ...)
    • ์žฅ์†Œ์˜ ๋‚ ์”จ (๋ง‘์Œ, ํ๋ฆ„, ๋น„...)
    • ๊ธฐ์˜จ (์ง€๊ธˆ์€ ํ™”์”จ์˜จ๋„๋กœ ๋˜์–ด์žˆ๋Š”๋ฐ, ์„ญ์”จ์˜จ๋„๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„œ api doc์„ ์‚ดํŽด๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.)
  • ํ™”์”จ(Fahrenheit) ์„ญ์”จ(Celsius) ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•
    • units์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
    • ๊ฐ๊ฐ imperial(ํ™”์”จ), metric(์„ญ์”จ), standard(์ ˆ๋Œ€์˜จ๋„)๋ฅผ ์˜๋ฏธ
    • url ๋์— &units=metric์„ ๋ถ™์ด๋ฉด ์„ญ์”จ์˜จ๋„๊ฐ€ ํฌํ•จ๋œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`;
  • fetch๊ฐ€ ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•˜์ง€ ์•Š์€ ๊ณผ์ •์ด๋‹ค
    • fetch๋Š” promise์ด๋‹ค.
    • promise๋Š” ๋‹น์žฅ ๋ญ”๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๊ณ  ์‹œ๊ฐ„์ด ์ข€ ์ง€๋‚œ ๋’ค์— ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์ด๋‹ค.
    • ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ๋‚ด์šฉ ์ถ”์ถœ Data ์–ป๊ธฐ ์ˆœ์„œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
    • ์•„๋ž˜์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ
    fetch(url).then(reponse => response.json()).then(data => {
     // ๊ธฐ๋‹ค๋ฆฐ ํ›„ ์‹คํ–‰ํ•˜๋ ค๋Š” ์ฝ”๋“œ ์ž…๋ ฅํ•˜๋Š” ๊ณณ
    })

    // ์˜ˆ์‹œ)
    fetch(url).then(reponse => response.json()).then(data => {
    console.log(data.name, data.weather[0].main)
    })

 

 

๐ŸŒฑ User์˜ ์œ„์น˜, ๋‚ ์”จ, ๊ธฐ์˜จ ์–ป๋Š” ๋ฐฉ๋ฒ•

๐Ÿ‘‰ ์ „์ฒด ์ฝ”๋“œ ์‚ดํŽด๋ณด๊ธฐ

  • json data๋ฅผ ํ†ตํ•ด ์–ป์„ ์ˆ˜ ์žˆ๋Š” Data
    • ์œ„์น˜ : data.name
    • ๋‚ ์”จ : data.weather[0].main
    • ์„ญ์”จ ๊ธฐ์˜จ : data.main.temp
  • html
  <div id="weather">
    <span></span>
    <span></span>
  </div>
  • javaScript
    const weather = document.querySelector("#weather span:first-child");
    const city = document.querySelector("#weather span:last-child");
    const API_KEY = "๊ฐ€์ž…ํ•˜๋ฉด์„œ ๋ฐ›์€ api key";


    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)
          .then((response) => response.json())
          .then((data) => {
          city.innerHTML = data.name;
          weather.innerText = `${data.weather[0].main} / ${data.main.temp}`;
          });
      console.log(url);
    }


    function onGeoError() {
    alert("Can't find you. No weather for you.");
    }

    navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€