-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.js
21 lines (18 loc) · 836 Bytes
/
weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const API_KEY = "2cbe22bd497f9e71186eb33db62739b6"
function onGeoSuccess(position) {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
const url = `http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${API_KEY}&units=metric`
fetch(url)
.then((response) => response.json())
.then((data) => {
const weatherContainer = document.querySelector("#weather span:first-child");
const city = document.querySelector("#weather span:last-child");
city.innerText = data.name;
weatherContainer.innerText = `${data.weather[0].main} / ${Math.round(data.main.temp)}'C`;
});
}
function onGeoError() {
alert("Can't find you. No weather for you.");
}
navigator.geolocation.getCurrentPosition(onGeoSuccess, onGeoError);