1
+ /**
2
+ * @license
3
+ * Copyright 2024 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 ( 'places' ) as google . maps . PlacesLibrary ;
10
+
11
+ // Use a place ID to create a new Place instance.
12
+ const place = new Place ( {
13
+ id : 'ChIJydSuSkkUkFQRsqhB-cEtYnw' , // Woodland Park Zoo, Seattle WA
14
+ } ) ;
15
+
16
+ // Call fetchFields, passing the desired data fields.
17
+ await place . fetchFields ( { fields : [ 'displayName' , 'photos' , 'editorialSummary' ] } ) ;
18
+
19
+ // Get the various HTML elements.
20
+ let heading = document . getElementById ( 'heading' ) as HTMLElement ;
21
+ let summary = document . getElementById ( 'summary' ) as HTMLElement ;
22
+ let gallery = document . getElementById ( 'gallery' ) as HTMLElement ;
23
+ let expandedImageDiv = document . getElementById ( 'expanded-image' ) as HTMLElement ;
24
+ let attributionLabel ;
25
+
26
+ // Show the display name and summary for the place.
27
+ heading . textContent = place . displayName as string ;
28
+ summary . textContent = place . editorialSummary as string ;
29
+
30
+ // Add photos to the gallery.
31
+ if ( place . photos ) {
32
+ place . photos ?. forEach ( ( photo ) => {
33
+ const img = document . createElement ( 'img' ) ;
34
+ const expandedImage = document . createElement ( 'img' ) ;
35
+ img . src = photo . getURI ( { maxHeight : 380 } ) ;
36
+ img . addEventListener ( 'click' , ( event ) => {
37
+ event . preventDefault ( ) ;
38
+ expandedImage . src = img . src ;
39
+ expandedImageDiv . innerHTML = '' ;
40
+ expandedImageDiv . appendChild ( expandedImage ) ;
41
+ attributionLabel = createAttribution ( photo . authorAttributions ) ;
42
+ expandedImageDiv . appendChild ( attributionLabel ) ;
43
+ } ) ;
44
+
45
+ gallery . appendChild ( img ) ;
46
+ } ) ;
47
+ }
48
+
49
+ // Display the first photo.
50
+ const img = document . createElement ( 'img' ) ;
51
+ img . src = place . photos ! [ 0 ] . getURI ( ) ;
52
+ expandedImageDiv . appendChild ( img ) ;
53
+ attributionLabel = createAttribution ( place . photos ! [ 0 ] . authorAttributions ) ;
54
+ expandedImageDiv . appendChild ( attributionLabel ) ;
55
+
56
+ // Helper function to create attribution DIV.
57
+ function createAttribution ( attribution ) {
58
+ attributionLabel = document . createElement ( "a" ) ;
59
+ attributionLabel . classList . add ( 'attribution-label' ) ;
60
+ attributionLabel . textContent = attribution [ 0 ] . displayName ;
61
+ attributionLabel . href = attribution [ 0 ] . uri ;
62
+ attributionLabel . target = '_blank;'
63
+ return attributionLabel ;
64
+ }
65
+ }
66
+
67
+ init ( ) ;
68
+ // [END maps_place_photos]
0 commit comments