Replacing Default Controls Stay organized with collections Save and categorize content based on your preferences.
AI-generated Key Takeaways
This example demonstrates how to replace the default map controls with custom ones.
It covers creating custom zoom, map type, and fullscreen controls.
The sample provides code in both JavaScript and TypeScript.
Users can interact with a live demo through JSFiddle or Google Cloud Shell.
Instructions for cloning and running the sample locally are included.
Read thedocumentation.
TypeScript
declareglobal{interfaceDocument{mozCancelFullScreen?:()=>Promise<void>;msExitFullscreen?:()=>Promise<void>;webkitExitFullscreen?:()=>Promise<void>;mozFullScreenElement?:Element;msFullscreenElement?:Element;webkitFullscreenElement?:Element;onwebkitfullscreenchange?:any;onmsfullscreenchange?:any;onmozfullscreenchange?:any;}interfaceHTMLElement{msRequestFullScreen?:()=>Promise<void>;mozRequestFullScreen?:()=>Promise<void>;webkitRequestFullScreen?:()=>Promise<void>;}}letmap:google.maps.Map;functioninitMap():void{map=newgoogle.maps.Map(document.querySelector("#map")asHTMLElement,{center:{lat:-34.397,lng:150.644},zoom:8,disableDefaultUI:true,});initZoomControl(map);initMapTypeControl(map);initFullscreenControl(map);}functioninitZoomControl(map:google.maps.Map){(document.querySelector(".zoom-control-in")asHTMLElement).onclick=function(){map.setZoom(map.getZoom()!+1);};(document.querySelector(".zoom-control-out")asHTMLElement).onclick=function(){map.setZoom(map.getZoom()!-1);};map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.querySelector(".zoom-control")asHTMLElement);}functioninitMapTypeControl(map:google.maps.Map){constmapTypeControlDiv=document.querySelector(".maptype-control")asHTMLElement;(document.querySelector(".maptype-control-map")asHTMLElement).onclick=function(){mapTypeControlDiv.classList.add("maptype-control-is-map");mapTypeControlDiv.classList.remove("maptype-control-is-satellite");map.setMapTypeId("roadmap");};(document.querySelector(".maptype-control-satellite")asHTMLElement).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);}functioninitFullscreenControl(map:google.maps.Map){constelementToSendFullscreen=map.getDiv().firstChildasHTMLElement;constfullscreenControl=document.querySelector(".fullscreen-control")asHTMLElement;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=document.onfullscreenchange=function(){if(isFullscreen(elementToSendFullscreen)){fullscreenControl.classList.add("is-fullscreen");}else{fullscreenControl.classList.remove("is-fullscreen");}};}functionisFullscreen(element:HTMLElement){return((document.fullscreenElement||document.webkitFullscreenElement||document.mozFullScreenElement||document.msFullscreenElement)==element);}functionrequestFullscreen(element:HTMLElement){if(element.requestFullscreen){element.requestFullscreen();}elseif(element.webkitRequestFullScreen){element.webkitRequestFullScreen();}elseif(element.mozRequestFullScreen){element.mozRequestFullScreen();}elseif(element.msRequestFullScreen){element.msRequestFullScreen();}}functionexitFullscreen(){if(document.exitFullscreen){document.exitFullscreen();}elseif(document.webkitExitFullscreen){document.webkitExitFullscreen();}elseif(document.mozCancelFullScreen){document.mozCancelFullScreen();}elseif(document.msExitFullscreen){document.msExitFullscreen();}}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
JavaScript
letmap;functioninitMap(){map=newgoogle.maps.Map(document.querySelector("#map"),{center:{lat:-34.397,lng:150.644},zoom:8,disableDefaultUI:true,});initZoomControl(map);initMapTypeControl(map);initFullscreenControl(map);}functioninitZoomControl(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"),);}functioninitMapTypeControl(map){constmapTypeControlDiv=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);}functioninitFullscreenControl(map){constelementToSendFullscreen=map.getDiv().firstChild;constfullscreenControl=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=document.onfullscreenchange=function(){if(isFullscreen(elementToSendFullscreen)){fullscreenControl.classList.add("is-fullscreen");}else{fullscreenControl.classList.remove("is-fullscreen");}};}functionisFullscreen(element){return((document.fullscreenElement||document.webkitFullscreenElement||document.mozFullScreenElement||document.msFullscreenElement)==element);}functionrequestFullscreen(element){if(element.requestFullscreen){element.requestFullscreen();}elseif(element.webkitRequestFullScreen){element.webkitRequestFullScreen();}elseif(element.mozRequestFullScreen){element.mozRequestFullScreen();}elseif(element.msRequestFullScreen){element.msRequestFullScreen();}}functionexitFullscreen(){if(document.exitFullscreen){document.exitFullscreen();}elseif(document.webkitExitFullscreen){document.webkitExitFullscreen();}elseif(document.mozCancelFullScreen){document.mozCancelFullScreen();}elseif(document.msExitFullscreen){document.msExitFullscreen();}}window.initMap=initMap;
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;}.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)0px1px4px-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.controlsbutton{border:0;background-color:white;color:rgba(0,0,0,0.6);}.gm-style.controlsbutton:hover{color:rgba(0,0,0,0.9);}.gm-style.controls.zoom-control{display:flex;flex-direction:column;height:auto;}.gm-style.controls.zoom-controlbutton{font:0.85emArial;margin:1px;padding:0;}.gm-style.controls.maptype-control{display:flex;flex-direction:row;width:auto;}.gm-style.controls.maptype-controlbutton{display:inline-block;font-size:0.5em;margin:01px;padding:06px;}.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-controlbutton{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:2px002px;left:0.1em;top:0.1em;}.gm-style.controls.fullscreen-control.is-fullscreen.fullscreen-control-icon.fullscreen-control-top-left{border-width:02px2px0;}.gm-style.controls.fullscreen-control.fullscreen-control-icon.fullscreen-control-top-right{border-width:2px2px00;right:0.1em;top:0.1em;}.gm-style.controls.fullscreen-control.is-fullscreen.fullscreen-control-icon.fullscreen-control-top-right{border-width:002px2px;}.gm-style.controls.fullscreen-control.fullscreen-control-icon.fullscreen-control-bottom-left{border-width:002px2px;left:0.1em;bottom:0.1em;}.gm-style.controls.fullscreen-control.is-fullscreen.fullscreen-control-icon.fullscreen-control-bottom-left{border-width:2px2px00;}.gm-style.controls.fullscreen-control.fullscreen-control-icon.fullscreen-control-bottom-right{border-width:02px2px0;right:0.1em;bottom:0.1em;}.gm-style.controls.fullscreen-control.is-fullscreen.fullscreen-control-icon.fullscreen-control-bottom-right{border-width:2px002px;}
HTML
<html> <head> <title>Replacing Default Controls</title> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <div></div> <!-- Hide controls until they are moved into the map. --> <div> <div> <button title="Zoom In">+</button> <button title="Zoom Out">−</button> </div> <div> <button title="Show road map">Map</button> <button title="Show satellite imagery" > Satellite </button> </div> <div> <button title="Toggle Fullscreen"> <div ></div> <div ></div> <div ></div> <div ></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>
Try Sample
Clone Sample
Git and Node.js are required to run this sample locally. Follow theseinstructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
gitclone-bsample-control-replacementhttps://github.com/googlemaps/js-samples.gitcdjs-samplesnpminpmstart
Other samples can be tried by switching to any branch beginning withsample-SAMPLE_NAME.
gitcheckoutsample-SAMPLE_NAMEnpminpmstart
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-11-21 UTC.