Skip to content

Commit 7a20954

Browse files
authored
feat: Migrates the Place Reviews sample. (#992)
* feat: Migrates the Place Reviews sample. * Incorporate review comments
1 parent 152788a commit 7a20954

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

samples/place-reviews/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Google Maps JavaScript Sample
2+
3+
## place-reviews
4+
5+
Demonstrates retrieving place reviews.
6+
7+
## Setup
8+
9+
### Before starting run:
10+
11+
`npm i`
12+
13+
### Run an example on a local web server
14+
15+
`cd samples/place-reviews`
16+
`npm start`
17+
18+
### Build an individual example
19+
20+
`cd samples/place-reviews`
21+
`npm run build`
22+
23+
From 'samples':
24+
25+
`npm run build --workspace=place-reviews/`
26+
27+
### Build all of the examples.
28+
29+
From 'samples':
30+
31+
`npm run build-all`
32+
33+
### Run lint to check for problems
34+
35+
`cd samples/place-reviews`
36+
`npx eslint index.ts`
37+
38+
## Feedback
39+
40+
For feedback related to this sample, please open a new issue on
41+
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).

samples/place-reviews/index.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright 2025 Google LLC. All Rights Reserved.
5+
SPDX-License-Identifier: Apache-2.0
6+
-->
7+
<!-- [START maps_place_reviews] -->
8+
<html>
9+
<head>
10+
<title>Place Reviews</title>
11+
12+
<link rel="stylesheet" type="text/css" href="./style.css" />
13+
<script type="module" src="./index.js"></script>
14+
<!-- prettier-ignore -->
15+
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
16+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
17+
</head>
18+
<body>
19+
<gmp-map
20+
center="42.349134, -71.083184"
21+
zoom="14"
22+
map-id="4504f8b37365c3d0">
23+
</gmp-map>
24+
</body>
25+
</html>
26+
<!-- [END maps_place_reviews] -->

samples/place-reviews/index.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_place_reviews]
8+
let innerMap;
9+
let infoWindow;
10+
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement;
11+
12+
async function initMap() {
13+
// Import the needed libraries.
14+
const [{ InfoWindow }, { AdvancedMarkerElement }, { Place }] =
15+
await Promise.all([
16+
google.maps.importLibrary(
17+
'maps'
18+
) as Promise<google.maps.MapsLibrary>,
19+
google.maps.importLibrary(
20+
'marker'
21+
) as Promise<google.maps.MarkerLibrary>,
22+
google.maps.importLibrary(
23+
'places'
24+
) as Promise<google.maps.PlacesLibrary>,
25+
]);
26+
27+
innerMap = mapElement.innerMap;
28+
29+
// [START maps_place_reviews_get_first]
30+
// Create a new Place instance.
31+
const place = new Place({
32+
id: 'ChIJpyiwa4Zw44kRBQSGWKv4wgA', // Faneuil Hall Marketplace, Boston, MA
33+
});
34+
35+
// Call fetchFields, passing 'reviews' and other needed fields.
36+
await place.fetchFields({
37+
fields: ['displayName', 'formattedAddress', 'location', 'reviews'],
38+
});
39+
40+
// Create an HTML container.
41+
const content = document.createElement('div');
42+
const title = document.createElement('div');
43+
const rating = document.createElement('div');
44+
const address = document.createElement('div');
45+
const review = document.createElement('div');
46+
const authorLink = document.createElement('a');
47+
48+
// If there are any reviews display the first one.
49+
if (place.reviews && place.reviews.length > 0) {
50+
// Get info for the first review.
51+
const reviewRating = place.reviews[0].rating;
52+
const reviewText = place.reviews[0].text;
53+
const authorName = place.reviews[0].authorAttribution!.displayName;
54+
const authorUri = place.reviews[0].authorAttribution!.uri;
55+
56+
// Safely populate the HTML.
57+
title.textContent = place.displayName || '';
58+
address.textContent = place.formattedAddress || '';
59+
rating.textContent = `Rating: ${reviewRating} stars`;
60+
review.textContent = reviewText || '';
61+
authorLink.textContent = authorName;
62+
authorLink.href = authorUri || '';
63+
authorLink.target = '_blank';
64+
65+
content.appendChild(title);
66+
content.appendChild(address);
67+
content.appendChild(rating);
68+
content.appendChild(review);
69+
content.appendChild(authorLink);
70+
} else {
71+
content.textContent =
72+
`No reviews were found for ${place.displayName}.`;
73+
}
74+
75+
// Create an infowindow to display the review.
76+
infoWindow = new InfoWindow({
77+
content,
78+
ariaLabel: place.displayName,
79+
});
80+
// [END maps_place_reviews_get_first]
81+
82+
// Add a marker.
83+
const marker = new AdvancedMarkerElement({
84+
map: innerMap,
85+
position: place.location,
86+
title: place.displayName,
87+
});
88+
89+
innerMap.setCenter(place.location);
90+
91+
// Show the info window.
92+
infoWindow.open({
93+
anchor: marker,
94+
map: innerMap,
95+
});
96+
}
97+
98+
initMap();
99+
// [END maps_place_reviews]

samples/place-reviews/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@js-api-samples/place-reviews",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh place-reviews && bash ../app.sh place-reviews && bash ../docs.sh place-reviews && npm run build:vite --workspace=. && bash ../dist.sh place-reviews",
6+
"test": "tsc && npm run build:vite --workspace=.",
7+
"start": "tsc && vite build --base './' && vite",
8+
"build:vite": "vite build --base './'",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {}
12+
}

samples/place-reviews/style.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/* [START maps_place_reviews] */
7+
/*
8+
* Always set the map height explicitly to define the size of the div element
9+
* that contains the map.
10+
*/
11+
gmp-map {
12+
height: 100%;
13+
}
14+
15+
/*
16+
* Optional: Makes the sample page fill the window.
17+
*/
18+
html,
19+
body {
20+
height: 100%;
21+
margin: 0;
22+
padding: 0;
23+
}
24+
25+
/* [END maps_place_reviews] */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"module": "esnext",
4+
"target": "esnext",
5+
"strict": true,
6+
"noImplicitAny": false,
7+
"lib": ["es2015", "esnext", "es6", "dom", "dom.iterable"],
8+
"moduleResolution": "Node",
9+
"jsx": "preserve"
10+
}
11+
}

0 commit comments

Comments
 (0)