diff --git a/samples/ui-kit-place-details-compact/index.html b/samples/ui-kit-place-details-compact/index.html index 1385994a..65365920 100644 --- a/samples/ui-kit-place-details-compact/index.html +++ b/samples/ui-kit-place-details-compact/index.html @@ -7,71 +7,60 @@ - Click on the map to view place details + Place Details Compact with Google Maps - - + + + + - -
- - +
+ + + + +
+ + + - - - - + place="ChIJC8HakaIRkFQRiOgkgdHmqkk"> + + + + + + + + + + + + - - diff --git a/samples/ui-kit-place-details-compact/index.ts b/samples/ui-kit-place-details-compact/index.ts index e03d8d25..44938065 100644 --- a/samples/ui-kit-place-details-compact/index.ts +++ b/samples/ui-kit-place-details-compact/index.ts @@ -5,74 +5,72 @@ */ /* [START maps_ui_kit_place_details_compact] */ // Use querySelector to select elements for interaction. -const mapContainer = document.getElementById('map-container') as any; +/* [START maps_ui_kit_place_details_compact_query_selector] */ +const map = document.querySelector('gmp-map') as google.maps.MapElement; const placeDetails = document.querySelector('gmp-place-details-compact') as any; const placeDetailsRequest = document.querySelector( 'gmp-place-details-place-request' ) as any; -let gMap; -let marker; +const marker = document.querySelector( + 'gmp-advanced-marker' +) as google.maps.marker.AdvancedMarkerElement; +/* [END maps_ui_kit_place_details_compact_query_selector] */ async function initMap(): Promise { - const { PlaceDetailsCompactElement, PlaceDetailsPlaceRequestElement } = - (await google.maps.importLibrary('places')) as any; - const { Map } = (await google.maps.importLibrary( - 'maps' - )) as google.maps.MapsLibrary; + // Request needed libraries. + await Promise.all([ + google.maps.importLibrary('maps'), + google.maps.importLibrary('marker'), + google.maps.importLibrary('places'), + ]); - const { AdvancedMarkerElement } = (await google.maps.importLibrary( - 'marker' - )) as google.maps.MarkerLibrary; - gMap = new Map(mapContainer, { mapId: 'DEMO_MAP_ID' }); - marker = new AdvancedMarkerElement({ map: gMap }); + // Set the map options. + map.innerMap.setOptions({ + mapTypeControl: false, + streetViewControl: false, + }); + + // Set up the info window. + const infoWindow = new google.maps.InfoWindow({}); - // Hide the map type control. - gMap.setOptions({ mapTypeControl: false }); + // Function to update map and marker based on place details + const updateMapAndMarker = () => { + if (placeDetails.place && placeDetails.place.location) { + const placeViewport = placeDetails.place.viewport; + map.innerMap.fitBounds(placeViewport, 0); + marker.position = placeDetails.place.location; + marker.collisionBehavior = + google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL; + marker.style.display = 'block'; + infoWindow.close(); + infoWindow.setContent(placeDetails); + infoWindow.open({ map: map.innerMap, anchor: marker }); + } + }; - // Set up map, marker, and infowindow once widget is loaded. - placeDetails.style.visibility = 'visible'; + // Set up map once widget is loaded. placeDetails.addEventListener('gmp-load', (event) => { - console.log('placeDetails initialized!'); updateMapAndMarker(); }); + + /* [START maps_ui_kit_place_details_compact_event] */ // Add an event listener to handle clicks. - gMap.addListener('click', async (event) => { + map.innerMap.addListener('click', async (event) => { + marker.position = null; event.stop(); - // Fire when the user clicks on a POI. if (event.placeId) { - console.log('clicked on POI'); - console.log(event.placeId); + // Fire when the user clicks a POI. placeDetailsRequest.place = event.placeId; updateMapAndMarker(); } else { // Fire when the user clicks the map (not on a POI). console.log('No place was selected.'); + marker.style.display = 'none'; + infoWindow.close(); } }); - // Function to update map, marker, and infowindow based on place details - const updateMapAndMarker = () => { - console.log('function called'); - if (placeDetails.place && placeDetails.place.location) { - marker.gMap = null; - let adjustedCenter = offsetLatLngRight( - placeDetails.place.location, - 0.002 - ); - gMap.panTo(adjustedCenter); - gMap.setZoom(16); // Set zoom after panning if needed - marker.content = placeDetails; - marker.position = placeDetails.place.location; - } else { - console.log('else'); - } - }; -} -// Helper function to offset marker placement for better visual appearance. -function offsetLatLngRight(latLng, latitudeOffset) { - const newLat = latLng.lat() + latitudeOffset; - return new google.maps.LatLng(newLat, latLng.lng()); } +/* [END maps_ui_kit_place_details_compact_event] */ initMap(); - /* [END maps_ui_kit_place_details_compact] */ diff --git a/samples/ui-kit-place-details-compact/style.css b/samples/ui-kit-place-details-compact/style.css index cfabe2ea..58d94bda 100644 --- a/samples/ui-kit-place-details-compact/style.css +++ b/samples/ui-kit-place-details-compact/style.css @@ -4,59 +4,23 @@ * SPDX-License-Identifier: Apache-2.0 */ /* [START maps_ui_kit_place_details_compact] */ +/* + * Optional: Makes the sample page fill the window. + */ html, body { - display: flex; - width: 100%; - height: 400px; + height: 100%; margin: 0; + padding: 0; } -h1 { - font-size: 16px; - text-align: center; -} - -#map-container { - box-sizing: border-box; +.container { + display: flex; + height: 100vh; width: 100%; } -/* [START maps_ui_kit_place_details_compact_style] */ -gmp-place-details-compact { - /* Sets the color for text and icons on the surface */ - /* Adapts automatically to the user's system light/dark mode preference */ - --gmp-mat-color-on-surface: light-dark(black, white); - /* Sets the background color of the surface */ - /* Adapts automatically to the user's system light/dark mode preference */ - --gmp-mat-color-surface: light-dark(white, black); - /* Defines the primary font stack used within the component */ - --gmp-mat-font-family: Google Sans Text, sans-serif; - /* Defines the style for medium body text (e.g., address, descriptions) */ - --gmp-mat-font-body-medium: normal 400 0.875em/1.25em - var(--gmp-mat-font-family, 'Google Sans Text'); - - width: 360px; - border: none; - padding: 0; - margin: 0; - position: relative; - transform: translate(0, calc(-20px)); -} -/* This creates the pointer attached to the bottom of the element. */ -gmp-place-details-compact::after { - content: ''; - position: absolute; - top: 100%; - left: 50%; - transform: translateX(-50%); - width: 0; - height: 0; - border-left: 16px solid transparent; - border-right: 16px solid transparent; - border-top: 20px solid - var(--gmp-mat-color-surface, light-dark(white, black)); +gmp-map { + flex-grow: 1; } - -/* [END maps_ui_kit_place_details_compact_style] */ /* [END maps_ui_kit_place_details_compact] */