Announcement: New basemap styling is coming soon to Google Maps Platform. This update to map styling includes a new default color palette, modernized pins, and improvements to map experiences and usability. All map styles will be automatically updated in March 2025. For more information on availability and how to opt in earlier, see New map style for Google Maps Platform.
// 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.
[null,null,["Last updated 2025-02-24 UTC."],[[["This example showcases creating a basic Google Map using HTML and the `gmp-map` web component."],["The map is initialized using JavaScript (or TypeScript) with an `initMap` function."],["Sample code provides implementation in both JavaScript and TypeScript, along with the corresponding HTML and CSS."],["Users can experiment with the example via provided links to JSFiddle and Google Cloud Shell or clone the repository for local execution."]]],["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"]]