-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherappJS.js
125 lines (116 loc) · 4.3 KB
/
weatherappJS.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
$(document).ready(function(){
var url = "";
var longitude;
var latitude;
var tempUnit = "imperial";
var imgUrl;
var accepted;
var temperature;
var hum;
start(); // call start() function to check if geolocation is supported
function start(){
accepted = false;
setTimeout(
function(){
if (!accepted) {
accepted = true;
useCity();
}
},
7000 // if nothing happens after 7secs, assumes the user clicked 'Not Now' and uses IP address to get location details
);
//if geolocation is supported, run getCurrentPosition(), if geolocation is not supported, display error message, then use ip address to get location details
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(success, error, {timeout:9000});
}
else{
$("#error").html("Geolocation is not supported by your browser. Using IP...");
useCity();
$("#error").hide();
}
}
//sets the latitude and longitude and then appends them to the API url and calls getLocation() to display details of the weather
function success(position){
accepted = true;
longitude = position.coords.longitude;
latitude = position.coords.latitude;
url = "https://api.openweathermap.org/data/2.5/weather?lat="+latitude+"&lon="+longitude+"&APPID=a9ec719a259e0f571fb4b509e76c4bc5&units="+tempUnit;
getLocation();
}
function error(positionError){
accepted = true;
useCity();
}
function getLocation(){
$.ajax({
url: url,
data:{
format:'json'
},
dataType:'jsonp',
error: function(jqXHR, textStatus, errorThrown){
$("#error").html("Opps, couldn't retrieve address " +jqXHR.status + jqXHR.statusText);
},
success: function(data){
var desc = data.weather[0].description;
var cityName = data.name;
var countryCode = data.sys.country;
temperature = data.main.temp;
hum = data.main.humidity;
imgUrl = "http://openweathermap.org/img/w/"+data.weather[0].icon+".png";
$("#testing").html("Temp : "+temperature);
$("#descrip").html(desc);
$("#humidity").html("Humidity : " + hum);
document.getElementById("weaImg").src=imgUrl;
$("#placeName").html(cityName +", "+countryCode);
},
type:'GET'
});
}
function useCity(){
$("#cityForm").show();
var city = document.getElementById("cities");
city.addEventListener("keydown", function(e){
if(e.keyCode === 13){
city = document.getElementById("cities").value;
url = "https://api.openweathermap.org/data/2.5/weather?q=" + city +"&APPID=a9ec719a259e0f571fb4b509e76c4bc5&units="+tempUnit;
getLocation();
$("#cityForm").hide();
}
});
document.getElementById("cities").value = "";
}
function useIP(){
$.ajax({
url: "https://ipinfo.io/geo",
datatype:'jsonp',
data:{
format: 'json'},
success: function(data){
var coords = data.loc;
var coordsArr = coords.split(",");
latitude = coordsArr[0];
longitude = coordsArr[1];
url = "https://api.openweathermap.org/data/2.5/weather?lat="+latitude+"&lon="+longitude+"&APPID=a9ec719a259e0f571fb4b509e76c4bc5&units="+tempUnit;
getLocation();
},
error: function(jqXHR, textStatus, errorThrown){
console.log("Opps, something went wrong.");
}
});
}
$("#cel").click(function(){
url=url.replace("imperial", "metric");
getLocation();
});
$("#fah").click(function(){
url = url.replace("metric", "imperial");
getLocation();
});
$("#ent").click(function(){
var city = document.getElementById("cities").value;
url = "https://api.openweathermap.org/data/2.5/weather?q=" + city +"&APPID=a9ec719a259e0f571fb4b509e76c4bc5&units="+tempUnit;
getLocation();
$("#cityForm").hide();
});
});