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@^1.2.0/dist/index.min.js"></script>
The full sample code is below.
Create a starter application from sample
A skeleton starter application using TypeScript, Webpack, and Babel can be generated from this sample using one of the methods below.
Run Locally
Node.js is required to run this sample locally. Follow these instructions to install Node.js and NPM.
npx @googlemaps/js-samples init programmatic-load-button DESTINATION_FOLDER
Run in Google Cloud Shell
Google Cloud Shell is an interactive shell environment for Google Cloud Platform that makes it easy for you to learn and experiment with GCP and manage your projects and resources from your web browser.
Run in Cloud Shell