|
| 1 | +/* |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +/* [START maps_ui_kit_advanced_place_details_compact] */ |
| 7 | +// Use querySelector to select elements for interaction. |
| 8 | +/* [START maps_ui_kit_advanced_place_details_compact_query_selector] */ |
| 9 | +const map = document.querySelector<google.maps.MapElement>('gmp-map')!; |
| 10 | +const placeDetails = document.querySelector< |
| 11 | + HTMLElement & { place?: google.maps.places.Place } |
| 12 | +>('gmp-advanced-place-details-compact')!; |
| 13 | +const placeDetailsRequest = document.querySelector<HTMLElement>( |
| 14 | + 'gmp-place-details-place-request' |
| 15 | +)!; |
| 16 | +const marker = document.querySelector<google.maps.marker.AdvancedMarkerElement>( |
| 17 | + 'gmp-advanced-marker' |
| 18 | +)!; |
| 19 | +/* [END maps_ui_kit_advanced_place_details_compact_query_selector] */ |
| 20 | +async function init(): Promise<void> { |
| 21 | + // Request needed libraries. |
| 22 | + void Promise.all([ |
| 23 | + google.maps.importLibrary('marker'), |
| 24 | + google.maps.importLibrary('places'), |
| 25 | + ]); |
| 26 | + const { InfoWindow } = await google.maps.importLibrary('maps'); |
| 27 | + |
| 28 | + await window.customElements.whenDefined('gmp-map'); |
| 29 | + // Set the inner map options. |
| 30 | + map.innerMap.setOptions({ |
| 31 | + mapTypeControl: false, |
| 32 | + streetViewControl: false, |
| 33 | + }); |
| 34 | + |
| 35 | + await window.customElements.whenDefined('gmp-advanced-marker'); |
| 36 | + marker.collisionBehavior = 'REQUIRED_AND_HIDES_OPTIONAL'; |
| 37 | + |
| 38 | + const infoWindow = new InfoWindow(); |
| 39 | + infoWindow.addListener('close', () => { |
| 40 | + marker.position = null; |
| 41 | + }); |
| 42 | + |
| 43 | + const showInfoWindow = () => { |
| 44 | + if (infoWindow.isOpen) return; |
| 45 | + infoWindow.setContent(placeDetails); |
| 46 | + infoWindow.open({ anchor: marker }); |
| 47 | + }; |
| 48 | + |
| 49 | + placeDetails.addEventListener('gmp-load', () => { |
| 50 | + // For the initial load case, with no user click, we fall back to the place's location, and ensure the map has a center set and the InfoWindow is show. |
| 51 | + // (The clicked POI LatLng will be a more natural marker position, when available.) |
| 52 | + if (!map.center && placeDetails.place?.location) { |
| 53 | + map.center = marker.position = placeDetails.place.location; |
| 54 | + showInfoWindow(); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + /* [START maps_ui_kit_advanced_place_details_compact_event] */ |
| 59 | + // Add an event listener to handle clicks. |
| 60 | + map.innerMap.addListener( |
| 61 | + 'click', |
| 62 | + (event: google.maps.MapMouseEvent | google.maps.IconMouseEvent) => { |
| 63 | + event.stop(); |
| 64 | + |
| 65 | + if ('placeId' in event && event.placeId) { |
| 66 | + // When the user clicks a POI. |
| 67 | + marker.position = event.latLng; |
| 68 | + placeDetailsRequest.setAttribute( |
| 69 | + 'place', |
| 70 | + `places/${event.placeId}` |
| 71 | + ); |
| 72 | + showInfoWindow(); |
| 73 | + } else { |
| 74 | + // When the user clicks the map (not on a POI). |
| 75 | + marker.position = null; |
| 76 | + placeDetailsRequest.removeAttribute('place'); |
| 77 | + console.log('No place was selected.'); |
| 78 | + } |
| 79 | + } |
| 80 | + ); |
| 81 | +} |
| 82 | +/* [END maps_ui_kit_advanced_place_details_compact_event] */ |
| 83 | +void init(); |
| 84 | +/* [END maps_ui_kit_advanced_place_details_compact] */ |
0 commit comments