如要載入 Maps JavaScript API 的 JavaScript 程式碼,您需要在網頁中加入「bootstrap」載入器指令碼,格式如下:
<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: "weekly", // Use the 'v' parameter to indicate the version to use (weekly, beta, alpha, etc.). // Add other bootstrap parameters as needed, using camel case. }); </script>
Maps JavaScript API 是由「程式庫」組成,這類程式庫只有在您明確要求時才會載入。將元件分散到各個程式庫中,有助於快速載入 (及剖析) API。這樣就只有在需要載入及剖析程式庫時,才會使系統承受額外負荷。
使用 await
運算子在 async
函式中呼叫 importLibrary()
,即可在執行階段載入其他程式庫。例如:
const { Map } = await google.maps.importLibrary("maps");
以下程式碼範例說明如何一併載入 Map
和 AdvancedMarkerElement
程式庫:
TypeScript
// Initialize and add the map let map; async function initMap(): Promise<void> { // The location of Uluru const position = { lat: -25.344, lng: 131.031 }; // Request needed libraries. //@ts-ignore const { Map } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary; const { AdvancedMarkerElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary; // The map, centered at Uluru map = new Map( document.getElementById('map') as HTMLElement, { zoom: 4, center: position, mapId: 'DEMO_MAP_ID', } ); // The marker, positioned at Uluru const marker = new AdvancedMarkerElement({ map: map, position: position, title: 'Uluru' }); } initMap();
JavaScript
// Initialize and add the map let map; async function initMap() { // The location of Uluru const position = { lat: -25.344, lng: 131.031 }; // Request needed libraries. //@ts-ignore const { Map } = await google.maps.importLibrary("maps"); const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); // The map, centered at Uluru map = new Map(document.getElementById("map"), { zoom: 4, center: position, mapId: "DEMO_MAP_ID", }); // The marker, positioned at Uluru const marker = new AdvancedMarkerElement({ map: map, position: position, title: "Uluru", }); } initMap();
Dynamic Library Import 適用的程式庫
下列程式庫可與 Dynamic Library Import 搭配使用:
core
(google.maps.CoreLibrary
)maps
(google.maps.MapsLibrary
)places
(google.maps.PlacesLibrary
)geocoding
(google.maps.GeocodingLibrary
)routes
(google.maps.RoutesLibrary
)marker
(google.maps.MarkerLibrary
)geometry
(google.maps.GeometryLibrary
)elevation
(google.maps.ElevationLibrary
)streetView
(google.maps.StreetViewLibrary
)journeySharing
(google.maps.JourneySharingLibrary
)drawing
(google.maps.DrawingLibrary
)visualization
(google.maps.VisualizationLibrary
)
Bootstrap 網址適用的程式庫 (舊版)
下列程式庫支援搭配舊版 Bootstrap 指令碼標記使用:drawing
提供圖形介面,可讓使用者在地圖上繪製多邊形、矩形、折線、圓形和標記。詳情請參閱繪圖程式庫說明文件。geometry
內含公用函式,可用於計算地表上的純量幾何圖形值 (例如距離和區域)。詳情請參閱幾何圖形程式庫說明文件。journeySharing
支援 Google 地圖平台上的運輸與物流解決方案。localContext
會向使用者顯示指定地點附近的重要觀光景點。詳情請參閱當地特色資料庫說明文件。marker
可讓您在地圖中加入能靈活自訂且效能卓越的進階標記。詳情請參閱進階標記說明文件。places
可讓應用程式在定義的區域內,搜尋建築物、地理位置或重要搜尋點等地點。詳情請參閱 Places Library 說明文件。visualization
提供熱視圖,以視覺化方式呈現資料。詳情請參閱視覺化程式庫說明文件。
下列 Bootstrap 要求示範如何在舊版 Bootstrap 載入器指令碼中,新增 Maps JavaScript API google.maps.geometry
程式庫的要求:
<script async
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&loading=async&libraries=geometry&callback=initMap">
</script>
如想要求多個程式庫,請使用半形逗號隔開:
<script async
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&loading=async&libraries=geometry,places&callback=initMap">
</script>