Skip to content
Snippets Groups Projects
Select Git revision
  • 5725517f01aa7f6b9c63f45037f09bcb9dc75bd0
  • main default protected
2 results

weather.js

Blame
  • weather.js 944 B
    const API_KEY = "252e2176496ae4337bc32ae0587d6d78";
    const API_KEY2 = "241051bf13976dd3ddf8b8d9f247255e";
    //키 아직 활성화 안됨
    
    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_KEY2}&units=metric`;
        console.log(url);
        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);