|
| 1 | +// Tutorial by http://youtube.com/CodeExplained |
| 2 | +// api key : 82005d27a116c2880c8f0fcb866998a0 |
| 3 | + |
1 | 4 | // SELECT ELEMENTS |
2 | 5 | const iconElement = document.querySelector(".weather-icon"); |
3 | 6 | const tempElement = document.querySelector(".temperature-value p"); |
4 | 7 | const descElement = document.querySelector(".temperature-description p"); |
5 | 8 | const locationElement = document.querySelector(".location p"); |
6 | 9 | const notificationElement = document.querySelector(".notification"); |
7 | 10 |
|
8 | | -// OUR APPLICATION DATA |
| 11 | +// App data |
9 | 12 | const weather = {}; |
10 | 13 |
|
11 | 14 | weather.temperature = { |
12 | 15 | unit : "celsius" |
13 | | -}; |
| 16 | +} |
14 | 17 |
|
15 | | -// APP CONSTANTS AND VARS |
| 18 | +// APP CONSTS AND VARS |
16 | 19 | const KELVIN = 273; |
17 | | - |
18 | 20 | // API KEY |
19 | 21 | const key = "82005d27a116c2880c8f0fcb866998a0"; |
20 | 22 |
|
21 | | -// CHECK IF BROWSER SUPPORT GEOLOCATION and GET LOCATION |
22 | | -if("geolocation" in navigator){ |
| 23 | +// CHECK IF BROWSER SUPPORTS GEOLOCATION |
| 24 | +if('geolocation' in navigator){ |
23 | 25 | navigator.geolocation.getCurrentPosition(setPosition, showError); |
24 | | - |
25 | 26 | }else{ |
26 | 27 | notificationElement.style.display = "block"; |
27 | | - notificationElement.innerHTML = "<p>Browser Doesn't Support Geolocation.</p>"; |
| 28 | + notificationElement.innerHTML = "<p>Browser doesn't Support Geolocation</p>"; |
28 | 29 | } |
29 | 30 |
|
30 | | -// SET POSITION |
| 31 | +// SET USER'S POSITION |
31 | 32 | function setPosition(position){ |
32 | | - console.log(position); |
33 | 33 | let latitude = position.coords.latitude; |
34 | 34 | let longitude = position.coords.longitude; |
35 | 35 |
|
36 | 36 | getWeather(latitude, longitude); |
37 | 37 | } |
38 | 38 |
|
39 | | -// GET WEATHER FROM API |
| 39 | +// SHOW ERROR WHEN THERE IS AN ISSUE WITH GEOLOCATION SERVICE |
| 40 | +function showError(error){ |
| 41 | + notificationElement.style.display = "block"; |
| 42 | + notificationElement.innerHTML = `<p> ${error.message} </p>`; |
| 43 | +} |
| 44 | + |
| 45 | +// GET WEATHER FROM API PROVIDER |
40 | 46 | function getWeather(latitude, longitude){ |
41 | 47 | let api = `http://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${key}`; |
| 48 | + |
42 | 49 | fetch(api) |
43 | 50 | .then(function(response){ |
44 | 51 | let data = response.json(); |
45 | 52 | return data; |
46 | | - |
47 | | - }).then(function(data){ |
48 | | - weather.temperature.value = Math.floor(data.main.temp - KELVIN); // Celsius |
| 53 | + }) |
| 54 | + .then(function(data){ |
| 55 | + weather.temperature.value = Math.floor(data.main.temp - KELVIN); |
49 | 56 | weather.description = data.weather[0].description; |
50 | 57 | weather.iconId = data.weather[0].icon; |
51 | 58 | weather.city = data.name; |
52 | 59 | weather.country = data.sys.country; |
53 | | - }).then(function(){ |
| 60 | + }) |
| 61 | + .then(function(){ |
54 | 62 | displayWeather(); |
55 | 63 | }); |
56 | 64 | } |
57 | 65 |
|
58 | | -// SHOW ERROR |
59 | | -function showError(error) { |
60 | | - notificationElement.style.display = "block"; |
61 | | - notificationElement.innerHTML = `<p> ${error.message} </p>`; |
| 66 | +// DISPLAY WEATHER TO UI |
| 67 | +function displayWeather(){ |
| 68 | + iconElement.innerHTML = `<img src="icons/${weather.iconId}.png"/>`; |
| 69 | + tempElement.innerHTML = `${weather.temperature.value}°<span>C</span>`; |
| 70 | + descElement.innerHTML = weather.description; |
| 71 | + locationElement.innerHTML = `${weather.city}, ${weather.country}`; |
| 72 | +} |
| 73 | + |
| 74 | +// C to F conversion |
| 75 | +function celsiusToFahrenheit(temperature){ |
| 76 | + return (temperature * 9/5) + 32; |
62 | 77 | } |
63 | 78 |
|
64 | | -// toggle fahrenheit ans celcius |
| 79 | +// WHEN THE USER CLICKS ON THE TEMPERATURE ELEMENET |
65 | 80 | tempElement.addEventListener("click", function(){ |
66 | 81 | if(weather.temperature.value === undefined) return; |
67 | | - if(weather.temperature.unit === "celsius"){ |
68 | | - |
| 82 | + |
| 83 | + if(weather.temperature.unit == "celsius"){ |
69 | 84 | let fahrenheit = celsiusToFahrenheit(weather.temperature.value); |
70 | 85 | fahrenheit = Math.floor(fahrenheit); |
71 | 86 |
|
72 | | - tempElement.innerHTML = `${fahrenheit}° <span>F</span>`; |
73 | | - weather.temperature.unit = "fahrenheit" |
| 87 | + tempElement.innerHTML = `${fahrenheit}°<span>C</span>`; |
| 88 | + weather.temperature.unit = "fahrenheit"; |
74 | 89 | }else{ |
75 | | - tempElement.innerHTML = `${weather.temperature.value}° <span>F</span>`; |
| 90 | + tempElement.innerHTML = `${weather.temperature.value}°<span>C</span>`; |
76 | 91 | weather.temperature.unit = "celsius" |
77 | 92 | } |
78 | 93 | }); |
79 | 94 |
|
80 | | -// display weather info to ui |
81 | | -function displayWeather(){ |
82 | | - |
83 | | - iconElement.innerHTML = `<img src="icons/${weather.iconId}.png"/>`; |
84 | | - tempElement.innerHTML = `${weather.temperature.value}° <span>C</span>`; |
85 | | - descElement.innerHTML = weather.description; |
86 | | - locationElement.innerHTML = `${weather.city}, ${weather.country}`; |
87 | | -} |
88 | | - |
89 | | -// °C to °F conversion |
90 | | -function celsiusToFahrenheit(temperature){ |
91 | | - // (0 °C × 9/5) + 32 = 32 °F |
92 | | - return (temperature * 9/5) + 32; |
93 | | -} |
94 | | - |
95 | | - |
96 | | - |
97 | | - |
98 | | - |
99 | | - |
100 | | - |
0 commit comments