Skip to content

Instantly share code, notes, and snippets.

@chaerynny
Last active July 12, 2020 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaerynny/5df28dd0fb99e2ce39e0aeadfa070c68 to your computer and use it in GitHub Desktop.
Save chaerynny/5df28dd0fb99e2ce39e0aeadfa070c68 to your computer and use it in GitHub Desktop.
openWeatherMap api
// 현재 위치 정보 가져오기
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)}°&nbsp`; // 온도를 정수로 변환
weather.innerHTML = `${json.name}`;
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment