Showing/Hiding Overlays
Sample code for Google Maps Platform JavaScript API author(s): Justin Poehnelt
by Evan Chavez
HTML
<!doctype html>
<!--
@license
Copyright 2019 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<title>Showing/Hiding Overlays</title>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map"></div>
<!--
The `defer` attribute causes the script 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. See
https://developers.google.com/maps/documentation/javascript/load-maps-js-api
for more information.
-->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=yearly"
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;
}
.custom-map-control-button {
background-color: #fff;
border: 0;
border-radius: 2px;
box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3);
margin: 10px;
padding: 0 0.5em;
font: 400 18px Roboto, Arial, sans-serif;
overflow: hidden;
height: 40px;
cursor: pointer;
}
.custom-map-control-button:hover {
background: rgb(235, 235, 235);
}
JavaScript
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// This example adds hide() and show() methods to a custom overlay's prototype.
// These methods toggle the visibility of the container <div>.
// overlay to or from the map.
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 11,
center: { lat: 62.323907, lng: -150.109291 },
mapTypeId: "satellite",
});
const bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(62.281819, -150.287132),
new google.maps.LatLng(62.400471, -150.005608),
);
// The photograph is courtesy of the U.S. Geological Survey.
let image = "https://evanchavez-iot.com/citybridge/CityBridgeParking.png";
//image += "https://evanchavez-iot.com/CityBridge/CityBridgeParking.png";
/**
* The custom USGSOverlay object contains the USGS image,
* the bounds of the image, and a reference to the map.
*/
class USGSOverlay extends google.maps.OverlayView {
bounds;
image;
div;
constructor(bounds, image) {
super();
this.bounds = bounds;
this.image = image;
}
/**
* onAdd is called when the map's panes are ready and the overlay has been
* added to the map.
*/
onAdd() {
this.div = document.createElement("div");
this.div.style.borderStyle = "none";
this.div.style.borderWidth = "0px";
this.div.style.position = "absolute";
// Create the img element and attach it to the div.
const img = document.createElement("img");
img.src = this.image;
img.style.width = "100%";
img.style.height = "100%";
img.style.position = "absolute";
this.div.appendChild(img);
// Add the element to the "overlayLayer" pane.
const panes = this.getPanes();
panes.overlayLayer.appendChild(this.div);
}
draw() {
// We use the south-west and north-east
// coordinates of the overlay to...