22.07.18 날씨 api 사용
const weather = document.querySelector("#weather span:first-child") // 인덱스 화일 const city = document.querySelector("#weather span:last-child")
const API_KEY ="69ccc4d6a092c24097b0d0c925e936bb"; // 이건 사이트에서 받은 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`;
fetch(url)
.then((response) => response.json())
.then((data) => {
city.innerText = data.name;
weather.innerText = `${data.weather[0].main} /${data.main.temp}`;
});
}
function onGeoError(){
alert("위치를 읽을 수 없습니다")
}
navigator.geolocation.getCurrentPosition(onGeoOk,onGeoError); //현재 위치를 알려주는 기능