|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// [START maps_place_photos] |
| 8 | +async function init() { |
| 9 | + const { Place } = (await google.maps.importLibrary( |
| 10 | + 'places' |
| 11 | + )) as google.maps.PlacesLibrary; |
| 12 | + |
| 13 | + // Use a place ID to create a new Place instance. |
| 14 | + const place = new Place({ |
| 15 | + id: 'ChIJydSuSkkUkFQRsqhB-cEtYnw', // Woodland Park Zoo, Seattle WA |
| 16 | + }); |
| 17 | + |
| 18 | + // Call fetchFields, passing the desired data fields. |
| 19 | + await place.fetchFields({ |
| 20 | + fields: ['displayName', 'photos', 'editorialSummary'], |
| 21 | + }); |
| 22 | + |
| 23 | + // Get the various HTML elements. |
| 24 | + const heading = document.getElementById('heading') as HTMLElement; |
| 25 | + const summary = document.getElementById('summary') as HTMLElement; |
| 26 | + const gallery = document.getElementById('gallery') as HTMLElement; |
| 27 | + const expandedImageDiv = document.getElementById( |
| 28 | + 'expanded-image' |
| 29 | + ) as HTMLElement; |
| 30 | + |
| 31 | + // Show the display name and summary for the place. |
| 32 | + heading.textContent = place.displayName as string; |
| 33 | + summary.textContent = place.editorialSummary as string; |
| 34 | + |
| 35 | + // Add photos to the gallery. |
| 36 | + place.photos?.forEach((photo) => { |
| 37 | + const altText = 'Photo of ' + place.displayName; |
| 38 | + const img = document.createElement('img'); |
| 39 | + const imgButton = document.createElement('button'); |
| 40 | + const expandedImage = document.createElement('img'); |
| 41 | + img.src = photo?.getURI({ maxHeight: 380 }); |
| 42 | + img.alt = altText; |
| 43 | + imgButton.addEventListener('click', (event) => { |
| 44 | + centerSelectedThumbnail(imgButton); |
| 45 | + event.preventDefault(); |
| 46 | + expandedImage.src = img.src; |
| 47 | + expandedImage.alt = altText; |
| 48 | + expandedImageDiv.innerHTML = ''; |
| 49 | + expandedImageDiv.appendChild(expandedImage); |
| 50 | + const attributionLabel = createAttribution( |
| 51 | + photo.authorAttributions[0] |
| 52 | + )!; |
| 53 | + expandedImageDiv.appendChild(attributionLabel); |
| 54 | + }); |
| 55 | + |
| 56 | + imgButton.addEventListener('focus', () => { |
| 57 | + centerSelectedThumbnail(imgButton); |
| 58 | + }); |
| 59 | + |
| 60 | + imgButton.appendChild(img); |
| 61 | + gallery.appendChild(imgButton); |
| 62 | + }); |
| 63 | + |
| 64 | + // Display the first photo. |
| 65 | + if (place.photos && place.photos.length > 0) { |
| 66 | + const photo = place.photos[0]; |
| 67 | + const img = document.createElement('img'); |
| 68 | + img.alt = 'Photo of ' + place.displayName; |
| 69 | + img.src = photo.getURI(); |
| 70 | + expandedImageDiv.appendChild(img); |
| 71 | + |
| 72 | + if (photo.authorAttributions && photo.authorAttributions.length > 0) { |
| 73 | + expandedImageDiv.appendChild( |
| 74 | + createAttribution(photo.authorAttributions[0]) |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Helper function to create attribution DIV. |
| 80 | + function createAttribution( |
| 81 | + attribution: google.maps.places.AuthorAttribution |
| 82 | + ) { |
| 83 | + const attributionLabel = document.createElement('a'); |
| 84 | + attributionLabel.classList.add('attribution-label'); |
| 85 | + attributionLabel.textContent = attribution.displayName; |
| 86 | + attributionLabel.href = attribution.uri!; |
| 87 | + attributionLabel.target = '_blank'; |
| 88 | + attributionLabel.rel = 'noopener noreferrer'; |
| 89 | + return attributionLabel; |
| 90 | + } |
| 91 | + |
| 92 | + // Helper function to center the selected thumbnail in the gallery. |
| 93 | + function centerSelectedThumbnail(element: HTMLElement) { |
| 94 | + element.scrollIntoView({ |
| 95 | + behavior: 'smooth', |
| 96 | + block: 'center', |
| 97 | + inline: 'center', |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +init(); |
| 103 | +// [END maps_place_photos] |
0 commit comments