-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
50 lines (46 loc) · 1.75 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const form = document.querySelector("#weather-look-up")
const input = document.querySelector('#location')
const weatherTemp = document.querySelector('#temp')
const weatherOutlook = document.querySelector('#outlook')
const weatherPic = document.querySelector('.weatherpic')
function setGeoDataByCityName( cityName ) {
var key = '23140de0e11e66b4e3331b76ba89a7ef';
fetch('http://api.openweathermap.org/geo/1.0/direct?q=' + cityName + '&appid=' + key)
.then(function(resp) { return resp.json() })
.then(function(data) {
console.log(data);
let lat = data[0].lat;
let lon = data[0].lon;
setWeatherFromLatLon(lat, lon);
})
.catch(function() {
console.log('error');
});
}
function setWeatherFromLatLon( lat, lon ) {
var key = '23140de0e11e66b4e3331b76ba89a7ef';
console.log(lat);
fetch('http://api.openweathermap.org/data/2.5/weather?lat='+ lat + '&lon=' + lon + '&appid=' + key)
.then(function(resp) { return resp.json() }) // Convert data to json
.then(function(data) {
let outlook = data.weather[0].description
weatherTemp.innerHTML = "Outlook: " + outlook;
displayWeatherPicture(data.weather[0].icon);
weatherOutlook.innerHTML = "Feels like: " + (Math.round(data.main.feels_like - 273.15)) + "°C" ;
console.log(data);
})
.catch(function() {
// catch any errors
});
}
function displayWeatherPicture(icon) {
//const path = "img/" + String(num) + ".png";
const path = "http://openweathermap.org/img/wn/" + icon + ".png";
console.log(path)
weatherPic.innerHTML = '<img src="' + path + '" height="100px" width="100px">';
}
form.addEventListener("submit", e => {
e.preventDefault();
const inputVal = input.value;
setGeoDataByCityName(inputVal)
})