網頁元件是熱門的 W3C 標準,能將 HTML、CSS 和 JS 封裝在可重複使用的自訂 HTML 元素中。這些可重複使用的元件種類廣泛,有小型的功能組成元素 (例如顯示星級評等),也有複雜的商業邏輯。本指南將說明 Maps JavaScript API 所提供的網頁元件。
如要進一步瞭解這項標準,請參閱「網頁元件」一文。
適用對象
本說明文件旨在協助您快速起步,探索如何使用網頁元件開發應用程式。建議需先熟悉 HTML 和 CSS 程式設計概念。
顯示地圖
開始學習網頁元件最簡單的方法,就是查看範例。以下範例顯示聖荷西區域的地圖。
TypeScript
// This example adds a map using web components. function initMap(): void { console.log('Maps JavaScript API loaded.'); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example adds a map using web components. function initMap() { console.log("Maps JavaScript API loaded."); } window.initMap = initMap;
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ #map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; } gmp-map { height: 400px; }
HTML
<html> <head> <title>Add a Map Web Component</title> <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <gmp-map center="37.4220656,-122.0840897" zoom="10" map-id="DEMO_MAP_ID" ></gmp-map> <!-- The `defer` attribute causes the callback to execute after the full HTML document has been parsed. For non-blocking uses, avoiding race conditions, and consistent behavior across browsers, consider loading using Promises. See https://developers.google.com/maps/documentation/javascript/load-maps-js-api for more information. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=beta" defer ></script> </body> </html>
測試範例
在本例中,有幾個地方需要注意:
- Maps JavaScript API 是非同步呼叫的。回呼函式會用來判斷載入 API 的時間。
- 地圖的呈現方式是以
<gmp-map>
自訂元素來決定。 - 定義地圖屬性的方式,是在
<gmp-map>
自訂元素中指定屬性。 - 樣式可以透過內嵌的方式套用至自訂元素,或是在獨立 CSS 檔案中宣告。
設定基本地圖的樣式
地圖 ID 是指與特定地圖樣式或地圖項目相關聯的 ID。如要使用自選的雲端設定功能,請將雲端式地圖樣式設定 DEMO_MAP_ID
換成您自己的地圖 ID。如要瞭解如何建立地圖 ID 並設定自訂樣式,請參閱「雲端式地圖樣式設定」一文。
在地圖中加入標記
就像建立內建 HTML 代碼的巢狀結構來塑造複雜的內容階層一樣,您也可以在元素內建立 <gmp-advanced-marker>
的巢狀結構來顯示一或多個地圖標記。
TypeScript
// This example adds a map with markers, using web components. function initMap(): void { console.log('Maps JavaScript API loaded.'); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example adds a map with markers, using web components. function initMap() { console.log("Maps JavaScript API loaded."); } window.initMap = initMap;
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ #map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; } gmp-map { height: 400px; }
HTML
<html> <head> <title>Add a Map with Markers using Web Components</title> <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <gmp-map center="43.4142989,-124.2301242" zoom="4" map-id="DEMO_MAP_ID"> <gmp-advanced-marker position="37.4220656,-122.0840897" title="Mountain View, CA" ></gmp-advanced-marker> <gmp-advanced-marker position="47.648994,-122.3503845" title="Seattle, WA" ></gmp-advanced-marker> </gmp-map> <!-- The `defer` attribute causes the callback to execute after the full HTML document has been parsed. For non-blocking uses, avoiding race conditions, and consistent behavior across browsers, consider loading using Promises. See https://developers.google.com/maps/documentation/javascript/load-maps-js-api for more information. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&libraries=marker&v=beta" defer ></script> </body> </html>
測試範例
這裡我們在 <gmp-map>
自訂元素內加入了兩個 <gmp-advanced-marker>
元素。title
的文字可為指定元素在游標懸停時額外提供顯示文字,也可當做無障礙標籤。
JavaScript 事件
網頁元件的主要好處是易於使用。只要加入幾行程式碼,就算是對 JavaScript 或進階程式設計瞭解有限的人,還是能順利顯示地圖畫面。導入完成後,您會發現這段程式碼的運作模式,和其他熟悉的 HTML 元素一樣。舉例來說,您可以運用原生瀏覽器事件系統,回應地圖或進階標記動作 (例如按下標記)。
TypeScript
// This example adds a map using web components. function initMap(): void { console.log('Maps JavaScript API loaded.'); const advancedMarkers = document.querySelectorAll("#marker-click-event-example gmp-advanced-marker"); for (const advancedMarker of advancedMarkers) { customElements.whenDefined(advancedMarker.localName).then(async () => { advancedMarker.addEventListener('gmp-click', async () => { const infoWindow = new google.maps.InfoWindow({ //@ts-ignore content: advancedMarker.title, }); infoWindow.open({ //@ts-ignore anchor: advancedMarker }); }); }); } } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example adds a map using web components. function initMap() { console.log("Maps JavaScript API loaded."); const advancedMarkers = document.querySelectorAll( "#marker-click-event-example gmp-advanced-marker", ); for (const advancedMarker of advancedMarkers) { customElements.whenDefined(advancedMarker.localName).then(async () => { advancedMarker.addEventListener("gmp-click", async () => { const infoWindow = new google.maps.InfoWindow({ //@ts-ignore content: advancedMarker.title, }); infoWindow.open({ //@ts-ignore anchor: advancedMarker, }); }); }); } } window.initMap = initMap;
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ #map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; } gmp-map { height: 400px; }
HTML
<html> <head> <title>Add a Map Web Component with Events</title> <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <gmp-map id="marker-click-event-example" center="43.4142989,-124.2301242" zoom="4" map-id="DEMO_MAP_ID" > <gmp-advanced-marker position="37.4220656,-122.0840897" title="Mountain View, CA" ></gmp-advanced-marker> <gmp-advanced-marker position="47.648994,-122.3503845" title="Seattle, WA" ></gmp-advanced-marker> </gmp-map> <!-- The `defer` attribute causes the callback to execute after the full HTML document has been parsed. For non-blocking uses, avoiding race conditions, and consistent behavior across browsers, consider loading using Promises. See https://developers.google.com/maps/documentation/javascript/load-maps-js-api for more information. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&libraries=marker&v=beta" defer ></script> </body> </html>
測試範例
在本例中,initMap
代表 Maps JavaScript API 完全載入後所需使用的回呼函式。這段程式碼會建立每個標記的事件監聽器,每當有人按下這些標記時,就會顯示資訊視窗。
後續步驟
- 透過 Maps JavaScript API 的 Issue Tracker 要求功能並回報錯誤。
- 探索地點總覽等較高層級網頁元件的擴充元件程式庫。