|
| 1 | +let weather = { |
| 2 | + "apikey": "466ddaa21a8de191e9f608bd11a56acb", |
| 3 | + fetchWeather: function(city){ |
| 4 | + fetch( |
| 5 | + "https://api.openweathermap.org/data/2.5/weather?q=" |
| 6 | + + city |
| 7 | + + "&units=metric&appid=" |
| 8 | + + this.apikey |
| 9 | + ) |
| 10 | + .then((response) => response.json()) |
| 11 | + .then((data) => this.displayWeather(data)); |
| 12 | + }, |
| 13 | + |
| 14 | + displayWeather: function(data){ |
| 15 | + const{ name } = data; |
| 16 | + const{ icon, description } = data.weather[0]; |
| 17 | + const{ temp, humidity } = data.main; |
| 18 | + const{ speed } = data.wind; |
| 19 | + // console.log(name,icon,description,temp,humidity,speed); |
| 20 | + document.querySelector(".city").innerText = "Weather in " + name; |
| 21 | + document.querySelector(".icon").src = "https://openweathermap.org/img/wn/"+ icon +".png"; |
| 22 | + document.querySelector(".description").innerText = description; |
| 23 | + document.querySelector(".temp").innerText = temp +"°C"; |
| 24 | + document.querySelector(".humidity").innerText = "Humidity: "+ humidity + "%"; |
| 25 | + document.querySelector(".wind").innerText = "Wind speed: "+ speed + "km/h"; |
| 26 | + document.querySelector(".weather").classList.remove("loading"); |
| 27 | + document.body.style.backgroundImage = "url('https://source.unsplash.com/1600x900/?" + name + "')" |
| 28 | + }, |
| 29 | + search: function(){ |
| 30 | + this.fetchWeather(document.querySelector(".search-bar").value); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +let geocode = { |
| 35 | + reverseGeocode: function (latitude ,longitude) { |
| 36 | + var api_key = '089943fd115440dbb4d95b091479e834'; |
| 37 | + |
| 38 | + var api_url = 'https://api.opencagedata.com/geocode/v1/json' |
| 39 | + |
| 40 | + var request_url = api_url |
| 41 | + + '?' |
| 42 | + + 'key=' + api_key |
| 43 | + + '&q=' + encodeURIComponent(latitude + ',' + longitude) |
| 44 | + + '&pretty=1' |
| 45 | + + '&no_annotations=1'; |
| 46 | + |
| 47 | + // see full list of required and optional parameters: |
| 48 | + // https://opencagedata.com/api#forward |
| 49 | + |
| 50 | + var request = new XMLHttpRequest(); |
| 51 | + request.open('GET', request_url, true); |
| 52 | + |
| 53 | + request.onload = function() { |
| 54 | + // see full list of possible response codes: |
| 55 | + // https://opencagedata.com/api#codes |
| 56 | + |
| 57 | + if (request.status === 200){ |
| 58 | + // Success! |
| 59 | + var data = JSON.parse(request.responseText); |
| 60 | + weather.fetchWeather(data.results[0].components.city); |
| 61 | + |
| 62 | + } else if (request.status <= 500){ |
| 63 | + // We reached our target server, but it returned an error |
| 64 | + |
| 65 | + console.log("unable to geocode! Response code: " + request.status); |
| 66 | + var data = JSON.parse(request.responseText); |
| 67 | + console.log('error msg: ' + data.status.message); |
| 68 | + } else { |
| 69 | + console.log("server error"); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + request.onerror = function() { |
| 74 | + // There was a connection error of some sort |
| 75 | + console.log("unable to connect to server"); |
| 76 | + }; |
| 77 | + |
| 78 | + request.send(); // make the request |
| 79 | + }, |
| 80 | + |
| 81 | + getLocation: function(){ |
| 82 | + function success(data){ |
| 83 | + geocode.reverseGeocode(data.coords.latitude, data.coords.longitude); |
| 84 | + |
| 85 | + |
| 86 | + if(navigator.geolocation){ |
| 87 | + navigator.geolocation.getCurrentPosition(success, console.error); |
| 88 | + } |
| 89 | + else{ |
| 90 | + weather.fetchWeather("denver"); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +document.querySelector(".search button").addEventListener("click", function () { |
| 97 | + weather.search(); |
| 98 | +}); |
| 99 | + |
| 100 | +document.querySelector(".search-bar").addEventListener("keyup", function(event) { |
| 101 | + if(event.key == 'Enter'){ |
| 102 | + weather.search(); |
| 103 | + } |
| 104 | +}); |
| 105 | + |
| 106 | + |
| 107 | +geocode.getLocation(); |
0 commit comments