Basic Map Types

This example replaces the default map tiles with custom imagery that displays the map's tile coordinates on a series of gray tiles.

Read thedocumentation.

TypeScript

/*
* This demo demonstrates how to replace default map tiles with custom imagery.
* In this case, the CoordMapType displays gray tiles annotated with the tile
* coordinates.
*
* Try panning and zooming the map to see how the coordinates change.
*/

classCoordMapType{
tileSize:google.maps.Size;
maxZoom=19;
name="Tile#s";
alt="TileCoordinateMapType";

constructor(tileSize:google.maps.Size){
this.tileSize=tileSize;
}

getTile(
coord:google.maps.Point,
zoom:number,
ownerDocument:Document
):HTMLElement{
constdiv=ownerDocument.createElement("div");

div.innerHTML=String(coord);
div.style.width=this.tileSize.width+"px";
div.style.height=this.tileSize.height+"px";
div.style.fontSize="10";
div.style.borderStyle="solid";
div.style.borderWidth="1px";
div.style.borderColor="#AAAAAA";
div.style.backgroundColor="#E5E3DF";
returndiv;
}

releaseTile(tile:HTMLElement):void{}
}

functioninitMap():void{
constmap=newgoogle.maps.Map(
document.getElementById("map")asHTMLElement,
{
zoom:10,
center:{lat:41.85,lng:-87.65},
streetViewControl:false,
mapTypeId:"coordinate",
mapTypeControlOptions:{
mapTypeIds:["coordinate","roadmap"],
style:google.maps.MapTypeControlStyle.DROPDOWN_MENU,
},
}
);

map.addListener("maptypeid_changed",()=>{
constshowStreetViewControl=
(map.getMapTypeId()asstring)!=="coordinate";

map.setOptions({
streetViewControl:showStreetViewControl,
});
});

// Now attach the coordinate map type to the map's registry.
map.mapTypes.set(
"coordinate",
newCoordMapType(newgoogle.maps.Size(256,256))
);
}

declareglobal{
interfaceWindow{
initMap:()=>void;
}
}
window.initMap=initMap;

JavaScript

/*
* This demo demonstrates how to replace default map tiles with custom imagery.
* In this case, the CoordMapType displays gray tiles annotated with the tile
* coordinates.
*
* Try panning and zooming the map to see how the coordinates change.
*/
classCoordMapType{
tileSize;
maxZoom=19;
name="Tile#s";
alt="TileCoordinateMapType";
constructor(tileSize){
this.tileSize=tileSize;
}
getTile(coord,zoom,ownerDocument){
constdiv=ownerDocument.createElement("div");

div.innerHTML=String(coord);
div.style.width=this.tileSize.width+"px";
div.style.height=this.tileSize.height+"px";
div.style.fontSize="10";
div.style.borderStyle="solid";
div.style.borderWidth="1px";
div.style.borderColor="#AAAAAA";
div.style.backgroundColor="#E5E3DF";
returndiv;
}
releaseTile(tile){}
}

functioninitMap(){
constmap=newgoogle.maps.Map(document.getElementById("map"),{
zoom:10,
center:{lat:41.85,lng:-87.65},
streetViewControl:false,
mapTypeId:"coordinate",
mapTypeControlOptions:{
mapTypeIds:["coordinate","roadmap"],
style:google.maps.MapTypeControlStyle.DROPDOWN_MENU,
},
});

map.addListener("maptypeid_changed",()=>{
constshowStreetViewControl=map.getMapTypeId()!=="coordinate";

map.setOptions({
streetViewControl:showStreetViewControl,
});
});
// Now attach the coordinate map type to the map's registry.
map.mapTypes.set(
"coordinate",
newCoordMapType(newgoogle.maps.Size(256,256)),
);
}

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;
}

HTML

<html>
<head>
<title>Basic Map Types</title>

<link rel= "stylesheet" type= "text/css" href= "./style.css" />
<script type= "module" src= "./index.js" ></script>
</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=weekly"
defer
></script>
</body>
</html>

Try Sample

Clone Sample

Git and Node.js are required to run this sample locally. Follow theseinstructionsto install Node.js and NPM. The following commands clone, install dependencies and start the sample application.

gitclone-bsample-maptype-basehttps://github.com/googlemaps/js-samples.git
cdjs-samples
npmi
npmstart

Other samples can be tried by switching to any branch beginning withsample-SAMPLE_NAME.

gitcheckoutsample-SAMPLE_NAME
npmi
npmstart