/
main.js
113 lines (95 loc) · 3.43 KB
/
main.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
var fullResult;
var userCity;
var icon;
var temp_kelvin;
var temp_celcius;
var temp_fahrenheit;
var fahrenheit = true;
$(document).ready(function() {
/******************************************************************************
Fetches the weather data from the OpenWeather api
This function will be called by the getLocation() function
*******************************************************************************/
function getWeather(city, country_code) {
//OpenWeather api requires an APPID for all api calls.
var app_id = 'b295c087f2f1be435ff5341d0c5868de';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
data: {
q: city + ',' + country_code,
APPID: app_id
}
})
.done(function (data){
fahrenheit = true;
//fullResult = JSON.stringify(data);
userCity = data.name;
icon = data.weather[0].icon;
temp_kelvin = data.main.temp;
temp_celcius = temp_kelvin - 273;
temp_fahrenheit = 9 / 5 * temp_celcius + 32;
$("#user-location").html(userCity);
$("#temperature").html( parseInt(temp_fahrenheit) );
$("#tempUnits").html(" ℉");
$("#weather-icon").attr('src', 'http://openweathermap.org/img/w/' +icon +'.png');
//$("#apiCall").html(fullResult);
})
.fail(function (err) {
alert("It failed");
});
};
/******************************************************************************
- We will use the ipinfo api, which returns json in the form:
- Assuming the call was http://ipinfo.io?dataType=jsonp, will return:
{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.3860,-122.0838",
"org": "AS15169 Google Inc.",
"postal": "94035"
}
******************************************************************************/
function getLocation() {
$.ajax({
url: 'http://ipinfo.io',
dataType: 'jsonp',
})
.done(function (data) { //I should probably use "success" here rather than ".done"
// Need to look into this!
var currentUserLocation = JSON.stringify(data);
//$("#userLocation").html(currentUserLocation);
var city = data.city;
// var city = "Dallas"; //Testing Line
var country_code = data.country;
//When done, call the getWeather() function
getWeather(city, country_code);
})
.fail(function(err) {
//$("#userLocation").html("The request to get the user location failed! Bleah!");
})
};
/*****************************************************************************
changeUnits will be called when the user wants to change the units
*****************************************************************************/
function changeUnits (tempF) {
if (fahrenheit) {
$("#temperature").html( parseInt(temp_celcius) );
$("#tempUnits").html(" ℃");
$("#changeUnits").html("℉");
fahrenheit = false;
}else {
$("#temperature").html( parseInt(temp_fahrenheit) );
$("#tempUnits").html(" ℉");
$("#changeUnits").html("℃");
fahrenheit = true;
}
}
//Call getLocation() when the document is ready - to get the user location
//getLocation will also call the getWeather function to retrieve the actual weather
getLocation();
//this is called if the user clicks the change units button
$("#changeUnits").on("click", changeUnits);
});