-
Notifications
You must be signed in to change notification settings - Fork 0
/
places_api_app.js
419 lines (346 loc) · 11.2 KB
/
places_api_app.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/** **************************************************************************************
* Summary: Google-powered, single-page, web app to tell you what to do with your day.
*
* Kim Mroz 2015
*/
/** **************************************************************************************
* Global Variables
*/
// Default location
var GOOGLE_POI_TYPES = ['amusement_park', 'aquarium', 'art_gallery', 'bowling_alley', 'casino', 'movie_theater', 'museum', 'zoo'];
var GOOGLE_RADIUS_M = 20000;
var DEFAULT_COORDS = {
lat: 51.5072,
lng: -0.1275
};
var DEFAULT_PLACE = 'London, UK';
var coord = DEFAULT_COORDS;
var locat = DEFAULT_PLACE;
// GMaps objects
var map = null;
var marker = null;
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var service = null;
// Compiled results
var result_set = [];
var current_result = 0;
/** **************************************************************************************
* Location Search
*/
/**
* Summary: Start the app
*/
$(document).ready(function() {
// Show body and button
$('#body-block').show();
$('.button-parent').show();
// Resize columns
$('#body-block').css({
'min-height': Math.max($('body').height() - 200, 400) + 'px'
});
$('#right-box').height($('#body-block').height() - 150);
$('#left-box-inner').css({
'max-height': $('#right-box').height() + 'px'
});
placeFooter();
// Kick off the search
getLocation();
});
/**
* Function: Try HTML5's Geolocation to find user's location
*/
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
alert("Geolocation not supported ... Imagine you're in " + DEFAULT_PLACE + '.');
}
}
/**
* Function: Geolocation success callback
*/
function showPosition(position) {
var geocoder = new google.maps.Geocoder();
var latlng = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
geocoder.geocode({
'location': latlng
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[1]) {
locat = results[1].formatted_address;
coord = latlng;
} else {
alert("Location not found ... Imagine you're in " + DEFAULT_PLACE + '.');
}
} else {
alert("Failed to identify your location ... Imagine you're in " + DEFAULT_PLACE + '.');
}
searchPOI(locat, coord);
});
}
/**
* Function: Geolocation failure callback
*/
function showError(undefined) {
alert("Geolocation failed ... Imagine you're in " + DEFAULT_PLACE + '.');
searchPOI(locat, coord);
}
/** **************************************************************************************
* POI Search
*/
/**
* Function: Try Google's Places API to search for local POI
*/
function searchPOI(locationName, coord) {
// Update the header
if ('' !== locationName) {
$('#location-heading').html('Bored in ' + locationName + '?');
}
// Request nearby POI via Google Places API
var request = {
location: coord,
radius: GOOGLE_RADIUS_M,
types: GOOGLE_POI_TYPES
};
var service = new google.maps.places.PlacesService($('footer img').get(0));
service.nearbySearch(request, processSearchResults);
}
/**
* Function: POI search callback
*/
function processSearchResults(results, status, pagination) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
// Successful request (response may be empty)
if (0 !== results.length) {
// Shuffle the results
for (var k = results.length - 1; k > 0; --k) {
var j = Math.floor(Math.random() * (k + 1));
var temp = results[k];
results[k] = results[j];
results[j] = temp;
}
// Add to the compiled list
$.merge(result_set, results);
// Kick off page initialisation with the first response
$(this).trigger('firstResult');
// Get any more available result pages
if (pagination.hasNextPage) {
pagination.nextPage();
}
}
} else {
// Failed Google Places API call
if (0 === result_set.length) {
showFinalMessage();
}
}
}
/**
* Function: When the first result is ready, fill the page and get further details
*/
$(window).one('firstResult', function() {
// Check that we have a result
if (0 === result_set.length) {
showFinalMessage();
} else {
// Show and size the review pane
$('#left-box').show();
$('#overflow-fade').width($('#left-box').width());
$('#overflow-fade').show();
// Add a Google map
map = new google.maps.Map(document.getElementById('map'), {
center: coord,
zoom: 10,
mapTypeControl: false
});
// Create the Google objects for getting a route
service = new google.maps.places.PlacesService(map);
directionsDisplay.setMap(map);
// Request further details on the POI
requestInfo(result_set[0]);
$('#next-button').removeClass('disabled');
}
});
/**
* Function: Request a place's details and route, and add responses to page
*/
function requestInfo(result) {
// Check that we have a result
if (!result) {
showFinalMessage();
} else {
// Request further details on the POI
service.getDetails({
placeId: result.place_id
}, function(place, status) {
// Expect a phone number, reviews and a web address
var phone = null;
var review = '';
var website = null;
// If the Google Places API call fails, carry on without the extra details
if (status == google.maps.places.PlacesServiceStatus.OK) {
phone = place.international_phone_number;
website = place.website;
if (place.reviews) {
for (var j = 0; j < place.reviews.length; ++j) {
if (place.reviews[j].text) {
review += '"';
review += place.reviews[j].text.trim();
review += '"<br><br>';
}
}
}
}
// Request a route and update the page with the results
requestRoute(result, website, phone, review);
});
}
}
/**
* Function: Request a route to this POI and add responses to page
*/
function requestRoute(result, website, phone, review) {
// Remove old marker
if (marker) {
marker.setMap(null);
}
var distance = -1;
var duration = -1;
// Request directions to the POI
directionsService.route({
origin: new google.maps.LatLng(coord.lat, coord.lng),
destination: result.geometry.location,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
// If a success then add the route, otherwise just add a marker and center the map
if (status === google.maps.DirectionsStatus.OK && response) {
// Success
directionsDisplay.setDirections(response);
distance = response.routes[0].legs[0].distance.text;
duration = response.routes[0].legs[0].duration.text;
} else {
// Failed
marker = new google.maps.Marker({
position: result.geometry.location,
map: map
});
map.setCenter(result.geometry.location);
}
// Update page with the requested place details
updateDetails(result, website, phone, review, distance, duration);
});
}
/**
* Function: Add any details and route to page for this POI
*/
function updateDetails(result, website, phone, review, distance, duration) {
// Remove last result
$('.poi-data').empty();
$('#poi-rating').hide();
// POI name
$('#poi-name').html(result.name);
// POI type and current open/closed status
var subLine = '';
if (result.types[0]) {
subLine += result.types[0].replace(/_/g, ' ');
}
if (result.opening_hours && result.opening_hours.hasOwnProperty('open_now')) {
subLine += ' — ';
subLine += result.opening_hours.open_now ? 'open' : 'closed';
subLine += ' now';
}
$('#poi-type').html('(' + subLine + ')');
// POI reviews
if (!review) {
review = 'No Google reviews yet.';
}
$('#poi-review').html(review);
// POI telephone
if (phone) {
$('#poi-phone').html(phone);
}
// POI web address
if (website) {
$('#poi-website').html('<a href="' + website + '">website</a>');
}
// POI star rating (rounded to nearest integer)
if (result.rating) {
$('#poi-rating').width(result.rating * 26.5);
$('#poi-rating').show();
}
// POI route length/duration when driving
if (-1 !== distance) {
$('#poi-distance').html(distance + '<br>~' + duration + ' drive');
}
}
/**
* Function: When no more POIs available, app finishes with a final message
*/
function showFinalMessage() {
var msg;
if (result_set.length < 60) {
msg = 'No ';
msg += result_set.length ? 'more ' : '';
msg += 'places found.<br><br>You should probably move ...';
} else {
msg = "Didn't like those?<br><br>You should probably move ...";
}
// Show final message
$('#body-block').html('<h2 id="final-message">' + msg + '</h2>');
// Remove the button without changing the page structure
$('.button-parent').height($('.button-parent').height());
$('.button-parent').empty();
}
/** **************************************************************************************
* User interactions
*/
/**
* Function: Button click should move to the next POI, once enabled
*/
$('#next-button').click(function() {
if (!$(this).hasClass('disabled')) {
requestInfo(result_set[++current_result]);
}
});
/**
* Function: [Enter] should move to the next POI
*/
$('html').keyup(function(event) {
if (13 == event.keyCode) {
$('#next-button').click();
}
});
/** **************************************************************************************
* Page Setup
*/
/**
* Function: On resize, ensure the responsive elements are relatively correct
*/
$(window).resize(function() {
placeFooter();
// Resize columns
$('#body-block').css({
'min-height': Math.max($('body').height() - 200, 400) + 'px'
});
$('#left-box-inner').css({
'max-height': $('#right-box').height() + 'px'
});
$('#overflow-fade').css({
'width': $('#left-box').width() + 'px'
});
});
/**
* Function: Place the footer either after the content or at the bottom of the view
*/
function placeFooter() {
var height = $(window).height() - $('footer').position().top - $('footer').height();
if (height > 0) {
$('footer').css({
'margin-top': height + 'px'
});
}
}