Delay Loading Dynamic Map

Stay organized with collections Save and categorize content based on your preferences.

This example uses a static map as a placeholder for a dynamic map. A click event handler on the placeholder and button triggers the loading of the Google Maps JavaScript API and replaces the static map with a dynamic map.

The example depends on @googlemaps/js-api-loader to dynamically append the Google Maps JavaScript script tag to the html. This can be installed with the following NPM install.

npm install @googlemaps/js-api-loader

Alternatively, the following script can be added to the HTML document.

<script src="https://unpkg.com/@googlemaps/js-api-loader/dist/index.min.js"></script>

The full sample code is below.

Try Sample

Clone Sample

Git and Node.js are required to run this sample locally. Follow these instructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.

  git clone -b sample-programmatic-load-button https://github.com/googlemaps/js-samples.git
  cd js-samples
  npm i
  npm start

Other samples can be tried by switching to any branch beginning with sample-SAMPLE_NAME.

  git checkout sample-SAMPLE_NAME
  npm i
  npm start