Simple Map

Sample code supporting Google Maps Platform JavaScript API documentation. author(s): Geo Developer IX Documentation Team

by Wolf

HTML

<!--
 @license
 Copyright 2025 Google LLC. All Rights Reserved.
 SPDX-License-Identifier: Apache-2.0
-->

<html>
  <head>
    <title>Simple Map</title>

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <div id="map"></div>

    <!-- prettier-ignore -->
    <script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
        ({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
  </body>
</html>

CSS

/*
 * @license
 * Copyright 2025 Google LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

/* 
 * Always set the map height explicitly to define the size of the div element
 * that contains the map. 
 */
#map {
  height: 100%;
}

/* 
 * Optional: Makes the sample page fill the window. 
 */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

JavaScript

"use strict";
/*
 * @license
 * Copyright 2025 Google LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

async function initMap() {
    const { Map } = await google.maps.importLibrary("maps");
    const map = new Map(document.getElementById("map"), {
        center: { lat: 47.3769, lng: 8.5417 },
        zoom: 12,
        scaleControl: false, // Disable the default scale control
    });

    // Create and add our custom scale bar
    const scaleControl = createScaleControl(map);
    map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(scaleControl);
}
initMap();

/**
 * Creates a custom scale bar control for a Google Map.
 *
 * @param {google.maps.Map} map The Google Map object.
 * @returns {HTMLDivElement} The scale bar control element.
 */
function createScaleControl(map) {
  // Create the container for the scale bar
  const scaleControlDiv = document.createElement('div');
  scaleControlDiv.style.background = 'rgba(255, 255, 255, 0.85)';
  scaleControlDiv.style.borderRadius = '2px';
  scaleControlDiv.style.padding = '5px';
  scaleControlDiv.style.margin = '10px';
  scaleControlDiv.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
  scaleControlDiv.style.cursor = 'pointer';
  scaleControlDiv.style.textAlign = 'center';

  // Create the div for the scale bar line
  const scaleBar = document.createElement('div');
  scaleBar.style.display = 'inline-block';
  scaleBar.style.height = '8px';
  scaleBar.style.borderLeft = '1px solid black';
  scaleBar.style.borderRight = '1px solid black';
  scaleBar.style.borderBottom = '1px solid black';
  scaleBar.style.boxSizing = 'border-box'; // Ensures border is inside the width
  scaleControlDiv.appendChild(scaleBar);

  // Create the div for the scale bar text
  const scaleText = document.createElement('div');
  scaleText.style.display = 'inline-block';
  scaleText.style.fontSize = '10px';
  scaleText.style.fontFamily = 'Roboto, Arial, sans-serif';
 ...