-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
346 lines (299 loc) · 9.28 KB
/
script.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// foursquare venue
const clientSecret = "ODC4U4DJV14HN0ZV4HPYJ3YUYPBMN3YCL40JS0C0DX3NPAZC";
const versionApi = "20181101";
const clientId = "JJKCIBIBQ2L2ACKIQ2SFIIG4YFXU1UAJKSGMGUCKNYEO3TCS";
const venueSearchURL = "https://api.foursquare.com/v2/venues/explore";
// google geocoding
const geoCodingUrl = `https://maps.googleapis.com/maps/api/geocode/json`;
const geoCodingClientKey = `AIzaSyC7B7GvOsWcm329Cf0Yl7Li7tW0u5wUlxM`;
// Weather app
const appid = "c1030d35c644039241e355758547f2ec";
const openWeatherUrl = "https://api.openweathermap.org/data/2.5/weather";
const unit = "imperial";
// map variables
var map;
var bounds;
// stores state data
let state = {
query: {
place: "",
address: ""
},
venues: [],
markers: [],
images: [],
location: {
lat: "",
lng: ""
},
selectedVenue: {},
weatherJson: {}
};
/********* Calls event handlers that are bind *********/
$(() => {
bindEventHandlers();
});
// binds event handlers
const bindEventHandlers = () => {
handleLandingPageFormSubmit();
handleResultsPageFormSubmit();
handleSearchResultsClick();
toggleSidebar();
closeModal();
markerHover();
removeMarkers();
};
/********* Explain what this does *********/
// Initiates map
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: { lat: 34.0522, lng: -118.2437 },
zoom: 3,
zoomControlOptions: {
position: google.maps.ControlPosition.TOP_RIGHT
},
fullScreenControl: false,
mapTypeControl: false,
streetViewControl: false,
streetViewControlOptions: {
position: google.maps.ControlPosition.RIGHT_CENTER
}
});
map.setOptions({ minZoom: 2, maxZoom: 28 });
bounds = new google.maps.LatLngBounds();
}
/********* Explain what this does *********/
// makes sidebar hide and unhide on click of button
function toggleSidebar() {
$("#show-hide").click(function() {
var currentStyle = $("#sidebar").css("width");
if (currentStyle === "0px") {
$("#sidebar").css("width", "calc(100% - 35px)");
$("#search-results", ".results-search-background").show();
$("#show-hide").css("left", "0");
} else {
$("#sidebar").css("width", "0");
$("#search-results", ".results-search-background").hide();
$("#show-hide").css("left", "0");
}
});
}
// closes modal
function closeModal() {
$(".popup-overlay").click(function(event) {
$("#search-modal").hide();
});
}
// adds animation when hovering over venue on sidebar
function markerHover() {
$("#search-results").on("mouseover", ".result", function(e) {
var index = $(this).attr("data-index");
state.markers[index].setAnimation(google.maps.Animation.BOUNCE);
});
$("#search-results").on("touchstart", ".result", function(e) {
var index = $(this).attr("data-index");
state.markers[index].setAnimation(google.maps.Animation.BOUNCE);
});
$("#search-results").on("mouseout", ".result", function(e) {
var index = $(this).attr("data-index");
state.markers[index].setAnimation(-1);
});
$("#search-results").on("touchend", ".result", function(e) {
var index = $(this).attr("data-index");
state.markers[index].setAnimation(-1);
});
}
function removeMarkers() {
$(".js-sidebar-submit").on("click", function() {
for (i = 0; i < state.markers.length; i++) {
state.markers[i].setMap(null);
}
});
}
/********* Explain what this does *********/
// listens for search submit on landing page
const handleLandingPageFormSubmit = () => {
$("#landing-page-form").submit(event => {
event.preventDefault();
state.query.place = $("#landing-place-query").val();
state.query.address = $("#landing-address-query").val();
fetchVenues();
});
};
// listens for search submit on side bar
const handleResultsPageFormSubmit = () => {
$("#results-page-form").submit(event => {
event.preventDefault();
state.query.place = $("#results-place-query").val();
state.query.address = $("#results-address-query").val();
fetchVenues();
});
};
const fetchVenues = (maxResults = 10) => {
const params = {
query: state.query.place,
near: state.query.address,
v: versionApi,
client_id: clientId,
client_secret: clientSecret,
limit: maxResults
};
const queryString = formatQueryParamsPlaces(params);
const url = venueSearchURL + "?" + queryString;
fetch(url)
.then(response => response.json())
.then(results => {
state.venues = results.response.groups[0].items;
displayResults();
})
.catch(err => {
$(".js-error-message").html(
"Whoops! We currently don't have anything available for your search. Please try another search."
);
});
};
// Displays search results
function displayResults() {
$(".container").hide();
$("#map-section").show();
$("#sidebar").show();
$("#show-hide").show();
bounds = new google.maps.LatLngBounds();
let renderedVenues = state.venues.map((venue, index) => {
setVenueMarker(venue, index);
getVenueImage(venue.venue.id, index);
return renderSidebarvenue(venue, index);
});
map.fitBounds(bounds);
map.panToBounds(bounds);
map.setCenter(map.getCenter());
$("#search-results").html(renderedVenues);
}
// Retrieves image for venue
function getVenueImage(venueID, index) {
const params = {
v: versionApi,
client_id: clientId,
client_secret: clientSecret,
limit: 1
};
const queryString = formatQueryParamsPlaces(params);
const url = `https://api.foursquare.com/v2/venues/${venueID}/photos?${queryString}`;
fetch(url)
.then(response => response.json())
.then(data => {
let img = data.response.photos.items[0];
let imgURL = `${img.prefix}300x300${img.suffix}`;
$(`#image-${venueID}`).attr("src", imgURL);
state.images[index] = imgURL;
})
.catch(error => {
console.log(`error`, error);
});
}
// Format query parameters
function formatQueryParamsPlaces(params) {
const queryvenues = Object.keys(params).map(
key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`
);
return queryvenues.join("&");
}
// Creates side bar with search results
function renderSidebarvenue(venue, index, imgURL) {
return `<section class="result js-result" data-index=${index}>
<a href=# class="js-venue-name">
<h3 class="venue-name">${venue.venue.name}</h3>
</a>
<div class="venue-img-container">
<img class="venue-img" id="image-${
venue.venue.id
}" src="${imgURL}" alt="image-of-venue">
</div>
<p class="address">${venue.venue.location.formattedAddress}</p>
</section>`;
}
// Set google map markers for venues
function setVenueMarker(venue, index) {
var venueLocation = new google.maps.LatLng(
venue.venue.location.lat,
venue.venue.location.lng
);
var marker = new google.maps.Marker({
position: venueLocation,
title: venue.venue.name,
map: map
});
bounds.extend(
new google.maps.LatLng(marker.position.lat(), marker.position.lng())
);
state.markers[index] = marker;
}
// retrieves information on selected venue
const handleSearchResultsClick = () => {
$("#search-results").on("click", ".result", function(event) {
event.preventDefault();
let index = $(event.currentTarget).attr("data-index");
state.selectedVenue = state.venues[index].venue;
state.imageURL = state.images[index];
fetchSelectedVenue();
});
};
// gets lat and long from selected venue and formats address to lat and long
const fetchSelectedVenue = () => {
const geoCodingParams = {
key: geoCodingClientKey,
address: state.selectedVenue.location.formattedAddress
};
const geoCodingQueryString = formatGeoCodingParams(geoCodingParams);
const geoUrl = geoCodingUrl + "?" + geoCodingQueryString;
fetch(geoUrl)
.then(response => response.json())
.then(geoCodingResponseJson => {
state.location.lat =
geoCodingResponseJson.results[0].geometry.location.lat;
state.location.lng =
geoCodingResponseJson.results[0].geometry.location.lng;
return fetchWeatherData();
})
.then(results => displaySelectedModal())
.catch(error => alert(error));
};
// formats geocoding parameters
function formatGeoCodingParams(geoCodingParams) {
const geoCodingQueryItems = Object.keys(geoCodingParams).map(
key =>
`${encodeURIComponent(key)}=${encodeURIComponent(geoCodingParams[key])}`
);
return geoCodingQueryItems.join("&");
}
function fetchWeatherData() {
return fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${
state.location.lat
}&lon=${
state.location.lng
}&units=imperial&appid=c1030d35c644039241e355758547f2ec`
)
.then(response => response.json())
.then(responseJson => {
state.weatherJson = responseJson;
});
}
// Displays modal when user clicks on venue
function displaySelectedModal() {
$(".popup-content").html(`
<h3 class="popup-name">${state.selectedVenue.name}</h2>
<img class="venue-img" id="image-{venue.venue.id}" src="${
state.imageURL
}" alt="selected-venue-image">
<p class="popup-address">${
state.selectedVenue.location.formattedAddress
}</p>
<div id="cloud-background">
<h4 class="temperature-tittle">Weather</h4>
<p class="weather-description">${state.weatherJson.weather[0].main}</p>
<p class="temperature">${state.weatherJson.main.temp} ℉</p>
</div>
`);
$("#search-modal").show();
}