|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// [START maps_ai_powered_summaries_basic] |
| 8 | +const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; |
| 9 | +let innerMap; |
| 10 | +let infoWindow; |
| 11 | + |
| 12 | +async function initMap() { |
| 13 | + const { Map, InfoWindow } = (await google.maps.importLibrary( |
| 14 | + 'maps' |
| 15 | + )) as google.maps.MapsLibrary; |
| 16 | + |
| 17 | + innerMap = mapElement.innerMap; |
| 18 | + infoWindow = new InfoWindow(); |
| 19 | + getPlaceDetails(); |
| 20 | +} |
| 21 | + |
| 22 | +async function getPlaceDetails() { |
| 23 | + // Request needed libraries. |
| 24 | + const [ {AdvancedMarkerElement}, { Place } ] = await Promise.all([ |
| 25 | + google.maps.importLibrary('marker') as Promise<google.maps.MarkerLibrary>, |
| 26 | + google.maps.importLibrary('places') as Promise<google.maps.PlacesLibrary>, |
| 27 | + ]); |
| 28 | + |
| 29 | + // [START maps_ai_powered_summaries_basic_placeid] |
| 30 | + // Use place ID to create a new Place instance. |
| 31 | + const place = new Place({ |
| 32 | + id: 'ChIJzzc-aWUM3IARPOQr9sA6vfY', // San Diego Botanic Garden |
| 33 | + }); |
| 34 | + // [END maps_ai_powered_summaries_basic_placeid] |
| 35 | + |
| 36 | + // Call fetchFields, passing the needed data fields. |
| 37 | + // [START maps_ai_powered_summaries_basic_fetchfields] |
| 38 | + await place.fetchFields({ |
| 39 | + fields: [ |
| 40 | + 'displayName', |
| 41 | + 'formattedAddress', |
| 42 | + 'location', |
| 43 | + 'generativeSummary', |
| 44 | + ], |
| 45 | + }); |
| 46 | + // [END maps_ai_powered_summaries_basic_fetchfields] |
| 47 | + |
| 48 | + // Add an Advanced Marker |
| 49 | + const marker = new AdvancedMarkerElement({ |
| 50 | + map: innerMap, |
| 51 | + position: place.location, |
| 52 | + title: place.displayName, |
| 53 | + }); |
| 54 | + |
| 55 | + // Create a content container. |
| 56 | + const content = document.createElement('div'); |
| 57 | + // Populate the container with data. |
| 58 | + const address = document.createElement('div'); |
| 59 | + const summary = document.createElement('div'); |
| 60 | + const lineBreak = document.createElement('br'); |
| 61 | + |
| 62 | + // Retrieve the summary text and disclosure text. |
| 63 | + //@ts-ignore |
| 64 | + let overviewText = place.generativeSummary.overview ?? 'No summary is available.'; |
| 65 | + //@ts-ignore |
| 66 | + let disclosureText = place.generativeSummary.disclosureText ?? ''; |
| 67 | + |
| 68 | + address.textContent = place.formattedAddress ?? ''; |
| 69 | + summary.textContent = `${overviewText} [${disclosureText}]`; |
| 70 | + content.append(address, lineBreak, summary);; |
| 71 | + |
| 72 | + innerMap.setCenter(place.location); |
| 73 | + |
| 74 | + // Display an info window. |
| 75 | + infoWindow.setHeaderContent(place.displayName); |
| 76 | + infoWindow.setContent(content); |
| 77 | + infoWindow.open({ |
| 78 | + anchor: marker, |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +initMap(); |
| 83 | +// [END maps_ai_powered_summaries_basic] |
0 commit comments