// This example adds a map using web components.asyncfunctioninitMap():Promise<void>{console.log('Maps JavaScript API loaded.');}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
<html>
<head>
<title>Add a Map using HTML</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<gmp-map
center="37.4220656,-122.0840897"
zoom="10"
map-id="DEMO_MAP_ID"
style="height: 400px"
></gmp-map>
<!--
The `defer` attribute causes the script to execute after the full HTML
document has been parsed. For non-blocking uses, avoiding race conditions,
and consistent behavior across browsers, consider loading using Promises. See
https://developers.google.com/maps/documentation/javascript/load-maps-js-api
for more information.
-->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&libraries=maps&v=beta"
defer
></script>
</body>
</html>
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-15 UTC."],[[["\u003cp\u003eThis example showcases creating a basic Google Map using HTML and the \u003ccode\u003egmp-map\u003c/code\u003e web component.\u003c/p\u003e\n"],["\u003cp\u003eThe map is initialized using JavaScript (or TypeScript) with an \u003ccode\u003einitMap\u003c/code\u003e function.\u003c/p\u003e\n"],["\u003cp\u003eSample code provides implementation in both JavaScript and TypeScript, along with the corresponding HTML and CSS.\u003c/p\u003e\n"],["\u003cp\u003eUsers can experiment with the example via provided links to JSFiddle and Google Cloud Shell or clone the repository for local execution.\u003c/p\u003e\n"]]],["This example demonstrates how to create a map using HTML and the `gmp-map` element. The HTML uses a `\u003cgmp-map\u003e` tag with attributes like `center`, `zoom`, and `map-id`. It loads the Maps JavaScript API via a `\u003cscript\u003e` tag. TypeScript and JavaScript code initialize the map, logging a confirmation. CSS styles the map. The sample can be tried online via JSFiddle or Google Cloud Shell, and can be cloned locally by cloning a git repository, installing dependencies, and starting the app.\n"],null,[]]