Place Details 元素和 Place Details Compact 元素是用來算繪地點詳細資料的 HTML 元素:
-
PlaceDetailsElement
支援所有 可視化地點資料,且可能包含多張相片。 -
PlaceDetailsCompactElement
的設計目的是盡可能節省空間,並顯示簡潔的地點資訊,包括名稱、地址、評分等。也可能包含一張相片。
PlaceDetails 元素
PlaceDetailsElement
支援多種內容元素,包括完整營業時間、網站、電話號碼、編輯摘要、類型專屬重點、評論、加號代碼和功能清單。
如要在地圖上顯示地點詳細資料,請在 HTML 網頁的 gmp-map
元素中新增 gmp-place-details
元素。加入子元素 gmp-place-details-place-request
來選取地點。這可以是 Place 物件、地點 ID,或以「places/{place_id}」格式表示的地點資源名稱:
<gmp-map center="47.759737, -122.250632" zoom="16" map-id="DEMO_MAP_ID"> <div class="widget-container" slot="control-inline-start-block-start"> <gmp-place-details> <gmp-place-details-place-request place="ChIJC8HakaIRkFQRiOgkgdHmqkk"></gmp-place-details-place-request> <gmp-place-all-content></gmp-place-all-content> </gmp-place-details> </div> <gmp-advanced-marker></gmp-advanced-marker> </gmp-map>
設定內容
您可以使用巢狀 gmp-place-content-config
元素,選取並設定地點詳細資料,藉此控管 gmp-place-details
元素顯示的特定地點內容,如以下範例所示:
<gmp-map center="47.759737, -122.250632" zoom="16" map-id="DEMO_MAP_ID"> <div class="widget-container" slot="control-inline-start-block-start"> <gmp-place-details> <gmp-place-details-place-request place="ChIJC8HakaIRkFQRiOgkgdHmqkk"></gmp-place-details-place-request> <gmp-place-content-config> <gmp-place-address></gmp-place-address> <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-opening-hours></gmp-place-opening-hours> <gmp-place-website></gmp-place-website> <gmp-place-phone-number></gmp-place-phone-number> <gmp-place-summary></gmp-place-summary> <gmp-place-type-specific-highlights></gmp-place-type-specific-highlights> <gmp-place-reviews></gmp-place-reviews> <gmp-place-feature-list></gmp-place-feature-list> <gmp-place-media lightbox-preferred ></gmp-place-media> <gmp-place-attribution light-scheme-color="gray" dark-scheme-color="white"></gmp-place-attribution> </gmp-place-content-config> </gmp-place-details> </div> <gmp-advanced-marker></gmp-advanced-marker> </gmp-map>
gmp-place-content-config
元素本身包含多個子項內容元素,每個元素都會選取要顯示的相應地點詳細資料:PlaceAddressElement
會選取地點的地址,PlacePriceElement
會選取地點的價格等級等等。子項元素的順序無關緊要,因為系統一律會以固定的預先定義順序顯示所選詳細資料。
您可以使用內容專屬屬性進一步設定部分元素:
-
gmp-place-media
元素用於顯示單一相片,並包含lightbox-preferred
屬性,可在點選時在燈箱中開啟相片。彈出式視窗預設為停用。 -
gmp-place-attribution
元素用於顯示相片來源。light-scheme-color
和dark-scheme-color
屬性可用於設定淺色和深色模式中歸屬文字的顏色。
PlaceContentConfigElement
參考說明文件。
為簡化操作,您可以將
gmp-place-content-config
元素替換為
gmp-place-all-content
,以便顯示 Place Details 元素中的所有詳細資料,或是替換為
gmp-place-standard-content
,以便顯示標準設定。
設定外觀
gmp-place-details
元素的建議寬度範圍為 250 像素至 400 像素。寬度小於 250 像素的圖片可能無法正確顯示。設定適合應用程式的高度。
Place Details 元素可視需要在分配的空間內捲動。
gmp-place-details
元素也支援多種自訂 CSS 屬性,可用來設定元素的顏色和字型。詳情請參閱「Places UI Kit 自訂樣式」。
查看完整程式碼範例
JavaScript
// Use querySelector to select elements for interaction. const map = document.querySelector('gmp-map'); const placeDetails = document.querySelector('gmp-place-details'); const placeDetailsRequest = document.querySelector('gmp-place-details-place-request'); const marker = document.querySelector('gmp-advanced-marker'); let center = { lat: 47.759737, lng: -122.250632 }; async function initMap() { // Request needed libraries. await google.maps.importLibrary("maps"); await google.maps.importLibrary("marker"); await google.maps.importLibrary("places"); // Hide the map type control. map.innerMap.setOptions({ mapTypeControl: false }); // Function to update map and marker based on place details const updateMapAndMarker = () => { if (placeDetails.place && placeDetails.place.location) { let adjustedCenter = offsetLatLngRight(placeDetails.place.location, -0.005); map.innerMap.panTo(adjustedCenter); map.innerMap.setZoom(16); // Set zoom after panning if needed marker.position = placeDetails.place.location; marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL; marker.style.display = 'block'; } }; // Set up map once widget is loaded. placeDetails.addEventListener('gmp-load', (event) => { updateMapAndMarker(); }); // Add an event listener to handle clicks. map.innerMap.addListener('click', async (event) => { marker.position = null; event.stop(); if (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'; } }); } // Helper function to offset marker placement for better visual appearance. function offsetLatLngRight(latLng, longitudeOffset) { const newLng = latLng.lng() + longitudeOffset; return new google.maps.LatLng(latLng.lat(), newLng); } initMap();
CSS
/* * Optional: Makes the sample page fill the window. */ html, body { display: flex; /* Use flexbox for layout */ justify-content: center; /* Center the content horizontally */ align-items: flex-start; /* Align items to the top */ width: 100% } h1 { font-size: 16px; text-align: center; } gmp-map { height: 400px; } gmp-place-details { border-radius: 0px; margin: 20px; width: 400px; height: 400px; margin-top: 0px; } gmp-advanced-marker { display: none; } .widget-container { min-width: 400px; overflow-y: none; overflow-x: none; }
HTML
<!DOCTYPE html> <html> <head> <title>Click on the map to view place details</title> <meta charset="utf-8"> <link rel="stylesheet" href="style.css"> <script type="module" src="./index.js"></script> </head> <body> <gmp-map center="47.759737, -122.250632" zoom="16" map-id="DEMO_MAP_ID"> <gmp-advanced-marker></gmp-advanced-marker> </gmp-map> <div class="widget-container" slot="control-inline-start-block-start"> <gmp-place-details> <gmp-place-details-place-request place="ChIJC8HakaIRkFQRiOgkgdHmqkk"></gmp-place-details-place-request> <gmp-place-all-content></gmp-place-all-content> </gmp-place-details> </div> <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>
Place Details 密集元素
PlaceDetailsCompactElement
會以最少的空間,顯示所選地點的詳細資料。這項功能可用於資訊視窗,在 Google 地圖上醒目顯示地點;在社群媒體體驗中,例如在聊天中分享地點;建議使用者選取目前位置;或在媒體文章中參照 Google 地圖上的地點。PlaceDetailsCompactElement
可顯示名稱、地址、評分、類型、價格、無障礙圖示、營業狀態和單張相片。
如要將元素新增至地圖,請在 HTML 網頁的 gmp-map
元素中新增 gmp-place-details-compact
元素,然後使用 orientation
屬性選取要以水平或垂直方式顯示。
在以下範例中,gmp-place-details-compact
會嵌套在 gmp-map
元素中,並將 orientation
設為 horizontal
。truncation-preferred
是額外屬性,可截斷特定內容,讓內容適合在單行顯示,而非換行顯示。gmp-place-details-compact
元素包含子元素 gmp-place-details-place-request
,用於選取地點。這可以是 Place 物件、地點 ID,或以「places/{place_id}」格式表示的地點資源名稱:
<gmp-map center="47.75972, -122.25094" zoom="19" map-id="DEMO_MAP_ID"> <gmp-place-details-compact orientation = "horizontal" truncation-preferred slot="control-block-start-inline-center" > <gmp-place-details-place-request place = "ChIJC8HakaIRkFQRiOgkgdHmqkk"></gmp-place-details-place-request> <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> </gmp-place-details-compact> <gmp-advanced-marker></gmp-advanced-marker> </gmp-map>
設定內容
您可以使用巢狀 gmp-place-content-config
元素,控管 gmp-place-details-compact
元素顯示的特定地點內容,藉此選取及設定地點詳細資料。gmp-place-content-config
元素本身包含多個子項內容元素,每個元素都會選取要顯示的相應地點詳細資料。子項元素的順序無關緊要,因為系統一律會以固定的預先定義順序轉譯所選詳細資料。您可以使用內容專屬屬性進一步設定部分元素。
PlaceContentConfigElement
參考文件。
為簡化操作,您可以將
gmp-place-content-config
元素替換為
gmp-place-all-content
,以便顯示 Place Details Compact 元素中的所有詳細資料,或是替換為
gmp-place-standard-content
,以便顯示標準設定。
設定外觀
在垂直方向中,gmp-place-details-compact
元素的建議寬度範圍為 180 px 至 300 px。寬度小於 160 像素的圖片可能無法正確顯示。請勿設定固定高度。
水平方向的 gmp-place-details-compact
元素建議寬度範圍為 180 px 至 500 px。寬度小於 160 像素的圖片可能無法正確顯示。如果寬度小於 350 像素,系統就不會顯示縮圖圖片。請勿設定固定高度。
gmp-place-details-compact
元素也支援多種自訂 CSS 屬性,可用來設定元素的顏色和字型。詳情請參閱「Places UI Kit 自訂樣式」。
查看完整程式碼範例
JavaScript
// Use querySelector to select elements for interaction. const mapContainer = document.getElementById("map-container"); const detailsContainer = document.getElementById("details-container"); const placeDetails = document.querySelector("gmp-place-details-compact"); const placeDetailsRequest = document.querySelector("gmp-place-details-place-request"); let gMap; let infowindow; let marker; async function initMap() { const { PlaceDetailsCompactElement, PlaceDetailsPlaceRequestElement } = await google.maps.importLibrary("places"); const { Map, InfoWindow } = await google.maps.importLibrary("maps"); const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); gMap = new Map(mapContainer, { mapId: 'DEMO_MAP_ID' }); marker = new AdvancedMarkerElement({ map: gMap }); infowindow = new InfoWindow({}); // Hide the map type control. gMap.setOptions({ mapTypeControl: false }); // Set up map, marker, and infowindow once widget is loaded. placeDetails.style.visibility = 'visible'; placeDetails.addEventListener('gmp-load', (event) => { console.log("placeDetails initialized!"); updateMapAndMarker(); }); // Add an event listener to handle clicks. gMap.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); placeDetailsRequest.place = event.placeId; updateMapAndMarker(); } else { // Fire when the user clicks the map (not on a POI). 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) { gMap.panTo(placeDetails.place.location); gMap.setZoom(16); // Set zoom after panning if needed marker.position = placeDetails.place.location; marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL; marker.style.display = 'block'; //@ts-ignore infowindow.setOptions({ content: placeDetails }); //@ts-ignore infowindow.open({ anchor: marker, map: gMap, }); } }; } initMap();
CSS
html, body { display: flex; width: 100%; height: 100%; margin: 0; } h1 { font-size: 16px; text-align: center; } #map-container { box-sizing: border-box; width: 100%; } 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; overflow-y: hidden; } .gm-style-iw button { display: none !important; } .gm-style-iw-c { padding: 0 !important; margin: 0 !important; } .gm-style-iw-d { padding: 0 !important; margin: 0 !important; /* Sometimes margin can also contribute to unwanted space */ overflow: visible !important; /* If you don't want scrollbars, but be careful with content overflow */ } .gm-style-iw-c * { /* Target all elements inside the infowindow content container */ padding: 0 !important; margin: 0 !important; } @media (prefers-color-scheme: dark) { .gm-style-iw.gm-style-iw-c, .gm-style .gm-style-iw-d, .gm-style .gm-style-iw-d::-webkit-scrollbar-track, .gm-style .gm-style-iw-d::-webkit-scrollbar-track-piece, .gm-style .gm-style-iw-c, .gm-style .gm-style-iw-t::after, .gm-style .gm-style-iw-tc::after { background-color: #000 !important; } .gm-ui-hover-effect>span { background-color: #fff !important; } }
HTML
<!DOCTYPE html> <html> <head> <title>Click on the map to view place details</title> <meta charset="utf-8"> <link rel="stylesheet" href="style.css"> <script type="module" src="./index.js"></script> </head> <body> <div id = "map-container"> <gmp-place-details-compact orientation="horizontal"> <gmp-place-details-place-request place = ChIJn97JQNpC1moRIcJsVMEQLI8></gmp-place-details-place-request> <gmp-place-all-content></gmp-place-all-content> </gmp-place-details-compact> </div> <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>