-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeather-app.html
169 lines (158 loc) · 5.82 KB
/
Weather-app.html
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script language="JavaScript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>
A Simple Weather App
</title>
<style type="text/css">
*{
padding:0;
margin:0;
}
.row{
margin:5%;
}
body{
background-color: black;
color:white;
}
#temp_unit{
cursor:pointer;
color: #006dcc;
}
#temp_unit:hover{
color:#005096;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-s-12 col-md-12 text-center">
<h1><i class="fa fa-cloud" aria-hidden="true"></i>Weather App</h1>
<small><a href="https://darksky.net/poweredby/" target="_blank">Powered by Dark Sky</a></small>
</div>
</div>
<div class="row text-center">
<form id="form-id">
<div class="col-xs-12 col-s-12 col-md-12 text-center form-group">
<label style="float:left;">Enter City Name:</label>
<input type="text" autocomplete="off" class="form-control" style="background-color: black;color:white;" id="city_text">
</div>
<button class="btn btn-primary btn-lg" id="submit" onclick="new_city();return false;">Submit</button>
</form>
</div>
<div class="row well" style="background-color: black;color:white;">
<div class="col-xs-12 col-s-12 col-md-12 text-center">
<h3 id="City"></h3>
</div>
<div class="col-xs-12 col-s-12 col-md-12 text-center">
<h4><span id="temp"></span><span id="temp_unit" style="display:none;"> ° F</span></h4>
</div>
<div class="col-xs-12 col-s-12 col-md-12 text-center">
<h4 id="summary"></h4>
</div>
<div class="col-xs-12 col-s-12 col-md-12 text-center">
<canvas id="icon" width="128" height="128"></canvas>
</div>
<div class="col-xs-12 col-s-12 col-md-12 text-center">
<marquee id="daily_summary"></marquee>
</div>
</div>
</div>
<footer class="text-center">
<p> Developed by: <a href="https://github.com/utsavkapoor">Utsav Kapoor</a></p>
<p>Contact information: <a href="mailto:[email protected]">[email protected]</a>.</p>
</footer>
<script language="JavaScript" type="text/javascript" src="skycons.js"></script>
<script type="text/javascript">
var latitude;
var longitude;
var city="";
var country = "";
var skycons;
$(window).ready(function(){
skycons = new Skycons({"color": "white"});
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
get_weather(latitude,longitude);
});
} else {
console.log("Geolocation is not supported by this browser.");
}
});
function new_city(){
console.log("I am here");
var temp = document.getElementById('city_text').value.split(",");
console.log(temp);
city = temp[0];
country = temp[1];
get_latlan(temp[0],temp[1]);
}
$("#temp_unit").click(function(){
var text = $("#temp_unit").text().split(" ")[2];
//console.log(text);
var temp = $("#temp").text();
if (text == "F"){
document.getElementById('temp').innerHTML = convert_to_celsius(temp);
document.getElementById('temp_unit').innerHTML = " ° C";
}
else {
document.getElementById('temp').innerHTML = convert_to_farenheit(temp);
document.getElementById('temp_unit').innerHTML = " ° F";
}
});
function get_weather(lat,lan)
{
var url = "https://api.darksky.net/forecast/<Dark sky Key>/" + lat + "," + lan + "?callback=?";
$.getJSON(url, function(data){
console.log(data);//drizzle,clouds,rain,snow,clear,thunderstorm
//document.getElementById('City').innerHTML = data.timezone;
if(city == "" && country == ""){
assign_city(lat,lan);
} else {
document.getElementById('City').innerHTML = city + "," + country;
}
document.getElementById('temp').innerHTML = Math.floor(parseInt(data.currently.apparentTemperature));
document.getElementById('temp_unit').style.display = "inline";
document.getElementById('summary').innerHTML = data.currently.summary;
var str = data.currently.icon;
skycons.remove("icon");
skycons.add("icon", str);
skycons.play();
document.getElementById('daily_summary').innerHTML = data.daily.summary;
});
}
function assign_city(lat,lan){
var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+"," + lan+ "&key=<Google API KEY>";
var answer;
$.getJSON(url, function(data){
//console.log(data.results[5].formatted_address);
var temp = data.results[3].formatted_address.split(",");
document.getElementById('City').innerHTML = temp[0] + "," + temp[2];
});
}
function get_latlan(city,country){
var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" +city+","+country + "&key=<Google API KEY>";
$.getJSON(url, function(data){
document.getElementById('City').innerHTML = city + "," + country;
var new_lat = data.results[0].geometry.location.lat;
var new_lng = data.results[0].geometry.location.lng;
console.log(new_lat+","+new_lng);
get_weather(new_lat,new_lng);
});
}
function convert_to_celsius(temp){
return Math.ceil((parseInt(temp)-32)/1.8);
}
function convert_to_farenheit(temp){
return Math.floor(parseInt(temp) * 1.8 + 32);
}
</script>
</body>
</html>