Skip to content
Snippets Groups Projects
Select Git revision
  • 82a68f66651adcc4876612a49348ee52fd9931d9
  • main default protected
  • master
3 results

index.js

Blame
  • Forked from JYCHOI / react-starter
    Source project has a limited visibility.
    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);