-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
189 lines (155 loc) · 5.96 KB
/
script.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//Declare a variable to store the searched city
var city="";
// variable declaration
var searchCity = $("#search-city");
var searchButton = $("#search-button");
var clearButton = $("#clear-history");
var currentCity = $("#current-city");
var currentTemperature = $("#temperature");
var currentHumidty= $("#humidity");
var currentWSpeed=$("#wind-speed");
var currentUvindex= $("#uv-index");
var sCity=[];
// searches the city to see if it exists in the entries from the storage
function find(c){
for (var i=0; i<sCity.length; i++){
if(c.toUpperCase()===sCity[i]){
return -1;
}
}
return 1;
}
//Set up the API key
var APIKey="a0aca8a89948154a4182dcecc780b513";
// Display the curent and future weather to the user after grabing the city form the input text box.
function displayWeather(event){
event.preventDefault();
if(searchCity.val().trim()!==""){
city=searchCity.val().trim();
currentWeather(city);
}
}
// Here we create the AJAX call
function currentWeather(city){
// Here we build the URL so we can get a data from server side.
var queryURL= "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&APPID=" + APIKey;
$.ajax({
url:queryURL,
method:"GET",
}).then(function(response){
// parse the response to display the current weather including the City name. the Date and the weather icon.
console.log(response);
//Dta object from server side Api for icon property.
var weathericon= response.weather[0].icon;
var iconurl="https://openweathermap.org/img/wn/"+weathericon +"@2x.png";
// The date format method is taken from the https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
var date=new Date(response.dt*1000).toLocaleDateString();
//parse the response for name of city and concanatig the date and icon.
$(currentCity).html(response.name +"("+date+")" + "<img src="+iconurl+">");
// parse the response to display the current temperature.
// Convert the temp to fahrenheit
var tempF = (response.main.temp - 273.15) * 1.80 + 32;
$(currentTemperature).html((tempF).toFixed(2)+"℉");
// Display the Humidity
$(currentHumidty).html(response.main.humidity+"%");
//Display Wind speed and convert to MPH
var ws=response.wind.speed;
var windsmph=(ws*2.237).toFixed(1);
$(currentWSpeed).html(windsmph+"MPH");
// Display UVIndex.
//By Geographic coordinates method and using appid and coordinates as a parameter we are going build our uv query url inside the function below.
UVIndex(response.coord.lon,response.coord.lat);
forecast(response.id);
if(response.cod==200){
sCity=JSON.parse(localStorage.getItem("cityname"));
console.log(sCity);
if (sCity==null){
sCity=[];
sCity.push(city.toUpperCase()
);
localStorage.setItem("cityname",JSON.stringify(sCity));
addToList(city);
}
else {
if(find(city)>0){
sCity.push(city.toUpperCase());
localStorage.setItem("cityname",JSON.stringify(sCity));
addToList(city);
}
}
}
});
}
// This function returns the UVIindex response.
function UVIndex(ln,lt){
//lets build the url for uvindex.
var uvqURL="https://api.openweathermap.org/data/2.5/uvi?appid="+ APIKey+"&lat="+lt+"&lon="+ln;
$.ajax({
url:uvqURL,
method:"GET"
}).then(function(response){
$(currentUvindex).html(response.value);
});
}
// Here we display the 5 days forecast for the current city.
function forecast(cityid){
var dayover= false;
var queryforcastURL="https://api.openweathermap.org/data/2.5/forecast?id="+cityid+"&appid="+APIKey;
$.ajax({
url:queryforcastURL,
method:"GET"
}).then(function(response){
for (i=0;i<5;i++){
var date= new Date((response.list[((i+1)*8)-1].dt)*1000).toLocaleDateString();
var iconcode= response.list[((i+1)*8)-1].weather[0].icon;
var iconurl="https://openweathermap.org/img/wn/"+iconcode+".png";
var tempK= response.list[((i+1)*8)-1].main.temp;
var tempF=(((tempK-273.5)*1.80)+32).toFixed(2);
var humidity= response.list[((i+1)*8)-1].main.humidity;
$("#fDate"+i).html(date);
$("#fImg"+i).html("<img src="+iconurl+">");
$("#fTemp"+i).html(tempF+"℉");
$("#fHumidity"+i).html(humidity+"%");
}
});
}
//Daynamically add the passed city on the search history
function addToList(c){
var listEl= $("<li>"+c.toUpperCase()+"</li>");
$(listEl).attr("class","list-group-item");
$(listEl).attr("data-value",c.toUpperCase());
$(".list-group").append(listEl);
}
// display the past search again when the list group item is clicked in search history
function invokePastSearch(event){
var liEl=event.target;
if (event.target.matches("li")){
city=liEl.textContent.trim();
currentWeather(city);
}
}
// render function
function loadlastCity(){
$("ul").empty();
var sCity = JSON.parse(localStorage.getItem("cityname"));
if(sCity!==null){
sCity=JSON.parse(localStorage.getItem("cityname"));
for(i=0; i<sCity.length;i++){
addToList(sCity[i]);
}
city=sCity[i-1];
currentWeather(city);
}
}
//Clear the search history from the page
function clearHistory(event){
event.preventDefault();
sCity=[];
localStorage.removeItem("cityname");
document.location.reload();
}
//Click Handlers
$("#search-button").on("click",displayWeather);
$(document).on("click",invokePastSearch);
$(window).on("load",loadlastCity);
$("#clear-history").on("click",clearHistory);