Replacing Default Controls
Sample code for Google Maps Platform JavaScript API
author(s):
Justin Poehnelt
HTML
<!doctype html>
<!--
@license
Copyright 2019 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<title>Replacing Default Controls</title>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map"></div>
<!-- Hide controls until they are moved into the map. -->
<div style="display: none">
<div class="controls zoom-control">
<button class="zoom-control-in" title="Zoom In">+</button>
<button class="zoom-control-out" title="Zoom Out">−</button>
</div>
<div class="controls maptype-control maptype-control-is-map">
<button class="maptype-control-map" title="Show road map">Map</button>
<button
class="maptype-control-satellite"
title="Show satellite imagery"
>
Satellite
</button>
</div>
<div class="controls fullscreen-control">
<button title="Toggle Fullscreen">
<div
class="fullscreen-control-icon fullscreen-control-top-left"
></div>
<div
class="fullscreen-control-icon fullscreen-control-top-right"
></div>
<div
class="fullscreen-control-icon fullscreen-control-bottom-left"
></div>
<div
class="fullscreen-control-icon fullscreen-control-bottom-right"
></div>
</button>
</div>
</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=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;
}
.gm-style .controls {
font-size: 28px; /* this adjusts the size of all the controls */
background-color: white;
box-shadow: rgba(0, 0, 0, 0.3) 0px 1px 4px -1px;
box-sizing: border-box;
border-radius: 2px;
cursor: pointer;
font-weight: 300;
height: 1em;
margin: 6px;
text-align: center;
user-select: none;
padding: 2px;
width: 1em;
}
.gm-style .controls button {
border: 0;
background-color: white;
color: rgba(0, 0, 0, 0.6);
}
.gm-style .controls button:hover {
color: rgba(0, 0, 0, 0.9);
}
.gm-style .controls.zoom-control {
display: flex;
flex-direction: column;
height: auto;
}
.gm-style .controls.zoom-control button {
font: 0.85em Arial;
margin: 1px;
padding: 0;
}
.gm-style .controls.maptype-control {
display: flex;
flex-direction: row;
width: auto;
}
.gm-style .controls.maptype-control button {
display: inline-block;
font-size: 0.5em;
margin: 0 1px;
padding: 0 6px;
}
.gm-style .controls.maptype-control.maptype-control-is-map .maptype-control-map {
font-weight: 700;
}
.gm-style .controls.maptype-control.maptype-control-is-satellite .maptype-control-satellite {
font-weight: 700;
}
.gm-style .controls.fullscreen-control button {
display: block;
font-size: 1em;
height: 100%;
width: 100%;
}
.gm-style .controls.fullscreen-control .fullscreen-control-icon {
border-style: solid;
height: 0.25em;
position: absolute;
width: 0.25em;
}
.gm-style .controls.fullscreen-control .fullscreen-control-icon.fullscreen-control-top-left {
border-width: 2px 0 0 2px;
left: 0.1em;
top: 0.1em;
}
.gm-style...
JavaScript
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
let map;
function initMap() {
map = new google.maps.Map(document.querySelector("#map"), {
center: { lat: -34.397, lng: 150.644 },
zoom: 8,
disableDefaultUI: true,
});
initZoomControl(map);
initMapTypeControl(map);
initFullscreenControl(map);
}
function initZoomControl(map) {
document.querySelector(".zoom-control-in").onclick = function () {
map.setZoom(map.getZoom() + 1);
};
document.querySelector(".zoom-control-out").onclick = function () {
map.setZoom(map.getZoom() - 1);
};
map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(
document.querySelector(".zoom-control"),
);
}
function initMapTypeControl(map) {
const mapTypeControlDiv = document.querySelector(".maptype-control");
document.querySelector(".maptype-control-map").onclick = function () {
mapTypeControlDiv.classList.add("maptype-control-is-map");
mapTypeControlDiv.classList.remove("maptype-control-is-satellite");
map.setMapTypeId("roadmap");
};
document.querySelector(".maptype-control-satellite").onclick = function () {
mapTypeControlDiv.classList.remove("maptype-control-is-map");
mapTypeControlDiv.classList.add("maptype-control-is-satellite");
map.setMapTypeId("hybrid");
};
map.controls[google.maps.ControlPosition.LEFT_TOP].push(mapTypeControlDiv);
}
function initFullscreenControl(map) {
const elementToSendFullscreen = map.getDiv().firstChild;
const fullscreenControl = document.querySelector(".fullscreen-control");
map.controls[google.maps.ControlPosition.RIGHT_TOP].push(fullscreenControl);
fullscreenControl.onclick = function () {
if (isFullscreen(elementToSendFullscreen)) {
exitFullscreen();
} else {
requestFullscreen(elementToSendFullscreen);
}
};
document.onwebkitfullscreenchange =
document.onmsfullscreenchange =
document.onmozfullscreenchange =
...