Skip to content

Commit 723a9ca

Browse files
authored
Update build-a-cafe-finder-with-javascript.mdx
1 parent 1d31d15 commit 723a9ca

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

projects/build-a-cafe-finder-with-javascript/build-a-cafe-finder-with-javascript.mdx

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,16 @@ tags:
3434
uid="CgUQBagDrWWBRUuoh3mkyi3aedg1"
3535
/>
3636

37-
**Prerequisites: HTML, CSS, JavaScript**
38-
37+
**Prerequisites:** HTML, CSS, JavaScript
3938
**Read Time:** 30 minutes
4039

4140
## Introduction
4241

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)!
4443

4544
## About the Google Maps Platform
4645

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!
4847

4948
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!
5049

@@ -56,11 +55,11 @@ The [Places API](https://developers.google.com/maps/documentation/places/web-ser
5655

5756
### Workspace
5857

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/).
6059

6160
### Google Cloud Console
6261

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).
6463

6564
### Clone Template
6665

@@ -87,22 +86,17 @@ Here's how to create and protect your API key:
8786
3. Make sure you're on your cafe finder project and select the APIs & Services button.
8887
4. On the left menu, click on library.
8988
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 **create credentials**, then API key.
9190
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.
9392

9493
Put your API key at the top of your JavaScript file. It should look like this:
9594

9695
```jsx
9796
const apiKey = "STRING_OF_RANDOM_CHARACTERS_HERE";
9897
```
9998

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!
106100

107101
### cors-anywhere
108102

@@ -120,34 +114,34 @@ const proxy = "https://cors-anywhere.herokuapp.com/";
120114
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.
121115

122116
```jsx
123-
function getLocation() {
124-
const cache = JSON.parse(localStorage.getItem('cachedLocation') || '{}');
125-
const now = Date.now();
117+
function getLocation() {
118+
const cache = JSON.parse(localStorage.getItem('cachedLocation') || '{}');
119+
const now = Date.now();
126120
```
127121
128122
Next, we'll check if any location data has been cached (saved to the browser) and is less than 10 minutes old.
129123
130124
```jsx
131125
if (cache.timestamp && now - cache.timestamp < 10 * 60 * 1000) {
132-
useLocation(cache.lat, cache.lng);
133-
}
126+
useLocation(cache.lat, cache.lng);
127+
}
134128
```
135129
136130
If there's no cached location information found, the browser pulls your current latitude and longitude from a built-in function.
137131
138132
```jsx
139133
else {
140-
navigator.geolocation.getCurrentPosition(pos => {
141-
const lat = pos.coords.latitude;
142-
const lng = pos.coords.longitude;
134+
navigator.geolocation.getCurrentPosition(pos => {
135+
const lat = pos.coords.latitude;
136+
const lng = pos.coords.longitude;
143137
```
144138
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.
146140
147141
```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."));
151145
```
152146
153147
### Using the API
@@ -166,8 +160,8 @@ We'll call the API to find nearby cafes and *_fetch_* their urls.
166160
167161
```jsx
168162
try {
169-
const response = await fetch(url);
170-
const data = await response.json();
163+
const response = await fetch(url);
164+
const data = await response.json();
171165
```
172166
173167
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
200194
We'll then make a for loop that iterates through the cafe list to…
201195
202196
- 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
205199
206200
```jsx
207201
cafes.forEach((cafe, i) => {
@@ -239,7 +233,7 @@ const cafeData = {
239233
};
240234
```
241235
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.
243237
244238
```jsx
245239
card.innerHTML = `

0 commit comments

Comments
 (0)