-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
152 lines (114 loc) · 4.46 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const form = document.getElementById('search-form');
const searchField = document.getElementById('search-key-word');
const responseContainer = document.getElementById('response-container');
const responseContainer2 = document.getElementById('response-container-2');
form.addEventListener('submit', function (e) {
e.preventDefault();
responseContainer.innerHTML = "";
searchedForText = searchField.value;
console.log(searchedForText);
//getWeatherData();
getCall();
});
function getCall() {
$.ajax({
type: "GET",
url: `https://maps.googleapis.com/maps/api/geocode/json?address=${searchedForText}&key=AIzaSyBtv8hBzVT3EDF39uVdN4oUxHGHEBOgpMI`,
dataType: 'json',
}).done(getLocation);
function getLocation(results) {
console.log(results);
const coordenates = results.results[0].geometry.location;
const lat = coordenates.lat;
const lng = coordenates.lng;
console.log(lat);
getWeatherData(lat, lng);
}
}
//problema con Access-Control-Allow, revisar consola. Solo se puede acceder instalando el pluggin de CORS
function getWeatherData(lat, lng) {
$.ajax({
type: "GET",
url: `https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/44f1b9aff98cc70b888435b1888ccba6/${lat},${lng}`,
/*origin:"https://api.darksky.net/forecast",
crossDomain : true,
Access-Control-Allow-Credentials: true,*/
dataType: 'json',
}).done(getWeather);
}
function getWeather(data) {
responseContainer2.innerHTML="";
console.log(data)
const temp = data.currently.apparentTemperature;
const summary = data.currently.summary;
const precip = data.currently.precipType;
const daily = data.daily.summary;
const weeklyInfo=data.daily.data;
console.log(temp);
const icon = data.currently.icon;
const humid = data.currently.humidity;
const wi = data.currently.windSpeed;
const uv = data.currently.uvIndex;
const press = data.currently.pressure;
//console.log(daily);
paintInfo(icon,humid,wi,uv,press,temp,summary,precip,daily)
weeklyInfo.forEach(function(day){
const time=day.time;
const max=day.apparentTemperatureMax;
const min=day.apparentTemperatureMin;
const datee=new Date(0);
datee.setUTCSeconds(time);
const realDate= datee.toLocaleString('en-US',{weekday:'short', day:'numeric'})
paintDays( realDate,max,min)
})
}
function paintInfo(icon,humid,wi,uv,press,temp,summary,precip,daily){
let name = document.createElement('h1');
let temperature = document.createElement('h2');
let hum = document.createElement('h3');
let wind = document.createElement('h3');
let uvIndex = document.createElement('h3');
let pressure = document.createElement('h3');
let summa = document.createElement('p');
let precipitation = document.createElement('span');
let daily1 = document.createElement('p');
let iconn = document.createElement('img');
iconn.innerHTML = icon;
name.innerText = searchedForText.toUpperCase();
temperature.innerText = temp + "ºF";
summa.innerText = "Sky: " + summary
precipitation.innerText = "Precipitation: " + precip;
daily1.innerText = daily;
hum.innerText = "Humidity: " + humid;
wind.innerText = "Wind Speed: " + wi;
uvIndex.innerText = "Uv Index: " + uv;
pressure.innerText = "Pressure: " + wi + " hPa";
responseContainer.appendChild(name);
responseContainer.appendChild(temperature);
responseContainer.appendChild(hum);
responseContainer.appendChild(wind);
responseContainer.appendChild(uvIndex);
responseContainer.appendChild(pressure);
responseContainer.appendChild(summa);
responseContainer.appendChild(precipitation);
responseContainer.appendChild(daily1);
responseContainer.appendChild(iconn);
}
function paintDays(realDate,max,min){
const date=document.createElement('p');
const maxTemp=document.createElement('span');
const minTemp=document.createElement('span');
date.innerHTML= realDate;
date.className="header";
minTemp.className="right";
maxTemp.className="right";
maxTemp.innerText=' '+'Max:'+ max+'º' + '-';
minTemp.innerText='Min:' + min+'º';
console.log(date);
date.appendChild(minTemp);
date.appendChild(maxTemp);
responseContainer2.appendChild(date);
}
/*
349687dbd574cdd20d242aa1f0461949 weather key
AIzaSyBtv8hBzVT3EDF39uVdN4oUxHGHEBOgpMI google key*/