Page Summary
-
This example showcases creating a basic Google Map using HTML and the
gmp-mapweb component. -
The map is initialized using JavaScript (or TypeScript) with an
initMapfunction. -
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 creating a map using HTML, with the gmp-map element.
Read the documentation.
TypeScript
// This example adds a map using web components. async function initMap() { console.log('Maps JavaScript API loaded.'); } initMap();
JavaScript
// This example adds a map using web components. async function initMap() { console.log('Maps JavaScript API loaded.'); } initMap();
CSS
HTML
<html>
<head>
<title>Add a Map using HTML</title>
<link rel="stylesheet" type="text/css" href="/proxy/https/developers.google.com/maps/documentation/javascript/examples/style.css" />
<script type="module" src="/proxy/https/developers.google.com/maps/documentation/javascript/examples/index.js"></script>
<script
src="/proxy/https/maps.googleapis.com/maps/api/js?key=AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8&libraries=maps,marker&v=weekly&internal_usage_attribution_ids=gmp_git_jsapisamples_v1_web-components"
defer></script>
</head>
<body>
<gmp-map
center="37.4220656,-122.0840897"
zoom="10"
map-id="DEMO_MAP_ID"
style="height: 500px"></gmp-map>
</body>
</html>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 https://github.com/googlemaps-samples/js-api-samples.gitcd samples/web-components-mapnpm inpm start