-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherReport.js
More file actions
54 lines (50 loc) · 1.59 KB
/
WeatherReport.js
File metadata and controls
54 lines (50 loc) · 1.59 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
var geo = navigator.geolocation;
var long;
var la;
var apiKey = "1ccc4f6b1ffdd08051436bc7194644da";
var weather;
var api = "api.openweathermap.org/data/2.5/weather?" + "APPID=" + apiKey;
$(document).ready(function(){
// test whether geolocation is available
if (geo) {
// if available do somehting
geo.getCurrentPosition(function(position){
long = "&lon=" + position.coords.longitude;
la = "&lat=" + position.coords.latitude;
displayWeather(long, la);
})
}
$("#changeUnit").on("click", function(){changeU()});
})
function displayWeather(lon, lat){
var url = "https://" + api + lon + lat + "&format=json";
// $("#h3").html(url);
$.getJSON(url, function(data){
// doesnt run
var weather = data.weather[0].main;
var icon = data.weather[0].icon;
var temp = data.main.temp;
var city = data.name;
var country = data.sys.country;
$("#h1").html(city + ", " + country);
$("#h2").html(weather);
//temp in kelvin
$("#h3").html(Math.round((temp - 273.15)*10/10));
setIcon(icon);
})
}
function changeU(){
var currentTempUnit = $("#changeUnit").text();
var newTempUnit = currentTempUnit == "' C" ? "' F" : "' C";
$("#changeUnit").html(newTempUnit);
var currentTemp = $("#h3").text();
var tempF = Math.round(((currentTemp * 1.8 +32)*10)/10);
var tempC = Math.round((((currentTemp - 32)/1.8)*10)/10);
var newTemp = currentTempUnit == "' C" ? tempF : tempC;
$("#h3").html(newTemp);
}
function setIcon(i){
var iconUrl = "http://openweathermap.org/img/w/" + i + ".png";
var img = document.getElementById("imge");
img.src = iconUrl;
}