PlaceDetailsElement
是用來轉譯地點詳細資料的 HTML 元素。您可以透過兩種方法設定 gmp-place-details
元素:
-
configureFromPlace
方法會根據地點 ID 或Place
物件算繪 PlaceDetails 元素。 -
configureFromLocation
方法會根據經緯度座標算繪 Place Details 元素。
本頁範例會在使用者按下地圖時,算繪「PlaceDetails」元素。如要在地圖中加入地點詳細資料,請在 HTML 網頁中加入 gmp-place-details
元素。分別使用 size
和 slot
屬性設定大小和位置。在以下範例中,它會嵌套在 gmp-map
元素中,其中 size
設為 medium
,slot
設為 control-inline-start-block-start
。
<gmp-map center="-37.813,144.963" zoom="2" map-id="DEMO_MAP_ID"> <gmp-place-details size="medium" slot="control-inline-start-block-start"></gmp-place-details> <gmp-advanced-marker></gmp-advanced-marker> </gmp-map>
querySelector
可用於選取要互動的每個網頁元素:
const map = document.querySelector('gmp-map'); const placeDetails = document.querySelector('gmp-place-details'); const marker = document.querySelector('gmp-advanced-marker');
以下程式碼片段中的事件處理常式會根據使用者在地圖上點選的位置,算繪 Place Details 元素:
// Add an event listener to handle map clicks. map.innerMap.addListener('click', async (event) => { marker.position = null; event.stop(); if (event.placeId) { // The user clicked a point on the map that has a Place ID (for example a // POI). await placeDetails.configureFromPlace({id: event.placeId}); } else { // The user clicked an area of the map with no Place ID. await placeDetails.configureFromLocation(event.latLng); } // Show the marker at the selected place. marker.position = placeDetails.place.location; marker.style.display = 'block'; });
查看完整程式碼範例
JavaScript
/** * @license * Copyright 2019 Google LLC. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ // Use querySelector to select elements for interaction. const map = document.querySelector('gmp-map'); const placeDetails = document.querySelector('gmp-place-details'); const marker = document.querySelector('gmp-advanced-marker'); async function init() { // Import the needed libraries. await google.maps.importLibrary('places'); await google.maps.importLibrary('marker'); // Calls the geolocation helper function to center the map on the current // device location. findCurrentLocation(); // Hide the map type control. map.innerMap.setOptions({mapTypeControl: false}); // Add an event listener to handle map clicks. map.innerMap.addListener('click', async (event) => { marker.position = null; event.stop(); if (event.placeId) { // Fire when the user clicks a POI. await placeDetails.configureFromPlace({id: event.placeId}); } else { // Fire when the user clicks the map (not on a POI). await placeDetails.configureFromLocation(event.latLng); } // Show the marker at the selected place. marker.position = placeDetails.place.location; marker.style.display = 'block'; }); } // Helper function for geolocation. async function findCurrentLocation() { const {LatLng} = await google.maps.importLibrary('core'); if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( (position) => { const pos = new LatLng(position.coords.latitude, position.coords.longitude); map.innerMap.panTo(pos); map.innerMap.setZoom(16); }, () => { console.log('The Geolocation service failed.'); map.innerMap.setZoom(16); }, ); } else { console.log('Your browser doesn\'t support geolocation'); map.innerMap.setZoom(16); } } init();
CSS
html, body { height: 100%; margin: 0; } body { display: flex; flex-direction: column; font-family: Arial, Helvetica, sans-serif; } h1 { font-size: 16px; text-align: center; } gmp-map { box-sizing: border-box; padding: 0 20px 20px; width: 100%; } gmp-place-details { background-color: #fff; border-radius: 8px; margin: 20px; width: 400px; } gmp-advanced-marker { display: 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"> </head> <body> <h1>Click on the map to view place details</h1> <gmp-map center="-37.813,144.963" zoom="2" map-id="DEMO_MAP_ID"> <gmp-place-details size="large" slot="control-inline-start-block-start"></gmp-place-details> <gmp-advanced-marker></gmp-advanced-marker> </gmp-map> <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: "YOUR_API_KEY", v: "alpha" }); </script> </body> </html>