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); //현재 위치를 알려주는 기능
'하찮은 개발일지' 카테고리의 다른 글
쿵쿵따만들기 (0) | 2022.10.02 |
---|---|
css 배경이미지 설정 (0) | 2022.09.25 |
2022.07.18 버튼을 눌러 삭제하는 기능 (0) | 2022.07.18 |
22.07.15 백그라운드 외.. (0) | 2022.07.15 |
2022.07.14 clock. (0) | 2022.07.14 |