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.JavaScript
// This example adds a map using web components.CSS
HTML
<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>
<script
async
src="https://maps.googleapis.com/maps/api/js?loading=async&key=GOOGLE_MAPS_API_KEY&libraries=maps,marker"></script>
</head>
<body>
<gmp-map
center="37.4220656,-122.0840897"
zoom="10"
style="height: 500px"></gmp-map>
</body>
</html>Try Sample
Clone Sample
Before running this sample locally, make sure you have:
- Git and Node.js (install Node.js and npm).
- A Google Maps API key. If this sample uses supported features, you can get a no-cost Maps Demo Key to prototype without billing.
The following commands clone the repository, install dependencies, and copy the environment file.
git clone https://github.com/googlemaps-samples/js-api-samples.gitcd js-api-samplesnpm icp .env.template .env
Once you copy the environment file, make sure to open the .env file and replace
<Insert your own development API key here> with your actual API key before
starting the sample.
In the sample code, GOOGLE_MAPS_API_KEY is a placeholder. When you run the sample, the
API key from your .env file replaces it. In your own app, read the key only from an
environment variable, with no literal key as a fallback value in source code.
cd samples/web-components-mapnpm start