|
| 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] |
0 commit comments