Add a Map with Markers using HTML

  • This example demonstrates adding markers to a map using the <gmp-advanced-marker> web component.

  • The sample utilizes Google Maps JavaScript API with web components for a streamlined approach.

  • Both TypeScript and JavaScript code snippets are provided for implementation flexibility.

  • The HTML structure showcases the integration of the map and markers within a web page.

  • Users can explore the sample interactively through JSFiddle or Google Cloud Shell, or clone the repository for local execution.

This example adds markers to a map, using <gmp-advanced-marker> .

Read the documentation .

TypeScript

 // This example adds a map with markers, using web components. 
 async 
  
 function 
  
 initMap 
 () 
  
 { 
  
 console 
 . 
 log 
 ( 
 'Maps JavaScript API loaded.' 
 ); 
 } 
 initMap 
 (); 
  

JavaScript

 // This example adds a map with markers, using web components. 
 async 
  
 function 
  
 initMap 
 () 
  
 { 
  
 console 
 . 
 log 
 ( 
 'Maps JavaScript API loaded.' 
 ); 
 } 
 initMap 
 (); 
  

CSS

 /* Note: This CSS file is intentionally blank. */ 
  

HTML

<html>
    <head>
        <title>Add a Map with Markers using HTML</title>
        <style>
            gmp-map {
                height: 100%;
            }
            html,
            body {
                height: 100%;
                margin: 0;
                padding: 0;
            }
        </style>
        <script type="module" src="./index.js"></script>
        <script
            src="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="41.027748173921374, -92.41852445367961"
            zoom="13"
            map-id="DEMO_MAP_ID">
            <gmp-advanced-marker
                position="41.027748173921374, -92.41852445367961"
                title="Ottumwa, IA"></gmp-advanced-marker>
        </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.git 
 
  
  cd 
  
 samples 
 / 
 web 
 - 
 components 
 - 
 markers 
 
  
  npm 
  
 i 
 
  
  npm 
  
 start 
 
Create a Mobile Website
View Site in Mobile | Classic
Share by: