cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
                  <div class="wrapper">
        <h1>Local Weather</h1>
        <div id="location"></div>
        <div id="temperature">
            <span id="temperature-value"></span>
            <a id="temperature-unit"></a>
        </div>
        <div id="description"></div>
        <img id="icon" />
        <div id="icon-attribution">Icons taken from
            <a href="https://www.amcharts.com/free-animated-svg-weather-icons/">amcharts.com/free-animated-svg-weather-icons</a> licensed under
            <a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>
        </div>
    </div>

            
          
!
            
              body {
  background-color: #212121;
  color: white;
  text-align: center;
  margin: 0;
}

h1 {
  font-size: 3rem;
}

#location,
#temperature,
#description {
  font-size: 1.5rem;
  line-height: 3rem;
}

#icon {
  width: auto;
  height: 250px;
}

#icon-attribution {
  font-size: 0.75rem;
}

#temperature-unit,
a {
  color: #2979ff;
}

            
          
!
            
              document.addEventListener("DOMContentLoaded", main);

let celsius = true;
let temp = 0;

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

function update_weather_icon(id, current_time, sunrise, sunset) {
  let day = current_time >= sunrise && current_time <= sunset;
  let icon_src = "";

  // documentation regarding the icons can be found at http://openweathermap.org/weather-conditions.

  if (id >= 200 && id < 300) {
    icon_src =
      "https://res.cloudinary.com/raphaeladdile/image/upload/s--aSnl_Jw_--/v1515194618/thunder_srzwaa.svg";
  } else if (id >= 300 && id < 400) {
    icon_src =
      "https://res.cloudinary.com/raphaeladdile/image/upload/s--yeTLFcMd--/v1515194570/rainy-7_sdbkyl.svg";
  } else if (id >= 500 && id < 600) {
    icon_src =
      "https://res.cloudinary.com/raphaeladdile/image/upload/s--lV_oG1pX--/v1515194565/rainy-6_pzlrlc.svg";
  } else if (id >= 600 && id < 700) {
    icon_src =
      "https://res.cloudinary.com/raphaeladdile/image/upload/s--EsqjgOhi--/v1515194606/snowy-6_zl9kwx.svg";
  } else if (id == 800) {
    icon_src = day
      ? "https://res.cloudinary.com/raphaeladdile/image/upload/s---6vDoixr--/v1515194528/day_shry4k.svg"
      : "https://res.cloudinary.com/raphaeladdile/image/upload/s--CxSp0zXi--/v1515194530/night_quuh8p.svg";
  } else if (id == 801) {
    icon_src = day
      ? "https://res.cloudinary.com/raphaeladdile/image/upload/s--pkzBuC_i--/v1515194500/cloudy-day-1_n3vykl.svg"
      : "https://res.cloudinary.com/raphaeladdile/image/upload/s--DdrT7Iph--/v1515194500/cloudy-night-1_ro8fb5.svg";
  } else if (id == 802) {
    icon_src = day
      ? "https://res.cloudinary.com/raphaeladdile/image/upload/s--lX8rXjWb--/v1515194500/cloudy-day-2_zqz0m3.svg"
      : "https://res.cloudinary.com/raphaeladdile/image/upload/s--PTiiuxLU--/v1515194500/cloudy-night-2_oeyhlt.svg";
  } else if (id == 803) {
    icon_src = day
      ? "https://res.cloudinary.com/raphaeladdile/image/upload/s--UzcYzIJf--/v1515194500/cloudy-day-3_g9ovdi.svg"
      : "https://res.cloudinary.com/raphaeladdile/image/upload/s--6Fz0I_JA--/v1515194524/cloudy-night-3_eeeode.svg";
  } else if (id == 804) {
    icon_src =
      "https://res.cloudinary.com/raphaeladdile/image/upload/s--ivgWegRI--/v1515194500/cloudy_vqbnvk.svg";
  } else if (id >= 900 && id <= 902) {
    icon_src =
      "https://res.cloudinary.com/raphaeladdile/image/upload/s--yeTLFcMd--/v1515194570/rainy-7_sdbkyl.svg";
  } else {
    icon_src =
      "https://res.cloudinary.com/raphaeladdile/image/upload/s--ivgWegRI--/v1515194500/cloudy_vqbnvk.svg";
  }

  // ignoring id 903+

  let icon_element = document.getElementById("icon");
  icon_element.src = icon_src;
}

function update_temperature(weather_data) {
  console.log(weather_data);

  let location_element = document.getElementById("location");
  location_element.innerText =
    weather_data.name + ", " + weather_data.sys.country;

  temp = weather_data.main.temp;

  let temperature_value_element = document.getElementById("temperature-value");
  temperature_value_element.innerText = Math.round(
    celsius ? temp : temp * 1.8 + 32
  );

  let temperature_unit_element = document.getElementById("temperature-unit");
  temperature_unit_element.innerText = celsius ? "°C" : "°F";

  let description_element = document.getElementById("description");
  description_element.innerText = weather_data.weather[0].description;

  update_weather_icon(
    weather_data.weather[0].id,
    weather_data.dt,
    weather_data.sys.sunrise,
    weather_data.sys.sunset
  );
}

async function geoFindMe() {
  while (!navigator.geolocation) {
    console.log("Geolocation is not supported by your browser");
    await sleep(2000);
  }

  function success(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    const request_url =
      "https://fcc-weather-api.glitch.me/api/current?lat=" +
      latitude +
      "&lon=" +
      longitude;

    fetch(request_url)
      .then(response => response.json())
      .then(update_temperature);
  }

  function error() {
    console.log("Unable to retrieve your location");
  }

  navigator.geolocation.getCurrentPosition(success, error);
}

function update_weather() {
  geoFindMe();
}

function change_temperature_unit() {
  celsius = !celsius;

  let temperature_value_element = document.getElementById("temperature-value");
  temperature_value_element.innerText = Math.round(
    celsius ? temp : temp * 1.8 + 32
  );

  let temperature_unit_element = document.getElementById("temperature-unit");
  temperature_unit_element.innerText = celsius ? "°C" : "°F";
}

async function main() {
  let temperature_unit_element = document.getElementById("temperature-unit");
  temperature_unit_element.addEventListener(
    "click",
    change_temperature_unit,
    false
  );

  while (true) {
    update_weather();
    await sleep(600000);
  }
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console