JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<title>Showing/Hiding Overlays</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>
<!-- Async script executes immediately and must be after any DOM elements used in callback. -->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly&v=beta"
async
></script>
</body>
</html>
CSS
/* 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: #ebebeb;
}
JavaScript
// 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",
mapId: "15431d2b469f209e"
});
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://developers.google.com/maps/documentation/javascript/";
image += "examples/full/images/talkeetna.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 peg it to the correct position and size.
// To do this, we need to retrieve the projection from the...