-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
254 lines (230 loc) · 6.06 KB
/
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
$(function() {
let infowindow;
let map;
let brewData;
let marker;
const coordObj = {};
let bounds;
let hours;
let address;
let website;
let brewName;
let prevInfoWindow;
let mapOptions;
let logo;
let description;
let phone;
let userCoords;
let userZip;
//Styling for google map
let styledMapType = new google.maps.StyledMapType(
[{
"featureType": "all",
"elementType": "labels.text.fill",
"stylers": [{
"color": "#ffffff"
}]
}, {
"featureType": "all",
"elementType": "labels.text.stroke",
"stylers": [{
"color": "#000000"
}, {
"lightness": 13
}]
}, {
"featureType": "administrative",
"elementType": "geometry.fill",
"stylers": [{
"color": "#000000"
}]
}, {
"featureType": "administrative",
"elementType": "geometry.stroke",
"stylers": [{
"color": "#144b53"
}, {
"lightness": 14
}, {
"weight": 1.4
}]
}, {
"featureType": "landscape",
"elementType": "all",
"stylers": [{
"color": "#08304b"
}]
}, {
"featureType": "poi",
"elementType": "geometry",
"stylers": [{
"color": "#0c4152"
}, {
"lightness": 5
}]
}, {
"featureType": "road.highway",
"elementType": "geometry.fill",
"stylers": [{
"color": "#000000"
}]
}, {
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [{
"color": "#0b434f"
}, {
"lightness": 25
}]
}, {
"featureType": "road.arterial",
"elementType": "geometry.fill",
"stylers": [{
"color": "#000000"
}]
}, {
"featureType": "road.arterial",
"elementType": "geometry.stroke",
"stylers": [{
"color": "#0b3d51"
}, {
"lightness": 16
}]
}, {
"featureType": "road.local",
"elementType": "geometry",
"stylers": [{
"color": "#000000"
}]
}, {
"featureType": "transit",
"elementType": "all",
"stylers": [{
"color": "#146474"
}]
}, {
"featureType": "water",
"elementType": "all",
"stylers": [{
"color": "#021019"
}]
}], {
name: 'Styled Map'
}
);
// initalize a new map
function initMap() {
mapOptions = {
zoom: 5,
center: new google.maps.LatLng(37.09024, -100.712891),
panControl: false,
zoomControl: true,
mapTypeControlOptions: {
mapTypeIds: ['roadmap', 'satellite', 'hybrid', 'terrain',
'styled_map'
]
},
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.RIGHT_CENTER
},
scaleControl: false
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
bounds = new google.maps.LatLngBounds();
map.mapTypes.set('styled_map', styledMapType);
map.setMapTypeId('styled_map');
}
// create new map marker from API data passed in and pushes marker into array, used in loop
function createMarker(brewery, coords) {
marker = new google.maps.Marker({
position: coords,
map: map,
title: brewery.brewery.name
});
}
// sets event listener on marker to close info window if another opens
function setEventListner() {
google.maps.event.addListener(marker, 'click', (function(marker, content, infowindow) {
return function() {
if (prevInfoWindow) {
prevInfoWindow.close();
}
prevInfoWindow = infowindow;
infowindow.open(map, marker);
};
})(marker, content, infowindow));
}
// creates the coords and info window content and sets the content
function setInfoWindowContents() {
for (const store of brewData) {
coordObj.lat = store.latitude;
coordObj.lng = store.longitude;
createMarker(store, coordObj);
loc = new google.maps.LatLng(marker.position.lat(), marker.position.lng());
bounds.extend(loc);
hours = store.hoursOfOperation || '';
address = store.streetAddress || '';
description = store.brewery.description || 'No Description Avaliable';
phone = store.phone || '';
if (store.website === undefined) {
website = '';
brewName = '';
} else {
website = store.website;
brewName = store.brewery.name;
}
if (store.brewery.images === undefined) {
logo = '';
} else {
logo = `<img src=${store.brewery.images.medium}>`;
}
content = '<div id="content">' +
`<h3 id="firstHeading" class="firstHeading">${store.brewery.name}</h3>` +
`<div>${logo}` +
'<div id="bodyContent">' +
`${description} ` +
'</div>' +
'</br>' +
`<div>${address}</div>` +
'</br>' +
`<div>${hours}</div>` +
'</br>' +
`<div><a href='tel:${phone}'>${phone}</div>` +
'</br>' +
`<div><a href=${website}>${brewName}</div>` +
'</div>';
infowindow = new google.maps.InfoWindow({
content: content
});
setEventListner();
}
}
function throwError() {
$('#error').text('No Breweries Found!')
}
function clearError() {
$('#error').text('')
}
// set click event on button to populate mapwith markers
function submitClick(event) {
event.preventDefault();
brewData = $.getJSON(`https://crossorigin.me/https://api.brewerydb.com/v2/locations/?key=dbf3bd0628e34ab8dd8398fa95503119&postalCode=${$('#number').val()}`);
brewData.done(function(resData) {
brewData = resData;
brewData = brewData.data;
if (brewData === undefined) {
throwError();
} else {
clearError()
map = null;
initMap()
setInfoWindowContents();
map.fitBounds(bounds);
map.panToBounds(bounds);
}
});
}
initMap(); // initializes first map on page load
$('#button').click(submitClick); // creates new map with markers generated from API data
});