-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplacesApi.js
More file actions
114 lines (105 loc) · 3.29 KB
/
Copy pathplacesApi.js
File metadata and controls
114 lines (105 loc) · 3.29 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
export class PlacesAPI {
constructor(apiKey, url) {
this.apiKey = apiKey;
this.url = url;
this.placeid = null;
}
// extract url to get review dict:
/* text:{author_name,
author_url,
language,
original_language,
profile_photo_rul,
rating,
relative_time_description,
text,
time,
translated}
*/
async getReviewsDict() {
try {
var reviewsDict = {};
if (this.placeid == null) {
this.placeid = await this.getPlaceId();
}
const reviews = await this.getReviews(this.placeid);
reviews.forEach(review => {
reviewsDict[review.text] = review;
});
return reviewsDict;
} catch (error) {
console.error(error);
}
}
// GET name, address, rating for a restaurant
async getPlaceInfo() {
try {
if (this.placeid == null) {
this.placeid = await this.getPlaceId();
}
const url = `https://maps.googleapis.com/maps/api/place/details/json?place_id=${this.placeid}&fields=name,rating,formatted_address&key=${this.apiKey}`;
const response = await fetch(url);
const data = await response.json();
const userInfo = {
name: data.result.name,
rating: data.result.rating,
address: data.result.formatted_address
};
return userInfo;
} catch (error) {
console.error(error)
}
return 0;
}
// retrieve place_id from url
async getPlaceId() {
const name = decodeURIComponent(this.url.split("/").pop().split("@")[0].replace(/\+/g, " "));
const lat = this.url.split(",")[0].split("@")[1];
const lng = this.url.split(",")[1];
const placeId = await this.getPlaceIdFromLatLng(lat, lng, name);
return placeId;
}
// GET request to retrieve place_id using lat and lng with correct name
async getPlaceIdFromLatLng(lat, lng, name) {
const url = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${lat},${lng}&radius=500&type=restaurant&key=${this.apiKey}`;
try {
const response = await fetch(url);
const data = await response.json();
var res = data.results[0].place_id;
data.results.forEach(function(result) {
if (result.name == name) {
res = result.place_id;
}
});
return res;
} catch (error) {
console.error(error)
}
return 0;
}
// GET request to retrieve all reviews and their info for placeId
async getReviews(placeId) {
const url = `https://maps.googleapis.com/maps/api/place/details/json?place_id=${placeId}&fields=reviews&key=${this.apiKey}`;
try {
const response = await fetch(url);
const data = await response.json();
return data.result.reviews;
} catch (error) {
console.error(error)
}
return 0;
}
}
// Sample usage:
/*
const apiKey = 'AIzaSyA2ft9Ti0ILphcvqQF3qvHvjY35LOhQxdk';
const url = "https://www.google.com/maps/place/Rose+Bakery+%26+Cafe/@43.4642986,-80.5231153,18z/data=!4m6!3m5!1s0x882b46ae49d69c17:0xd949247f34b9b696!8m2!3d43.55811!4d-79.642758!16s%2Fg%2F1tc_04hc";
const placesAPI = new PlacesAPI(apiKey,url);
async function myFunction() {
const review = await placesAPI.getReviewsDict();
console.log(review);
const placeinfo = await placesAPI.getPlaceInfo();
console.log(userinfo);
}
myFunction();
*/