Stay organized with collections
Save and categorize content based on your preferences.
Comparing layers
This example allows comparison of two layers with resizable map panes using the
Split.js
library.
TypeScript
functioninitMap():void{constmapOptions={center:{lat:44.5250489,lng:-110.83819},zoom:18,scaleControl:false,streetViewControl:false,};// instantiate the map on the left with control positioningmapLeft=newgoogle.maps.Map(document.getElementById("map-left")asHTMLElement,{...mapOptions,mapTypeId:"satellite",tilt:0,// at high zoom levels we need to maintain the same projectionfullscreenControlOptions:{position:google.maps.ControlPosition.LEFT_BOTTOM,},mapTypeControlOptions:{position:google.maps.ControlPosition.LEFT_TOP,},zoomControlOptions:{position:google.maps.ControlPosition.LEFT_BOTTOM,},});// instantiate the map on the right with control positioningmapRight=newgoogle.maps.Map(document.getElementById("map-right")asHTMLElement,{...mapOptions,fullscreenControlOptions:{position:google.maps.ControlPosition.RIGHT_BOTTOM,},mapTypeControlOptions:{position:google.maps.ControlPosition.RIGHT_TOP,},zoomControlOptions:{position:google.maps.ControlPosition.RIGHT_BOTTOM,},});// helper function to keep maps in syncfunctionsync(...maps:google.maps.Map[]){letcenter:google.maps.LatLng,zoom:number;functionupdate(changedMap){maps.forEach((m)=>{if(m===changedMap){return;}m.setCenter(center);m.setZoom(zoom);});}maps.forEach((m)=>{m.addListener("bounds_changed",()=>{constchangedCenter=m.getCenter()!;constchangedZoom=m.getZoom()!;if(changedCenter!==center||changedZoom!==zoom){center=changedCenter;zoom=changedZoom;update(m);}});});}sync(mapLeft,mapRight);functionhandleContainerResize(){constwidth=(document.getElementById("container")asHTMLElement).offsetWidth;(document.getElementById("map-left")asHTMLElement).style.width=`${width}px`;(document.getElementById("map-right")asHTMLElement).style.width=`${width}px`;}// trigger to set map container size since using absolutehandleContainerResize();// add event listenerwindow.addEventListener("resize",handleContainerResize);//@ts-ignoreSplit(["#left","#right"],{sizes:[50,50],});}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
functioninitMap(){constmapOptions={center:{lat:44.5250489,lng:-110.83819},zoom:18,scaleControl:false,streetViewControl:false,};// instantiate the map on the left with control positioningmapLeft=newgoogle.maps.Map(document.getElementById("map-left"),{...mapOptions,mapTypeId:"satellite",tilt:0,// at high zoom levels we need to maintain the same projectionfullscreenControlOptions:{position:google.maps.ControlPosition.LEFT_BOTTOM,},mapTypeControlOptions:{position:google.maps.ControlPosition.LEFT_TOP,},zoomControlOptions:{position:google.maps.ControlPosition.LEFT_BOTTOM,},});// instantiate the map on the right with control positioningmapRight=newgoogle.maps.Map(document.getElementById("map-right"),{...mapOptions,fullscreenControlOptions:{position:google.maps.ControlPosition.RIGHT_BOTTOM,},mapTypeControlOptions:{position:google.maps.ControlPosition.RIGHT_TOP,},zoomControlOptions:{position:google.maps.ControlPosition.RIGHT_BOTTOM,},});// helper function to keep maps in syncfunctionsync(...maps){letcenter,zoom;functionupdate(changedMap){maps.forEach((m)=>{if(m===changedMap){return;}m.setCenter(center);m.setZoom(zoom);});}maps.forEach((m)=>{m.addListener("bounds_changed",()=>{constchangedCenter=m.getCenter();constchangedZoom=m.getZoom();if(changedCenter!==center||changedZoom!==zoom){center=changedCenter;zoom=changedZoom;update(m);}});});}sync(mapLeft,mapRight);functionhandleContainerResize(){constwidth=document.getElementById("container").offsetWidth;document.getElementById("map-left").style.width=`${width}px`;document.getElementById("map-right").style.width=`${width}px`;}// trigger to set map container size since using absolutehandleContainerResize();// add event listenerwindow.addEventListener("resize",handleContainerResize);//@ts-ignoreSplit(["#left","#right"],{sizes:[50,50],});}window.initMap=initMap;
/* * 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;}#container,#left,#right,.map,.gutter{height:100%;}#left,#right,.gutter{float:left;position:relative;overflow:hidden;}#map-left{position:absolute;left:0;top:0;}#map-right{position:absolute;right:0;top:0;}.gutter{background-color:#eee;background-repeat:no-repeat;background-position:50%;}.gutter.gutter-horizontal{cursor:col-resize;background-image:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAeCAYAAADkftS9AAAAIklEQVQoU2M4c+bMfxAGAgYYmwGrIIiDjrELjpo5aiZeMwF+yNnOs5KSvgAAAABJRU5ErkJggg==");}
<html>
<head>
<title>Split Map Panes</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/split.js/1.5.11/split.min.js"></script>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="container">
<div id="left">
<div id="map-left" class="map"></div>
</div>
<div id="right">
<div id="map-right" class="map"></div>
</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>
Git and Node.js are required to run this sample locally. Follow these instructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eThis example demonstrates how to compare two Google Maps layers side-by-side using resizable panes.\u003c/p\u003e\n"],["\u003cp\u003eIt utilizes the Split.js library to create and manage the resizable panes for a seamless user experience.\u003c/p\u003e\n"],["\u003cp\u003eThe code synchronizes the two map instances, ensuring that they maintain the same center and zoom level as the user interacts with either pane.\u003c/p\u003e\n"],["\u003cp\u003eBoth TypeScript and JavaScript code snippets are provided, along with HTML and CSS for styling and structure.\u003c/p\u003e\n"],["\u003cp\u003eUsers can clone and run the sample locally using Git and Node.js or try it directly in Google Cloud Shell.\u003c/p\u003e\n"]]],["This code demonstrates comparing two map layers using resizable panes. It initializes two Google Maps instances (`mapLeft`, `mapRight`) with synchronized center and zoom. The `sync` function ensures both maps stay aligned during navigation. `handleContainerResize` sets map container sizes, and a `resize` event listener is added. The Split.js library is used to make the map panes resizable, with the panes set to an initial 50/50 split.\n"],null,["# Resizable Split Map Panes\n\nComparing layers\n----------------\n\nThis example allows comparison of two layers with resizable map panes using the\n[Split.js](https://github.com/nathancahill/split/tree/master/packages/splitjs)\nlibrary. \n\n### TypeScript\n\n```typescript\nfunction initMap(): void {\n const mapOptions = {\n center: { lat: 44.5250489, lng: -110.83819 },\n zoom: 18,\n scaleControl: false,\n streetViewControl: false,\n };\n\n // instantiate the map on the left with control positioning\n mapLeft = new google.maps.Map(\n document.getElementById(\"map-left\") as HTMLElement,\n {\n ...mapOptions,\n mapTypeId: \"satellite\",\n tilt: 0, // at high zoom levels we need to maintain the same projection\n fullscreenControlOptions: {\n position: google.maps.ControlPosition.LEFT_BOTTOM,\n },\n mapTypeControlOptions: {\n position: google.maps.ControlPosition.LEFT_TOP,\n },\n zoomControlOptions: {\n position: google.maps.ControlPosition.LEFT_BOTTOM,\n },\n }\n );\n\n // instantiate the map on the right with control positioning\n mapRight = new google.maps.Map(\n document.getElementById(\"map-right\") as HTMLElement,\n {\n ...mapOptions,\n fullscreenControlOptions: {\n position: google.maps.ControlPosition.RIGHT_BOTTOM,\n },\n mapTypeControlOptions: {\n position: google.maps.ControlPosition.RIGHT_TOP,\n },\n zoomControlOptions: {\n position: google.maps.ControlPosition.RIGHT_BOTTOM,\n },\n }\n );\n\n // helper function to keep maps in sync\n function sync(...maps: google.maps.Map[]) {\n let center: google.maps.LatLng, zoom: number;\n\n function update(changedMap) {\n maps.forEach((m) =\u003e {\n if (m === changedMap) {\n return;\n }\n\n m.setCenter(center);\n m.setZoom(zoom);\n });\n }\n\n maps.forEach((m) =\u003e {\n m.addListener(\"bounds_changed\", () =\u003e {\n const changedCenter = m.getCenter()!;\n const changedZoom = m.getZoom()!;\n\n if (changedCenter !== center || changedZoom !== zoom) {\n center = changedCenter;\n zoom = changedZoom;\n update(m);\n }\n });\n });\n }\n\n sync(mapLeft, mapRight);\n\n function handleContainerResize() {\n const width = (document.getElementById(\"container\") as HTMLElement)\n .offsetWidth;\n\n (\n document.getElementById(\"map-left\") as HTMLElement\n ).style.width = `${width}px`;\n (\n document.getElementById(\"map-right\") as HTMLElement\n ).style.width = `${width}px`;\n }\n\n // trigger to set map container size since using absolute\n handleContainerResize();\n // add event listener\n window.addEventListener(\"resize\", handleContainerResize);\n\n //@ts-ignore\n Split([\"#left\", \"#right\"], {\n sizes: [50, 50],\n });\n}\n\ndeclare global {\n interface Window {\n initMap: () =\u003e void;\n }\n}\nwindow.initMap = initMap;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/samples/split-map-panes/index.ts#L9-L112\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\n### JavaScript\n\n```javascript\nfunction initMap() {\n const mapOptions = {\n center: { lat: 44.5250489, lng: -110.83819 },\n zoom: 18,\n scaleControl: false,\n streetViewControl: false,\n };\n\n // instantiate the map on the left with control positioning\n mapLeft = new google.maps.Map(document.getElementById(\"map-left\"), {\n ...mapOptions,\n mapTypeId: \"satellite\",\n tilt: 0, // at high zoom levels we need to maintain the same projection\n fullscreenControlOptions: {\n position: google.maps.ControlPosition.LEFT_BOTTOM,\n },\n mapTypeControlOptions: {\n position: google.maps.ControlPosition.LEFT_TOP,\n },\n zoomControlOptions: {\n position: google.maps.ControlPosition.LEFT_BOTTOM,\n },\n });\n // instantiate the map on the right with control positioning\n mapRight = new google.maps.Map(document.getElementById(\"map-right\"), {\n ...mapOptions,\n fullscreenControlOptions: {\n position: google.maps.ControlPosition.RIGHT_BOTTOM,\n },\n mapTypeControlOptions: {\n position: google.maps.ControlPosition.RIGHT_TOP,\n },\n zoomControlOptions: {\n position: google.maps.ControlPosition.RIGHT_BOTTOM,\n },\n });\n\n // helper function to keep maps in sync\n function sync(...maps) {\n let center, zoom;\n\n function update(changedMap) {\n maps.forEach((m) =\u003e {\n if (m === changedMap) {\n return;\n }\n\n m.setCenter(center);\n m.setZoom(zoom);\n });\n }\n\n maps.forEach((m) =\u003e {\n m.addListener(\"bounds_changed\", () =\u003e {\n const changedCenter = m.getCenter();\n const changedZoom = m.getZoom();\n\n if (changedCenter !== center || changedZoom !== zoom) {\n center = changedCenter;\n zoom = changedZoom;\n update(m);\n }\n });\n });\n }\n\n sync(mapLeft, mapRight);\n\n function handleContainerResize() {\n const width = document.getElementById(\"container\").offsetWidth;\n\n document.getElementById(\"map-left\").style.width = `${width}px`;\n document.getElementById(\"map-right\").style.width = `${width}px`;\n }\n\n // trigger to set map container size since using absolute\n handleContainerResize();\n // add event listener\n window.addEventListener(\"resize\", handleContainerResize);\n //@ts-ignore\n Split([\"#left\", \"#right\"], {\n sizes: [50, 50],\n });\n}\n\nwindow.initMap = initMap;https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/split-map-panes/docs/index.js#L9-L94\n```\n| **Note:** The JavaScript is compiled from the TypeScript snippet.\n\n### CSS\n\n```css\n/* \n * Always set the map height explicitly to define the size of the div element\n * that contains the map. \n */\n#map {\n height: 100%;\n}\n\n/* \n * Optional: Makes the sample page fill the window. \n */\nhtml,\nbody {\n height: 100%;\n margin: 0;\n padding: 0;\n}\n\n#container,\n#left,\n#right,\n.map,\n.gutter {\n height: 100%;\n}\n\n#left,\n#right,\n.gutter {\n float: left;\n position: relative;\n overflow: hidden;\n}\n\n#map-left {\n position: absolute;\n left: 0;\n top: 0;\n}\n\n#map-right {\n position: absolute;\n right: 0;\n top: 0;\n}\n\n.gutter {\n background-color: #eee;\n background-repeat: no-repeat;\n background-position: 50%;\n}\n\n.gutter.gutter-horizontal {\n cursor: col-resize;\n background-image: url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAeCAYAAADkftS9AAAAIklEQVQoU2M4c+bMfxAGAgYYmwGrIIiDjrELjpo5aiZeMwF+yNnOs5KSvgAAAABJRU5ErkJggg==\");\n}\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/split-map-panes/docs/style.css#L7-L63\n```\n\n### HTML\n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eSplit Map Panes\u003c/title\u003e\n\n \u003cscript src=\"https://cdnjs.cloudflare.com/ajax/libs/split.js/1.5.11/split.min.js\"\u003e\u003c/script\u003e\n\n \u003clink rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" /\u003e\n \u003cscript type=\"module\" src=\"./index.js\"\u003e\u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv id=\"container\"\u003e\n \u003cdiv id=\"left\"\u003e\n \u003cdiv id=\"map-left\" class=\"map\"\u003e\u003c/div\u003e\n \u003c/div\u003e\n \u003cdiv id=\"right\"\u003e\n \u003cdiv id=\"map-right\" class=\"map\"\u003e\u003c/div\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n\n \u003c!-- \n The `defer` attribute causes the script to execute after the full HTML\n document has been parsed. For non-blocking uses, avoiding race conditions,\n and consistent behavior across browsers, consider loading using Promises. See\n https://developers.google.com/maps/documentation/javascript/load-maps-js-api\n for more information.\n --\u003e\n \u003cscript\n src=\"https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly\"\n defer\n \u003e\u003c/script\u003e\n \u003c/body\u003e\n\u003c/html\u003ehttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/split-map-panes/docs/index.html#L8-L39\n```\n\n### Try Sample\n\n[Google Cloud Shell](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fgooglemaps%2Fjs-samples&cloudshell_git_branch=sample-split-map-panes&cloudshell_tutorial=cloud_shell_instructions.md&cloudshell_workspace=.)\n\n### Clone Sample\n\n\nGit and Node.js are required to run this sample locally. Follow these [instructions](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) to install Node.js and NPM. The following commands clone, install dependencies and start the sample application. \n\n git clone -b sample-split-map-panes https://github.com/googlemaps/js-samples.git\n cd js-samples\n npm i\n npm start\n\n\nOther samples can be tried by switching to any branch beginning with `sample-`\u003cvar translate=\"no\"\u003eSAMPLE_NAME\u003c/var\u003e. \n\n git checkout sample-\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-nx\"\u003eSAMPLE_NAME\u003c/span\u003e\u003c/var\u003e\n npm i\n npm start"]]