-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather script test.js
More file actions
89 lines (54 loc) · 1.87 KB
/
weather script test.js
File metadata and controls
89 lines (54 loc) · 1.87 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
//Weather App api key = b804f4d3e596f6dee05285c99ccb597d
//Weather API
//Get weather by location call api.openweathermap.org/data/2.5/weather?id=2655138&units=imperial&APPID=b804f4d3e596f6dee05285c99ccb597d
//Boston city id 2655138
//Check geolocation to get coords and get weather
function getLocation(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(location){
var lat = (location.coords.latitude);
var lon = (location.coords.longitude);
var key = '&APPID=b804f4d3e596f6dee05285c99ccb597d';
var api = "http://api.openweathermap.org/data/2.5/weather?";
var units = "&units=imperial"
var url = api+"lat="+lat+"&lon="+lon+units+key;
console.log(url)
console.log(location.coords)
$.getJSON(url).done(function getWeather(data){
var icon = "http://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
var temperature = data.main.temp
var temp = data.main.temp;
var location = data.name
var celTemp = ((data.main.temp-32)*5/9).toFixed(2);
console.log(temperature);
console.log(temp);
console.log(icon);
console.log(location);
console.log(celTemp);
$("#location").html(location);
$("#temperature").html( temp+ "°F");
$("#icon").attr("src", icon);
$('#fTemp').click(function(){
$("#temperature").html(temp + "°F");
});
$('#cTemp').click(function(){
$("#temperature").html(celTemp + "°C");
});
})
})
}
//location function and ajax call
else{
alert("Geolocation is not supported by this browser")
};
};
//Good example
/*
https://codepen.io/beumsk/pen/Xpbyxv?editors=1010
*/
$(document).ready(function(){
getLocation();
})
//var weather = api+city+"&"+units+"&"+key
//console.log(weather);
//example from youtube *below*https://www.youtube.com/watch?v=4UoUqnjUC2c