You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: projects/build-a-cafe-finder-with-javascript/build-a-cafe-finder-with-javascript.mdx
+25-31Lines changed: 25 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,17 +34,16 @@ tags:
34
34
uid="CgUQBagDrWWBRUuoh3mkyi3aedg1"
35
35
/>
36
36
37
-
**Prerequisites: HTML, CSS, JavaScript**
38
-
37
+
**Prerequisites:** HTML, CSS, JavaScript
39
38
**Read Time:** 30 minutes
40
39
41
40
## Introduction
42
41
43
-
If you're an iced-bev-meets-coworking-space fiend like me and want to shake up what cafe you head to next, this is the perfect project for you. We'll be using our trilogy of web dev tools: ****HTML****, ****CSS****, and ****JavaScript****, and an API from the Google Maps Platform called ****Places****. We'll also make some quick animations and implement swiping recognition with a JavaScript library called ****Hammer****!
42
+
If you're an iced-bev-meets-coworking-space fiend like me and want to shake up what cafe you head to next, this is the perfect project for you. We'll be using our trilogy of web dev tools: HTML, CSS, and JavaScript, and an API from the Google Maps Platform called [Places](https://developers.google.com/maps/documentation/places/web-service/overview). We'll also make some quick animations and implement swiping recognition with a JavaScript library called [Hammer](https://hammerjs.github.io)!
44
43
45
44
## About the Google Maps Platform
46
45
47
-
The Google Maps Platform is a collection of Application Programming Interfaces (APIs) and Software Development Kits (SDKs). Each tool on the platform lets users use Google Maps functions and data in their own projects!
46
+
The [Google Maps Platform](https://mapsplatform.google.com) is a collection of Application Programming Interfaces (APIs) and Software Development Kits (SDKs). Each tool on the platform lets users use Google Maps functions and data in their own projects!
48
47
49
48
Some popular apps that use the Google Maps Platform include Uber and Airbnb, which access a user's location to calculate rides and show rentals in specific areas!
50
49
@@ -56,11 +55,11 @@ The [Places API](https://developers.google.com/maps/documentation/places/web-ser
56
55
57
56
### Workspace
58
57
59
-
Start by creating a new HTML/CSS/JS project in [Codédex Builds](https://www.codedex.io/builds). You can also use a Code editor of your choice, like [VSCode](https://code.visualstudio.com/).
58
+
Start by creating a new HTML/CSS/JS project in [Codédex Builds](https://www.codedex.io/builds). You can also use a code editor of your choice, like [VS Code](https://code.visualstudio.com/).
60
59
61
60
### Google Cloud Console
62
61
63
-
Set up your project in the [[Google Cloud Console](https://developers.google.com/maps/documentation/elevation/cloud-setup). You'll enter a project name and billing information. Your website won't work as it should unless you enable billing, but you won't be charged if the API doesn't get called past the monthly limit (we'll teach you how to cap it at the limit, which is usually 10,000 API calls).
62
+
Set up your project in the [Google Cloud Console](https://developers.google.com/maps/documentation/elevation/cloud-setup). You'll enter a project name and billing information. Your website won't work as it should unless you enable billing, but you won't be charged if the API doesn't get called past the monthly limit (we'll teach you how to cap it at the limit, which is usually 10,000 API calls).
64
63
65
64
### Clone Template
66
65
@@ -87,22 +86,17 @@ Here's how to create and protect your API key:
87
86
3. Make sure you're on your cafe finder project and select the APIs & Services button.
88
87
4. On the left menu, click on library.
89
88
5. Search for the Places API (new) and click on it. Make sure the Places API is enabled.
90
-
6. Click on the hamburger menu and look for the APIs & Services button again. Then click on ****create****credentials****, then API key.
89
+
6. Click on the hamburger menu and look for the APIs & Services button again. Then click on **createcredentials**, then API key.
91
90
7. After your key is generated, click on edit key. Restrict your application to websites.
92
-
8. Add the domain your project is hosted on. In my case, mine is hosted on Codédex builds.
91
+
8. Add the domain your project is hosted on. In my case, mine is hosted on Codédex Builds.
93
92
94
93
Put your API key at the top of your JavaScript file. It should look like this:
95
94
96
95
```jsx
97
96
constapiKey="STRING_OF_RANDOM_CHARACTERS_HERE";
98
97
```
99
98
100
-
<aside>
101
-
⚠️
102
-
103
-
****Make sure your API key is restricted to just your project (step 7), or else anyone can use it freely and you'll get charged for it!****
104
-
105
-
</aside>
99
+
**⚠️ Warning:** Make sure your API key is restricted to just your project (step 7), or else anyone can use it freely and you'll get charged for it!
To see cafes nearby, the browser needs access to your location. There's a built-in JavaScript function that takes care of that for you called ``useLocation()`` and it takes your device's latitude and longitude coordinates.
Next, we'll check if any location data has been cached (saved to the browser) and is less than 10 minutes old.
129
123
130
124
```jsx
131
125
if (cache.timestamp&& now -cache.timestamp<10*60*1000) {
132
-
useLocation(cache.lat, cache.lng);
133
-
}
126
+
useLocation(cache.lat, cache.lng);
127
+
}
134
128
```
135
129
136
130
If there's no cached location information found, the browser pulls your current latitude and longitude from a built-in function.
137
131
138
132
```jsx
139
133
else {
140
-
navigator.geolocation.getCurrentPosition(pos=> {
141
-
constlat=pos.coords.latitude;
142
-
constlng=pos.coords.longitude;
134
+
navigator.geolocation.getCurrentPosition(pos=> {
135
+
constlat=pos.coords.latitude;
136
+
constlng=pos.coords.longitude;
143
137
```
144
138
145
-
We'll save the location and timestamp in ``localStorage`` in case we use our cafe finder website in the near future. Then we'll add error handling - an alert that lets you know if your location can't be accessed.
139
+
We'll save the location and timestamp in `localStorage` in case we use our cafe finder website in the near future. Then we'll add error handling - an alert that lets you know if your location can't be accessed.
146
140
147
141
```jsx
148
-
localStorage.setItem('cachedLocation', JSON.stringify({ lat, lng, timestamp: now }));
149
-
useLocation(lat, lng);
150
-
}, () =>alert("Location access denied or unavailable."));
142
+
localStorage.setItem('cachedLocation', JSON.stringify({ lat, lng, timestamp: now }));
143
+
useLocation(lat, lng);
144
+
}, () =>alert("Location access denied or unavailable."));
151
145
```
152
146
153
147
### Using the API
@@ -166,8 +160,8 @@ We'll call the API to find nearby cafes and *_fetch_* their urls.
166
160
167
161
```jsx
168
162
try {
169
-
constresponse=awaitfetch(url);
170
-
constdata=awaitresponse.json();
163
+
constresponse=awaitfetch(url);
164
+
constdata=awaitresponse.json();
171
165
```
172
166
173
167
If the API finds results for nearby cafes, we'll take that data and insert it into cards (we'll write the `displayCards` function next!)
@@ -200,8 +194,8 @@ We'll start by creating a container that takes the first saved cafe in our cafe
200
194
We'll then make a for loop that iterates through the cafe list to…
201
195
202
196
- Create a new div to wrap each cafe card
203
-
- Add a class (``swipe-wrapper``) to each card to keep card styles consistent
204
-
- Adjust the card's ****z-index**** (display order) so that new cards appear under old ones
197
+
- Add a class (`swipe-wrapper`) to each card to keep card styles consistent
198
+
- Adjust the card's **z-index** (display order) so that new cards appear under old ones
205
199
206
200
```jsx
207
201
cafes.forEach((cafe, i) => {
@@ -239,7 +233,7 @@ const cafeData = {
239
233
};
240
234
```
241
235
242
-
Putting in tags between the ````` symbol when changing an element's ``innerHTML`` allows you to render HTML elements when functions run. We'll append (connect) the card to its wrapper, and append its wrapper to its container.
236
+
Putting in tags between the `````` symbol when changing an element's `innerHTML` allows you to render HTML elements when functions run. We'll append (connect) the card to its wrapper, and append its wrapper to its container.
0 commit comments