-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
194 lines (155 loc) · 5.34 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
//Blank namespace.
var guac = {
};
guac.clientId = 'M01QAACEECGNTWIG3C3GEC5BEX3SNLI2EZ4SJ0WHVQEY3ZA5';
guac.clientSecret = 'YKEJGAP4EJ12E4QR1ZZ3K5WR0W0AZSPDZCOEXKYX3TISEMRU';
// Walking + driving variables.
guac.walkingButton = document.querySelector( 'button.walking' );
guac.drivingButton = document.querySelector( 'button.driving' );
guac.init = function() {
guac.setListeners();
} // end .init
guac.getCoordinates = function() {
// Geolocator.
const getPosition = function ( options ) {
return new Promise(function ( resolve, reject ) {
navigator.geolocation.getCurrentPosition( resolve, reject, options );
});
}
getPosition()
.then( ( position ) => {
guac.lat = position.coords.latitude;
guac.lon = position.coords.longitude;
// Check to see if walking or driving selected.
guac.checkQuery();
// Get results.
guac.getPlaces();
})
.catch( ( err ) => {
console.error( err.message );
});
}; // end getCoordinates.
guac.setListeners = function() {
// Walking icon click.
guac.walkingButton.addEventListener( 'click', function(e) {
// Prevent the default.
e.preventDefault();
// Adds class.
this.classList.add( 'toggleSVG' );
// Remove class from driving icon if selected.
if( guac.drivingButton.classList.contains( 'toggleSVG' ) ) {
guac.drivingButton.classList.remove( 'toggleSVG' );
}
} );
// Driving icon click.
guac.drivingButton.addEventListener( 'click', function(e) {
// Prevent the default.
e.preventDefault();
// Adds class.
this.classList.add( 'toggleSVG' );
// Remove class from driving icon if selected.
if( guac.walkingButton.classList.contains( 'toggleSVG' ) ) {
guac.walkingButton.classList.remove( 'toggleSVG' );
}
} );
// What happens when you submit the form?
document.querySelector( '.search input[type="submit"]' ).addEventListener( 'click', function(e) {
// Prevent the default.
e.preventDefault();
// Begin loading animation.
document.querySelector( '.waiting' ).classList.remove( 'loading' );
document.querySelector( 'img.avocado' ).classList.add( 'avocadoFade' );
// Get coordinates.
guac.getCoordinates();
} );
} // end .setListeners
guac.checkQuery = function() {
// Conditional to check for selection class.
if ( guac.walkingButton.classList.contains( 'toggleSVG' ) ) {
guac.radius = 2500;
} else if ( guac.drivingButton.classList.contains( 'toggleSVG' ) ) {
guac.radius = 40000;
}
} // end .checkQuery
// Function that will go and get information from the API.
guac.getPlaces = function() {
const url = 'https://api.foursquare.com/v2/venues/explore';
const searchArguments = {
intent : 'browse',
client_id : guac.clientId,
client_secret : guac.clientSecret,
ll : `${guac.lat},${guac.lon}`,
radius : guac.radius,
limit : 9,
query : 'guacamole',
openNow : true,
sortByDistance: 1,
venuePhotos : '1',
v : 20160806
}
axios.get( url, {
params: searchArguments
})
.then(function (response) {
guac.displayPlaces(response);
// End Loading Animation.
document.querySelector( '.waiting' ).classList.add( 'loading' );
document.querySelector( 'img.avocado' ).classList.remove( 'avocadoFade' );
})
.catch(function (error) {
console.error(error);
});
} // end .getPlaces.
// Function that is used to display information in html
guac.displayPlaces = function(result) {
// Clear old results.
document.querySelector( '.results' ).innerHTML = null;
// Create variable that holds path to data.
const places = result.data.response.groups[0].items;
// If there aren't any results, display this message to the user.
if (places.length === 0) {
const gothamole = '<div class="wrapper"><h4>Uh oh. It looks like there aren\'t any open places near you that have guac.</h4></div>';
document.querySelector( 'section.results' ).insertAdjacentHTML( 'afterbegin', gothamole );
}
// Loop that will go through array being held in items above.
places.forEach( function(place) {
const name = place.venue.name;
const id = place.venue.id;
const address = place.venue.location.address;
const distance = place.venue.location.distance;
const stars = place.venue.rating;
const photo = place.venue.photos.groups[0].items[0];
const addrPrefix = 'https://foursquare.com/v/';
const photoLink = photo.prefix + photo.height + photo.suffix;
const url = place.venue.url;
let dist = '';
// Only add link markup within title if it exists.
if ( url != undefined ) {
bizLink = `<a href="${url}">${name}</a>`;
} else {
bizLink = `${name}`;
}
// If distance from current location is more than 1000m, convert it to KM and attach different concatenation to add to page.
if ( distance > 1000 ) {
dist = `You are ${(distance/1000).toFixed( 1 )} km away.`;
} else {
dist = `You are ${distance} m away.`;
}
const placeCard =
`<article class="place-result">
<h3>${bizLink}</h3>
<img src="${photoLink}" alt="${name}">
<div class="content">
<a class="addr" href="${addrPrefix}${id}"><i class="fa fa-home"></i>${address}</a>
<p class="dist">${dist}</p>
<p class="rating">This place is rated ${stars} stars!</p>
</div>
</article>`;
// Append that div to results section of HTML.
document.querySelector( 'section.results' ).insertAdjacentHTML( 'beforeend', placeCard );
} ); // end forEach.
} // end displayResults.
// Document ready statement.
document.addEventListener( 'DOMContentLoaded', function() {
guac.init();
} ) ;