asyncfunctioninitMap(){// Request needed libraries.const{Map}=awaitgoogle.maps.importLibrary("maps")asgoogle.maps.MapsLibrary;const{AdvancedMarkerElement}=awaitgoogle.maps.importLibrary("marker")asgoogle.maps.MarkerLibrary;constmap=newMap(document.getElementById('map')asHTMLElement,{center:{lat:37.424563902650114,lng:-122.09512859577026},zoom:17,mapId:'4504f8b37365c3d0',});constmarker01=newAdvancedMarkerElement({map,position:{lat:37.4239163,lng:-122.094},title:'This marker is visible at zoom level 15 and higher.'});constmarker02=newAdvancedMarkerElement({map,position:{lat:37.4245,lng:-122.096},title:'This marker is visible at zoom level 16 and higher.'});constmarker03=newAdvancedMarkerElement({map,position:{lat:37.4249,lng:-122.095},title:'This marker is visible at zoom level 17 and higher.'});constmarker04=newAdvancedMarkerElement({map,position:{lat:37.425,lng:-122.0955},title:'This marker is visible at zoom level 18 and higher.'});map.addListener('zoom_changed',()=>{constzoom=map.getZoom();if(zoom){// Only show each marker above a certain zoom level.marker01.map=zoom > 14?map:null;marker02.map=zoom > 15?map:null;marker03.map=zoom > 16?map:null;marker04.map=zoom > 17?map:null;}});}initMap();
asyncfunctioninitMap(){// Request needed libraries.const{Map}=awaitgoogle.maps.importLibrary("maps");const{AdvancedMarkerElement}=awaitgoogle.maps.importLibrary("marker");constmap=newMap(document.getElementById('map'),{center:{lat:37.424563902650114,lng:-122.09512859577026},zoom:17,mapId:'4504f8b37365c3d0',});constmarker01=newAdvancedMarkerElement({map,position:{lat:37.4239163,lng:-122.094},title:'This marker is visible at zoom level 15 and higher.'});constmarker02=newAdvancedMarkerElement({map,position:{lat:37.4245,lng:-122.096},title:'This marker is visible at zoom level 16 and higher.'});constmarker03=newAdvancedMarkerElement({map,position:{lat:37.4249,lng:-122.095},title:'This marker is visible at zoom level 17 and higher.'});constmarker04=newAdvancedMarkerElement({map,position:{lat:37.425,lng:-122.0955},title:'This marker is visible at zoom level 18 and higher.'});map.addListener('zoom_changed',()=>{constzoom=map.getZoom();if(zoom){// Only show each marker above a certain zoom level.marker01.map=zoom > 14?map:null;marker02.map=zoom > 15?map:null;marker03.map=zoom > 16?map:null;marker04.map=zoom > 17?map:null;}});}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;}
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 control the visibility of markers based on the map's zoom level.\u003c/p\u003e\n"],["\u003cp\u003eBy dynamically setting the \u003ccode\u003emarker.map\u003c/code\u003e property to either the map instance or \u003ccode\u003enull\u003c/code\u003e, markers can be shown or hidden as the user zooms in or out.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code utilizes the \u003ccode\u003eAdvancedMarkerElement\u003c/code\u003e class and listens to the \u003ccode\u003ezoom_changed\u003c/code\u003e event to adjust marker visibility accordingly.\u003c/p\u003e\n"],["\u003cp\u003eYou can view the marker visibility change by zooming in or out on the map provided in the sample.\u003c/p\u003e\n"]]],["The core functionality involves dynamically controlling the visibility of map markers based on the map's zoom level. Four markers are created, each with a specific zoom threshold for visibility. As the user zooms in or out, a `zoom_changed` listener checks the current zoom level. Based on the zoom, each marker's `map` property is either set to the map instance (visible) or `null` (hidden), thus altering its visibility. The CSS styles the map, ensuring it takes up the full screen.\n"],null,["This example shows controlling marker visibility by zoom level. See the markers'\nvisibility change (begin by zooming out):\n\nRead the\n[documentation](/maps/documentation/javascript/advanced-markers/collision-behavior#control_marker_visibility_by_map_zoom_level). \n\nTypeScript \n\n```typescript\nasync function initMap() {\n // Request needed libraries.\n const { Map } = await google.maps.importLibrary(\"maps\") as google.maps.MapsLibrary;\n const { AdvancedMarkerElement } = await google.maps.importLibrary(\"marker\") as google.maps.MarkerLibrary;\n\n const map = new Map(document.getElementById('map') as HTMLElement, {\n center: {lat: 37.424563902650114, lng: -122.09512859577026},\n zoom: 17,\n mapId: '4504f8b37365c3d0',\n });\n\n const marker01 = new AdvancedMarkerElement({\n map,\n position: { lat: 37.4239163, lng: -122.094 },\n title: 'This marker is visible at zoom level 15 and higher.'\n });\n\n const marker02 = new AdvancedMarkerElement({\n map,\n position: { lat: 37.4245, lng: -122.096 },\n title: 'This marker is visible at zoom level 16 and higher.'\n });\n\n const marker03 = new AdvancedMarkerElement({\n map,\n position: { lat: 37.4249, lng: -122.095 },\n title: 'This marker is visible at zoom level 17 and higher.'\n });\n\n const marker04 = new AdvancedMarkerElement({\n map,\n position: { lat: 37.425, lng: -122.0955 },\n title: 'This marker is visible at zoom level 18 and higher.'\n });\n map.addListener('zoom_changed', () =\u003e {\n const zoom = map.getZoom();\n if (zoom) {\n // Only show each marker above a certain zoom level.\n marker01.map = zoom \u003e 14 ? map : null;\n marker02.map = zoom \u003e 15 ? map : null;\n marker03.map = zoom \u003e 16 ? map : null;\n marker04.map = zoom \u003e 17 ? map : null;\n }\n });\n}\n\ninitMap();https://github.com/googlemaps-samples/js-api-samples/blob/5f4e6decfaf59cd69918bbf4e6338de03a63a5ba/samples/advanced-markers-zoom/index.ts#L9-L57\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\nJavaScript \n\n```javascript\nasync function initMap() {\n // Request needed libraries.\n const { Map } = await google.maps.importLibrary(\"maps\");\n const { AdvancedMarkerElement } = await google.maps.importLibrary(\"marker\");\n const map = new Map(document.getElementById('map'), {\n center: { lat: 37.424563902650114, lng: -122.09512859577026 },\n zoom: 17,\n mapId: '4504f8b37365c3d0',\n });\n const marker01 = new AdvancedMarkerElement({\n map,\n position: { lat: 37.4239163, lng: -122.094 },\n title: 'This marker is visible at zoom level 15 and higher.'\n });\n const marker02 = new AdvancedMarkerElement({\n map,\n position: { lat: 37.4245, lng: -122.096 },\n title: 'This marker is visible at zoom level 16 and higher.'\n });\n const marker03 = new AdvancedMarkerElement({\n map,\n position: { lat: 37.4249, lng: -122.095 },\n title: 'This marker is visible at zoom level 17 and higher.'\n });\n const marker04 = new AdvancedMarkerElement({\n map,\n position: { lat: 37.425, lng: -122.0955 },\n title: 'This marker is visible at zoom level 18 and higher.'\n });\n map.addListener('zoom_changed', () =\u003e {\n const zoom = map.getZoom();\n if (zoom) {\n // Only show each marker above a certain zoom level.\n marker01.map = zoom \u003e 14 ? map : null;\n marker02.map = zoom \u003e 15 ? map : null;\n marker03.map = zoom \u003e 16 ? map : null;\n marker04.map = zoom \u003e 17 ? map : null;\n }\n });\n}\ninitMap();https://github.com/googlemaps-samples/js-api-samples/blob/5f4e6decfaf59cd69918bbf4e6338de03a63a5ba/dist/samples/advanced-markers-zoom/docs/index.js#L8-L50\n```\n| **Note:** The JavaScript is compiled from the TypeScript snippet.\n\nCSS \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}\nhttps://github.com/googlemaps-samples/js-api-samples/blob/5f4e6decfaf59cd69918bbf4e6338de03a63a5ba/dist/samples/advanced-markers-zoom/docs/style.css#L7-L24\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eAdvanced Marker Zoom Visibility\u003c/title\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=\"map\"\u003e\u003c/div\u003e\n\n \u003c!-- prettier-ignore --\u003e\n \u003cscript\u003e(g=\u003e{var h,a,k,p=\"The Google Maps JavaScript API\",c=\"google\",l=\"importLibrary\",q=\"__ib__\",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=\u003eh||(h=new Promise(async(f,n)=\u003e{await (a=m.createElement(\"script\"));e.set(\"libraries\",[...r]+\"\");for(k in g)e.set(k.replace(/[A-Z]/g,t=\u003e\"_\"+t[0].toLowerCase()),g[k]);e.set(\"callback\",c+\".maps.\"+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=\u003eh=n(Error(p+\" could not load.\"));a.nonce=m.querySelector(\"script[nonce]\")?.nonce||\"\";m.head.append(a)}));d[l]?console.warn(p+\" only loads once. Ignoring:\",g):d[l]=(f,...n)=\u003er.add(f)&&u().then(()=\u003ed[l](f,...n))})\n ({key: \"AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8\", v: \"weekly\"});\u003c/script\u003e\n \u003c/body\u003e\n\u003c/html\u003ehttps://github.com/googlemaps-samples/js-api-samples/blob/5f4e6decfaf59cd69918bbf4e6338de03a63a5ba/dist/samples/advanced-markers-zoom/docs/index.html#L8-L22\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps-samples/js-api-samples/tree/main/dist/samples/advanced-markers-zoom/jsfiddle)\n\nClone 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 https://github.com/googlemaps-samples/js-api-samples.git\n cd samples/advanced-markers-zoom\n npm i\n npm start"]]