라이브러리

Maps JavaScript API용 자바스크립트 코드를 로드하려면 페이지에 다음 형태의 부트스트랩 로더 스크립트를 포함합니다.

<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_HERE",
    // Add other bootstrap parameters as needed, using camel case.
    // Use the 'v' parameter to indicate the version to load (alpha, beta, weekly, etc.)
  });
</script>

Maps JavaScript API는 특별히 요청할 때까지 로드되지 않는 라이브러리로 구성됩니다. 구성요소를 라이브러리로 분할하면 API가 빠르게 로드 및 파싱될 수 있습니다. 필요한 경우에만 라이브러리 로드 및 파싱 오버헤드가 추가로 발생합니다.

await 연산자를 사용하여 async 함수 내에서 importLibrary()를 호출하여 런타임에 추가 라이브러리를 로드합니다. 예:

const { Map } = await google.maps.importLibrary("maps");

다음 코드 예에서는 MapAdvancedMarkerView 라이브러리를 모두 로드하는 방법을 보여줍니다.

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 { AdvancedMarkerView } = 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 AdvancedMarkerView({
    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 { AdvancedMarkerView } = 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 AdvancedMarkerView({
    map: map,
    position: position,
    title: "Uluru",
  });
}

initMap();

동적 라이브러리 가져오기용 라이브러리

다음 라이브러리를 동적 라이브러리 가져오기와 함께 사용할 수 있습니다.

부트스트랩 URL용 라이브러리(기존)

기존 부트스트랩 스크립트 태그와 함께 사용할 수 있는 라이브러리는 다음과 같습니다.
  • drawing은 사용자가 지도에 다각형, 직사각형, 다중선, 원 및 마커를 그릴 수 있는 그래픽 인터페이스를 제공합니다. 자세한 내용은 그림 라이브러리 문서를 참고하세요.
  • geometry에는 지구 표면의 스칼라 도형 값(예: 거리, 면적)을 계산하기 위한 유틸리티 함수가 포함되어 있습니다. 자세한 내용은 도형 라이브러리 문서를 참고하세요.
  • journeySharing은 Google Maps Platform 운송 및 물류 솔루션을 지원합니다.
  • localContext는 지정된 위치 주변의 주요 관심 장소를 사용자에게 표시합니다. 자세한 내용은 로컬 컨텍스트 라이브러리 문서를 참고하세요.
  • marker를 사용하면 세부적인 맞춤설정이 가능하고 성능이 뛰어난 고급 마커를 지도에 추가할 수 있습니다. 자세한 내용은 고급 마커 문서를 참고하세요.
  • places를 사용하면 애플리케이션이 정의된 지역 내에서 시설, 지리적 위치, 주요 관심 장소 등의 장소를 검색할 수 있습니다. 자세한 내용은 장소 라이브러리 문서를 참고하세요.
  • visualization은 데이터의 시각적 표현을 위한 히트맵을 제공합니다. 자세한 내용은 시각화 라이브러리 문서를 참고하세요.

다음 부트스트랩 요청은 Maps JavaScript API의 google.maps.geometry 라이브러리 요청을 기존 부트스트랩 로더 스크립트에 추가하는 방법을 보여줍니다.

<script async
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry&callback=initMap">
</script>

여러 라이브러리를 요청하려면 다음과 같이 쉼표로 구분하세요.

<script async
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry,places&callback=initMap">
</script>