사용자의 geolocation(위치)를 주고 날씨를 보여준다.
navigator.geolocation.getCurrentPosition()const API_KEY = "1545879515889df25c23857021328cf1";
function onGeoOK(position) {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
console.log("You live in", lat, lon);
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) => {
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}`;
}); //js가 url 부름, promise(시간이 좀 걸린 뒤에 일어나는 것)
}
function onGeoError() {
alert("Can't find you. No weather for you.");
}
navigator.geolocation.getCurrentPosition(onGeoOK, onGeoError);