02.16(복습)

홍왕열·2022년 2월 16일
0

날씨 API = https://openweathermap.org/

위치를 나타내주는 매소드

//navigator.geolocation.getCurrentPosition(잘 되었을 때, error가 났을 때)

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=${ApiKey}&units=metric`
    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}도`
    }));
}
function onGeoError(){
    alert("Can't find you. No weather for you.")
}


navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError)

console.log(position)으로 안에 무엇이 들어와있는지를 확인한 후, 이 메소드가 무엇을 보내주는지 확인.

profile
코딩 일기장

0개의 댓글