-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (94 loc) · 2.63 KB
/
index.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
const express = require("express");
const cors = require("cors");
const app = express();
const axios = require("axios");
const dotenv = require("dotenv");
dotenv.config();
app.use(cors());
app.use(express.json());
const port = 5000;
app.get("/restaurants", async (req, res) => {
const { location } = req.query;
const resultsPerPage = 20;
let results = [];
let next_page_token = "";
const { data } = await axios.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json",
{
params: {
type: "restaurant",
location,
radius: 15 * 1609.34,
key: process.env.GOOGLE_PLACES_API_KEY,
},
}
);
results = results.concat(data.results);
next_page_token = data.next_page_token;
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
while (next_page_token) {
await delay(2000);
const nextPage = await axios.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json",
{
params: {
pagetoken: next_page_token,
type: "restaurant",
location,
radius: 1609.34 * 15,
key: process.env.GOOGLE_PLACES_API_KEY,
},
}
);
results = results.concat(nextPage.data.results);
next_page_token = nextPage.data.next_page_token;
}
const requests = results.map((result) => {
const photo_reference =
result.photos && result.photos.length > 0
? result.photos[0].photo_reference
: null;
const mapUrl = `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(
result.vicinity
)}`;
if (photo_reference) {
return axios
.get("https://maps.googleapis.com/maps/api/place/photo", {
params: {
maxwidth: 400,
photoreference: photo_reference,
key: process.env.GOOGLE_PLACES_API_KEY,
},
})
.then((response) => ({
...result,
mapUrl,
address: result.vicinity,
photo: response.request.res.responseUrl,
}));
} else {
return {
...result,
mapUrl,
address: result.vicinity,
photo: null,
};
}
});
const restaurants = await axios.all(requests);
res.json(restaurants);
});
app.get("/restaurants/details", async (req, res) => {
const { place_id } = req.query;
const { data } = await axios.get(
"https://maps.googleapis.com/maps/api/place/details/json",
{
params: {
place_id,
key: process.env.GOOGLE_PLACES_API_KEY,
},
}
);
res.json(data);
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));