如要載入 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,因此只有在需要載入及剖析程式庫時,才會使系統承受額外負荷。使用動態程式庫匯入時,內嵌 Bootstrap 載入器預設不會載入任何程式庫 (甚至不會載入 core 或 maps);在呼叫 google.maps.importLibrary() 之前,系統不會載入 Maps JavaScript API 的任何部分。
如要在執行階段載入程式庫,請使用 await 運算子從 async 函式中呼叫 google.maps.importLibrary()。假設只載入要求的程式庫,並為程式碼使用的每個程式庫呼叫 google.maps.importLibrary()。例如:
const { Map } = await google.maps.importLibrary("maps");
以下程式碼範例說明如何一併載入 maps 和 marker 程式庫,以存取 Map 和 AdvancedMarkerElement 類別:
TypeScript
async function init(): Promise<void> { // Request the needed libraries. const [{ AdvancedMarkerElement }] = await Promise.all([ google.maps.importLibrary('marker'), google.maps.importLibrary('maps'), ]); // Get the gmp-map element. const mapElement = document.querySelector('gmp-map')!; // Get the inner map. const innerMap = mapElement.innerMap; // Set map options. innerMap.setOptions({ mapTypeControl: false, }); // Add a marker positioned at the map center (Uluru). new AdvancedMarkerElement({ map: innerMap, position: mapElement.center, title: 'Uluru/Ayers Rock', }); } void init();
JavaScript
async function init() { // Request the needed libraries. const [{ AdvancedMarkerElement }] = await Promise.all([ google.maps.importLibrary('marker'), google.maps.importLibrary('maps'), ]); // Get the gmp-map element. const mapElement = document.querySelector('gmp-map'); // Get the inner map. const innerMap = mapElement.innerMap; // Set map options. innerMap.setOptions({ mapTypeControl: false, }); // Add a marker positioned at the map center (Uluru). new AdvancedMarkerElement({ map: innerMap, position: mapElement.center, title: 'Uluru/Ayers Rock', }); } void init();
可用的程式庫
core(google.maps.CoreLibrary)maps(google.maps.MapsLibrary)maps3d(google.maps.Maps3DLibrary)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)visualization(google.maps.VisualizationLibrary)airQuality(google.maps.AirQualityLibrary)addressValidation(google.maps.AddressValidationLibrary)drawing(google.maps.DrawingLibrary) (已淘汰)
下列 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>