https://nomadcoders.co/javascript-for-beginners
바닐라 JS로 크롬 앱 만들기 – 노마드 코더 Nomad Coders
Javascript for Beginners
nomadcoders.co
<!--index.html-->
<!--생략-->
<div id="weather">
<span></span>
<span></span>
</div>
<!--생략-->
//weather.js
const API_KEY = "자기 api키 넣기";
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`;
//javascript가 url을 호출하고 response를 받음 + response의 json을 얻음
//내용을 추출했으면 data를 얻음
fetch(url)
.then(response => response.json())
.then(data => {
const weather = document.querySelector("#weather span:first-child");
const city = document.querySelector("#weather span:last-child");
city.innerText = data.name;
weather.innerText = `${data.weather[0].main} / ${data.main.temp}ºC`;
});
}
function onGeoError() {
alert("Can't find you. No weather for you.");
}
navigator.geolocation.getCurrentPosition(onGeoOK, onGeoError);
'Study > Vanila JS' 카테고리의 다른 글
[노마드코더] 바닐라 JS로 그림 만들기 - project setup (0) | 2023.08.22 |
---|---|
[Javascript][개념정리] Array.filter() (0) | 2023.01.12 |
[노마드코더] 바닐라 JS로 크롬 앱 만들기 - todo리스트 만들기 (0) | 2022.12.20 |
[노마드코더] 바닐라 JS로 크롬 앱 만들기 - 로그인 폼 (0) | 2022.12.19 |
[노마드코더] 바닐라 JS로 크롬 앱 만들기 - 랜덤 사진 띄우기 (0) | 2022.12.15 |