Answers for "JavaScript Weather App"

3

build a weather app javascript

// Full tutorial: https://www.youtube.com/watch?v=WZNG8UomjSI

fetch(
  "https://api.openweathermap.org/data/2.5/weather?q=" +
    city +
    "&units=metric&appid=" +
    apiKey
)
  .then((response) => response.json())
  .then((data) => this.displayWeather(data));
Posted by: Guest on June-01-2021
0

JavaScript Weather App

// Declaring the variables
let lon;
let lat;
let temperature = document.querySelector(".temp");
let summary = document.querySelector(".summary");
let loc = document.querySelector(".location");
let icon = document.querySelector(".icon");
const kel = 273;
  
window.addEventListener("load", () => {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition((position) => {
      console.log(position);
      lon = position.coords.longitude;
      lat = position.coords.latitude;
  
      // API ID
      const api = "YOUR API KEY";
  
      // API URL
      const base =
`http://api.openweathermap.org/data/2.5/weather?lat=${lat}&` +
`lon=${lon}&appid=YOUR API KEY`;
  
      // Calling the API
      fetch(base)
        .then((response) => {
          return response.json();
        })
        .then((data) => {
          console.log(data);
          temperature.textContent = 
              Math.floor(data.main.temp - kel) + "°C";
          summary.textContent = data.weather[0].description;
          loc.textContent = data.name + "," + data.sys.country;
          let icon1 = data.weather[0].icon;
          icon.innerHTML = 
              `<img src="https://openweathermap.org/img/w/${icon1}.png">`;
        });
    });
  }
});
Posted by: Guest on September-19-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language