Last active
July 12, 2020 14:29
-
-
Save chaerynny/5df28dd0fb99e2ce39e0aeadfa070c68 to your computer and use it in GitHub Desktop.
openWeatherMap api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 현재 위치 정보 가져오기 | |
navigator.geolocation.getCurrentPosition(position => { | |
latitude = position.coords.latitude; | |
longtitude = position.coords.longitude; | |
// fetch 이용하여 API와 통신 | |
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longtitude}&appid=myapikeynumbers&units=metric`) | |
.then(function(response) { | |
return response.json(); | |
}) | |
.then(function(json) { | |
console.log(json); | |
// HTML에 보여주기 | |
const location = document.querySelector('#location'); | |
const weather = document.querySelector('#weather'); | |
location.innerHTML = `${Math.floor(json.main.temp)}° `; // 온도를 정수로 변환 | |
weather.innerHTML = `${json.name}`; | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment