-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather-map.html
164 lines (123 loc) · 7.33 KB
/
weather-map.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Weather Api</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/weather-map.css">
<link href="https://fonts.googleapis.com/css?family=Dosis|Josefin+Sans|Titillium+Web" rel="stylesheet">
</head>
<body>
<!--------------------------------------------------------------------------------------------------------------------->
<!----------------------------------------------------- HEADER -------------------------------------------------------->
<!--------------------------------------------------------------------------------------------------------------------->
<h1 class="text-center header-text">Weather Application</h1>
<!--------------------------------------------------------------------------------------------------------------------->
<!-------------------------------------------------- USER INPUT AREA -------------------------------------------------->
<!--------------------------------------------------------------------------------------------------------------------->
<div id="input-area">
<input type="text" id="user-input">
<button id="submit">enter</button>
</div>
<!--------------------------------------------------------------------------------------------------------------------->
<!------------------------------------------- WEATHER INFO DISPLAY AREA ---------------------------------------------->
<!--------------------------------------------------------------------------------------------------------------------->
<div id="for0" class="forecast"></div>
<div id="for1" class="forecast"></div>
<div id="for2" class="forecast"></div>
<div class="map"></div>
<!--------------------------------------------------------------------------------------------------------------------->
<!--------------------------------------------- GOOGLE MAPS CANVAS DISPLAY -------------------------------------------->
<!--------------------------------------------------------------------------------------------------------------------->
<div id="map-canvas"></div>
<!--------------------------------------------------------------------------------------------------------------------->
<!--------------------------------------------------- SCRIPT AREA ---------------------------------------------------->
<!--------------------------------------------------------------------------------------------------------------------->
<script src="js/jquery-2.2.4.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD4oZM_9jWPZ7n7XYtWS1CY9eSBbxJRVdM"></script>
<script>
var submit = $("#submit");
////////////////////////////////////////////////////////////////////////////////////////
////////////////////// GETTING WEATHER INFO FROM WEATHER API ///////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////// FUNCTION TAKING THE AJAX CALL TO WEATHER API AND ADDING ARGUMENT TO LOCATION AREA.
var getWeather = function (weather) {
///////////////////////////////////// ADDING THE AJAX CALL TO TO A VARIABLE.
var weatherThing = $.get("http://api.openweathermap.org/data/2.5/forecast", {
APPID: "07099172fe609f5517481eb2e1e81cf4",
q: weather,
units: "imperial",
cnt: 3
});
///////////////////////////////////// DONE CALLBACK FUNCTION.
weatherThing.done(function (data) {
var html = $(".forecast");
var containers = [$("#for0"), $("#for1"), $("#for2")];
var ul = "<ul>";
data.list.forEach(function (day, i) {
///////////////////////////// ADDING UNORDERED LIST TO PAGE. WITH INFO RETRIEVED FROM THE API DATABASE.
containers[i].html(ul + "<li id='first'>" + parseInt(day.main.temp_max) + "º" + " / " + parseInt(day.main.temp_min) + "º" + "</li>" + " <li>" + "<img src='http://openweathermap.org/img/w/" + day.weather[0].icon + ".png'>" + "</li>" + "<li>" + "Humidity: " + day.main.humidity + "</li>" + "<li>" + "Wind: " + day.wind.speed + "</li>" + "<li>" + "Pressure: " + day.main.pressure + "</li>");
containers[i].append("</ul>");
$("ul").css("listStyle", "none");
console.log(data.list)
})
});
};
////////////////////////////////////////////////////////////////////////////////////////
////////////////////// EVENT LISTENER BINDING FUNCTION TO BUTTON ///////////////////////
////////////////////////////////////////////////////////////////////////////////////////
submit.bind("click", function () {
var user_input = $("#user-input").val();
////////////////////////////////// GET WEATHER FUNCTION CALL.
getWeather(user_input);
////////////////////////////////// GET GET GEOLOCATION FUNCTION CALL.
getGeoLocation(user_input);
});
////////////////////////////////////////////////////////////////////////////////////////
//////////////////////// GENERATING GOOGLE MAPS ////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// SET UP MAP OPTIONS OBJECT.
var mapOptions = {
////////////////////////////////// SET UP ZOOM LEVEL.
zoom: 15,
////////////////////////////////// SETS THE CENTER FOR THE MAP.
center: {
lat: 29.426791,
lng: -98.489602
}
};
////////////////////////////////////// RENDER MAP.
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
////////////////////////////////////////////////////////////////////////////////////////
/////////////////////// GENERATING THE GEOLOCATION FUNCTION ///////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
var getGeoLocation = function (place) {
/////////////////////////////////// INITIALIZING THE GEOCODER OBJECT.
var geocoder = new google.maps.Geocoder();
////////////////////////////////// GEOCODE THE ADDRESS.
geocoder.geocode({"address": place}, function (results, status) {
////////////////////////////// CHECK FOR SUCCESSFUL RESULT.
if (status == google.maps.GeocoderStatus.OK) {
////////////////////////// RE-CENTER MAP OVER THE NEW ADDRESS.
map.setCenter(results[0].geometry.location);
} else {
////////////////////////// IF REQUEST FAILS SHOW MESSAGE WITH STATUS.
alert("Geocoding was not successful - STATUS: " + status);
}
////////////////////////////// SPOT OBJECT.
var spot = {
lat: results[0].geometry.location.lat(),
lng: results[0].geometry.location.lng()
};
////////////////////////////// GENERATE MARKER USING SPOT LOCATION.
var marker = new google.maps.Marker({
position: spot,
map: map
});
});
}
//////////////////////////////////////////////////////////////////////////////////////////
</script>
</body>
</html>