-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
153 lines (120 loc) · 4.94 KB
/
Copy pathscript.js
File metadata and controls
153 lines (120 loc) · 4.94 KB
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
//Declare a variable to store the searched city
let city="";
// variable declaration
let searchCity = $("#search-city");
let searchButton = $("#search-button");
let clearButton = $("#clear-history");
let currentCity = $("#current-city");
let currentTemperature = $("#temperature");
let currentHumidty= $("#humidity");
let currentWSpeed=$("#wind-speed");
let currentUvindex= $("#uv-index");
let 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
let APIKey="d1d1c7bcb9ef6d0effd63fca0dd08d01";
// 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.
const 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.
const weathericon= response.weather[0].icon;
const 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
const 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
const 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
const ws=response.wind.speed;
const 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.
const 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){
const dayover= false;
const 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++){
const date= new Date((response.list[((i+1)*8)-1].dt)*1000).toLocaleDateString();
const iconcode= response.list[((i+1)*8)-1].weather[0].icon;
const iconurl="https://openweathermap.org/img/wn/"+iconcode+".png";
const tempK= response.list[((i+1)*8)-1].main.temp;
const tempF=(((tempK-273.5)*1.80)+32).toFixed(2);
const 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+"%");
}
});
}
//Click Handlers
$("#search-button").on("click",displayWeather);
$(document).on("click",invokePastSearch);
$(window).on("load",loadlastCity);
$("#clear-history").on("click",clearHistory);