Simple Map

Sample code for Google Maps Platform JavaScript API author(s): Justin Poehnelt

by Wolf

HTML

<!DOCTYPE html>
<!--
 @license
 Copyright 2019 Google LLC. All Rights Reserved.
 SPDX-License-Identifier: Apache-2.0
-->
<html>
  <head>
    <title>Simple Map</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
    <!-- jsFiddle will insert css and js -->
  </head>
  <body>
    <div id="map"></div>

    <!-- 
      The `defer` attribute causes the callback 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
      with https://www.npmjs.com/package/@googlemaps/js-api-loader.
      -->
    <script
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly"
      defer
    ></script>
  </body>
</html>

CSS

/**
 * @license
 * Copyright 2019 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

function initialize() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: new google.maps.LatLng(37.4419, -122.389),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  var markers = [];
  var infoWindows = [];

  var addMarker = function(latLng, title, infoWindowContent) {
    var marker = new google.maps.Marker({
      position: latLng,
      title: title
    });
    markers.push(marker);
    infoWindow = new google.maps.InfoWindow({
      content: infoWindowContent
    });
    infoWindows.push(infoWindow);
  };

  var addListener = function(marker, event, listener) {
    marker.addListener(event, listener);
  };

  var openInfoWindow = function(marker) {
    infoWindow.open(map, marker);
  };

  var closeInfoWindow = function(marker) {
    infoWindow.close();
  };

  var markerClusterer = new google.maps.MarkerClusterer(
    map,
    {
      markers: markers,
      infoWindows: infoWindows,
      clusterRadius: 100,
      animation: google.maps.MarkerClusterer.ANIMATION_DROP
    }
  );

  var infoWindowTemplate = {
    content: 'This is the content of the info window.'
  };

  addMarker(new google.maps.LatLng(37.4419, -122.389), 'Store 1', infoWindowTemplate);
  addMarker(new google.maps.LatLng(37.4429, -122.389), 'Store 2', infoWindowTemplate);
  addMarker(new google.maps.LatLng(37.4439, -122.389), 'Store 3', infoWindowTemplate);

  addListener(markers[0], 'click', openInfoWindow);
  addListener(markers[1], 'click', openInfoWindow);
  addListener(markers[2], 'click', openInfoWindow);

  closeInfoWindow(markers[0]);
  closeInfoWindow(markers[1]);
  closeInfoWindow(markers[2]);
}

function init() {
  google.maps.event.addListener(map, 'click', initialize);
  initialize();
}