如需加载 Maps JavaScript API 的 JavaScript 代码,您需要在网页上添加引导程序加载器脚本,格式如下:
<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
)
可与引导程序网址(旧版)搭配使用的库
以下库可与旧版引导程序脚本标记搭配使用:drawing
,提供一个图形界面,以供用户在地图上绘制多边形、矩形、多段线、圆形和标记。如需了解详情,请参阅绘图库文档。geometry
,包含实用函数,用于计算地球表面的标量几何值(例如距离和面积)。如需了解详情,请参阅几何图形库文档。journeySharing
,为 Google Maps Platform 交通运输和物流解决方案提供支持。localContext
,向用户显示指定位置附近的主要景点。如需了解详情,请参阅 Local Context Library 文档。marker
,可让您向地图添加可灵活定制且性能出色的高级标记。如需了解详情,请参阅高级标记文档。places
,让您的应用能够在指定的区域内搜索场所、地理位置或受关注的地图注点等地点。如需了解详情,请参阅地点库文档。visualization
,提供热图,用于直观呈现数据。如需了解详情,请参阅可视化库文档。
以下引导程序请求展示了如何将对 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>