Skip to content
106 changes: 46 additions & 60 deletions samples/ui-kit-place-details-compact/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,57 @@
<!doctype html>
<html>
<head>
<title>Click on the map to view place details</title>
<title>Place Details Compact with Google Maps</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" />
<script type="module" src="./index.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="module" src="./index.js" defer></script>
<!-- prettier-ignore -->
<script>
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});
</script>
</head>
<body>
<!--[START maps_ui_kit_place_details_compact_container]-->
<div id="map-container"></div>
<gmp-place-details-compact orientation="horizontal">
<!--[START maps_ui_kit_place_details_compact_request]-->
<div class="container">
<!-- map-id is required to use Advanced Markers. See https://developers.google.com/maps/documentation/javascript/map-ids/mapid-over. -->
<gmp-map zoom="17" map-id="DEMO_MAP_ID">
<gmp-advanced-marker></gmp-advanced-marker>
</gmp-map>
</div>
<!--
The gmp-place-details-compact element is styled inline because it is
conditionally rendered and moved into the info window, which is
part of the map's shadow DOM.
-->
<!--[START maps_ui_kit_place_details_compact_map_setup] -->
<gmp-place-details-compact
orientation="horizontal"
truncation-preferred
style="
width: 400px;
padding: 0;
margin: 0;
border: none;
background-color: transparent;
color-scheme: light;
">
<gmp-place-details-place-request
place="ChIJn97JQNpC1moRIcJsVMEQLI8"></gmp-place-details-place-request>
<!--[END maps_ui_kit_place_details_compact_request]-->
<!--[START maps_ui_kit_place_details_compact_config]-->
<gmp-place-all-content></gmp-place-all-content>
<!--[END maps_ui_kit_place_details_compact_config]-->
place="ChIJC8HakaIRkFQRiOgkgdHmqkk"></gmp-place-details-place-request>
<!--[END maps_ui_kit_place_details_compact_map_setup] -->
<!--[START maps_ui_kit_place_details_compact_config] -->
<gmp-place-content-config>
<gmp-place-media lightbox-preferred></gmp-place-media>
<gmp-place-rating></gmp-place-rating>
<gmp-place-type></gmp-place-type>
<gmp-place-price></gmp-place-price>
<gmp-place-accessible-entrance-icon></gmp-place-accessible-entrance-icon>
<gmp-place-open-now-status></gmp-place-open-now-status>
<gmp-place-attribution
light-scheme-color="gray"
dark-scheme-color="white"></gmp-place-attribution>
</gmp-place-content-config>
<!--[END maps_ui_kit_place_details_compact_config] -->
</gmp-place-details-compact>
<!--[END maps_ui_kit_place_details_compact_container]-->
<script>
((g) => {
var h,
a,
k,
p = 'The Google Maps JavaScript API',
c = 'google',
l = 'importLibrary',
q = '__ib__',
m = document,
b = window;
b = b[c] || (b[c] = {});
var d = b.maps || (b.maps = {}),
r = new Set(),
e = new URLSearchParams(),
u = () =>
h ||
(h = new Promise(async (f, n) => {
await (a = m.createElement('script'));
e.set('libraries', [...r] + '');
for (k in g)
e.set(
k.replace(
/[A-Z]/g,
(t) => '_' + t[0].toLowerCase()
),
g[k]
);
e.set('callback', c + '.maps.' + q);
a.src =
`https://maps.${c}apis.com/maps/api/js?` + e;
d[q] = f;
a.onerror = () =>
(h = n(Error(p + ' could not load.')));
a.nonce =
m.querySelector('script[nonce]')?.nonce || '';
m.head.append(a);
}));
d[l]
? console.warn(p + ' only loads once. Ignoring:', g)
: (d[l] = (f, ...n) =>
r.add(f) && u().then(() => d[l](f, ...n)));
})({
key: 'AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8',
v: 'weekly',
});
</script>
</body>
</html>
<!--[END maps_ui_kit_place_details_compact] -->
98 changes: 50 additions & 48 deletions samples/ui-kit-place-details-compact/index.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,80 @@
'use strict';
/*
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* [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<void> {
const { PlaceDetailsCompactElement, PlaceDetailsPlaceRequestElement } =
(await google.maps.importLibrary('places')) as any;
const { Map } = (await google.maps.importLibrary(
// Request needed libraries.
Promise.all([
google.maps.importLibrary('marker'),
google.maps.importLibrary('places'),
]);
const { InfoWindow } = (await google.maps.importLibrary(
'maps'
)) as google.maps.MapsLibrary;

const { AdvancedMarkerElement } = (await google.maps.importLibrary(
'marker'
)) as google.maps.MarkerLibrary;
gMap = new Map(mapContainer, { mapId: 'DEMO_MAP_ID' });
marker = new AdvancedMarkerElement({ map: gMap });
await window.customElements.whenDefined('gmp-map');
// Set the inner map options.
map.innerMap.setOptions({
mapTypeControl: false,
streetViewControl: false,
});

await window.customElements.whenDefined('gmp-advanced-marker');
marker.collisionBehavior =
google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL;

const infoWindow = new InfoWindow();
infoWindow.addListener('close', () => {
marker.position = null;
});

// Hide the map type control.
gMap.setOptions({ mapTypeControl: false });
const showInfoWindow = () => {
if (infoWindow.isOpen) return;
infoWindow.setContent(placeDetails);
infoWindow.open({ anchor: marker });
};

// Set up map, marker, and infowindow once widget is loaded.
placeDetails.style.visibility = 'visible';
placeDetails.addEventListener('gmp-load', (event) => {
console.log('placeDetails initialized!');
updateMapAndMarker();
// 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.
// (The clicked POI LatLng will be a more natural marker position, when available.)
if (!map.center) {
map.center = marker.position = placeDetails.place.location;
showInfoWindow();
}
});

/* [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) => {
event.stop();
// Fire when the user clicks on a POI.

if (event.placeId) {
console.log('clicked on POI');
console.log(event.placeId);
// When the user clicks a POI.
marker.position = event.latLng;
placeDetailsRequest.place = event.placeId;
updateMapAndMarker();
showInfoWindow();
} else {
// Fire when the user clicks the map (not on a POI).
// When the user clicks the map (not on a POI).
marker.position = null;
placeDetailsRequest.place = null;
console.log('No place was selected.');
}
});
// 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] */
56 changes: 10 additions & 46 deletions samples/ui-kit-place-details-compact/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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] */
Loading