-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
214 lines (161 loc) · 6.1 KB
/
script.js
File metadata and controls
214 lines (161 loc) · 6.1 KB
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
$(document).ready(function () {
// To search with enter key
$(document).on("keyup", function (event) {
if (event.keyCode === 13) {
event.preventDefault();
$("#find-breweries-button").click();
}
});
// To start the search for breweries
$("#find-breweries-button").on("click", function () {
localStorage.clear();
$(".brewNames").empty();
state = $("#state").val();
console.log(state)
localStorage.setItem("searched state", state);
breweryType = $("#brewery-type").val();
localStorage.setItem("searched brewery type", breweryType);
var queryURL = "https://api.openbrewerydb.org/breweries?by_state=" + state + "&by_type=" + breweryType + "&per_page=50"
//OPEN BREWERY DB CALL
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
if(response.length >= 1){
console.log(response)
for (var i = 0; i < response.length; i++) {
var testBrewery = response[i].name;
var newBreweryButton = $("<button>");
newBreweryButton.text(testBrewery);
newBreweryButton.attr("data-name", testBrewery);
newBreweryButton.attr("data-toggle", "yelp-modal")
newBreweryButton.addClass("dynamicallyCreatedButtons button");
$(".brewNames").append(newBreweryButton)
}
} else {
var noResult = "No Search Results Found";
var noResultSpan = $("<span>");
var imageTag = $("<img>");
noResultSpan.text(noResult);
noResultSpan.addClass("dynamicallyCreatedButtons button")
$(".brewNames").append(noResultSpan)
}
});
});
$(document).on("click", ".dynamicallyCreatedButtons", function () {
var breweryTitle = $(this).attr("data-name");
localStorage.setItem("Brewery Name", breweryTitle)
console.log(breweryTitle)
// Yelp call based on name and state
var localstorageBreweryName = localStorage.getItem("Brewery Name");
breweryName = localstorageBreweryName
var yelpURL = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=" + localstorageBreweryName + "&location=" + state
$.ajax({
url: yelpURL,
headers: {
'Authorization': 'Bearer r-ru9f0en8RW39PeL2KLuaZ1wwUgo6nm3cHomQ9RbfzCM63ocbL6mrc2C3Culn8SmlqtM5w65eUmULYxoGBHwoE0ibv-e2E-tNx0zE7kbiF01t8IjhfP7l7ocZVhX3Yx',
},
method: "GET"
}).then(function (yelp) {
// Yelp Information in Console
console.log(yelp);
console.log(yelp.businesses[0].name)
console.log(yelp.businesses[0]);
console.log(yelp.businesses[0].rating)
console.log(yelp.businesses[0].location.address1)
console.log(businessID)
var businessID = yelp.businesses[0].id
// Lat and Lon for google maps
lon = yelp.businesses[0].coordinates.longitude;
lat = yelp.businesses[0].coordinates.latitude;
localStorage.setItem("latitude", lat)
localStorage.setItem("longitude", lon)
initMap();
//YELP RICH BUSINESS INFO CALL. Includes pics and more information.
var yelpBizURL = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/" + businessID
$.ajax({
url: yelpBizURL,
headers: {
'Authorization': 'Bearer r-ru9f0en8RW39PeL2KLuaZ1wwUgo6nm3cHomQ9RbfzCM63ocbL6mrc2C3Culn8SmlqtM5w65eUmULYxoGBHwoE0ibv-e2E-tNx0zE7kbiF01t8IjhfP7l7ocZVhX3Yx',
},
method: "GET"
}).then(function (biz) {
console.log(biz)
console.log(biz.image_url)
//create div to display business info
var infoDiv = $("<div>")
// Brewery Name
var name = yelp.businesses[0].name
var nameHead = $("<h3 class='modalLeft'>")
nameHead.text(name)
infoDiv.append(nameHead)
$("#brewInfo").append(infoDiv)
// Brewery Address
var address = yelp.businesses[0].location.address1
var addressHead = $("<h4 class='modalLeft'>")
addressHead.text(address)
infoDiv.append(addressHead)
//Yelp Rating
var rating = biz.rating
var ratingImg = $("<img class='rating'>")
ratingImg.attr("src", "assets/" + "yelpstars/" + rating + ".png")
infoDiv.append(ratingImg)
var reviewCount = biz.review_count
var reviewP = $("<h4 class='rating'>")
reviewP.text(" " + reviewCount + " Ratings")
infoDiv.append(reviewP)
yelpSite = biz.url
var toYelp = $("<a>")
toYelp.attr("href", yelpSite)
toYelp.attr("target", '_blank')
toYelp.html(" on Yelp")
reviewP.append(toYelp)
infoDiv.append("<hr>")
// Brewery Picture
var photo = biz.image_url
var breweryImg = $("<img id='breweryPic'>")
breweryImg.attr("src", photo)
infoDiv.append(breweryImg)
$("#brewInfo").html(infoDiv);
});
});
});
});
// Getting lat and lon from yelp call
var lat = parseFloat(localStorage.getItem("latitude"));
var lon = parseFloat(localStorage.getItem("longitude"));
var breweryName = localStorage.getItem("Brewery Name");
googleApi = "https://maps.googleapis.com/maps/api/js?key=AIzaSyB-WOeEFR0l5fEi4fiug6nt43CVRRbqdc0&callback=initMap"
$("#google").attr("src", googleApi);
// Initiate google maps
function initMap() {
var options = {
zoom: 13,
center: { lat: lat, lng: lon }
}
var map = new google.maps.Map(document.getElementById('map'), options)
addMarker({
coords: { lat: lat, lng: lon },
iconImage: 'https://img.icons8.com/office/40/000000/beer.png',
content: '<h1>' + breweryName + '</h1>'
});
console.log(addMarker)
function addMarker(props) {
var marker = new google.maps.Marker({
position: props.coords,
map: map,
icon: props.iconImage
});
if (props.iconImage) {
marker.setIcon(props.iconImage);
}
if (props.content) {
var infoWindow = new google.maps.InfoWindow({
content: props.content
});
marker.addListener('click', function () {
infoWindow.open(map, marker);
});
}
};
}