Marker Accessibility Stay organized with collections Save and categorize content based on your preferences.
AI-generated Key Takeaways
google.maps.Markeris deprecated, withgoogle.maps.marker.AdvancedMarkerElementrecommended as its replacement.This example demonstrates how to create accessible and focusable markers on a Google Map using the Maps JavaScript API.
Users can navigate between markers using keyboard controls (tab, arrow keys, enter, spacebar) for improved accessibility.
The sample code showcases creating markers with info windows, setting their positions and titles, and handling click events for displaying information.
The example includes code snippets in TypeScript, JavaScript, CSS, and HTML, along with instructions for running the sample locally or trying it online.
As of February 21st, 2024 (v3.56), google.maps.Marker is deprecated. We encourage you to transition to the newgoogle.maps.marker.AdvancedMarkerElement class. Advanced markers provide substantial improvements over the legacygoogle.maps.Marker class. The minimum version of the Maps JavaScript API withgoogle.maps.marker.AdvancedMarkerElement is 3.53.2. At this time, google.maps.Marker is not scheduled to be discontinued.
This example creates five markers which are accessible. The first markerreceives focus when tab is pressed; you can then use the arrow keys to movebetween markers. Press tab again to continue moving through the rest of the mapcontrols. If a marker has an info window, you can open it by clicking, or bypressing the enter key or space bar. When the info window closes, focus willreturn to the associated marker.
Read thedocumentation.
TypeScript
// The following example creates five accessible and// focusable markers.functioninitMap():void{constmap=newgoogle.maps.Map(document.getElementById("map")asHTMLElement,{zoom:12,center:{lat:34.84555,lng:-111.8035},});// Set LatLng and title text for the markers. The first marker (Boynton Pass)// receives the initial focus when tab is pressed. Use arrow keys to// move between markers; press tab again to cycle through the map controls.consttourStops:[google.maps.LatLngLiteral,string][]=[[{lat:34.8791806,lng:-111.8265049},"Boynton Pass"],[{lat:34.8559195,lng:-111.7988186},"Airport Mesa"],[{lat:34.832149,lng:-111.7695277},"Chapel of the Holy Cross"],[{lat:34.823736,lng:-111.8001857},"Red Rock Crossing"],[{lat:34.800326,lng:-111.7665047},"Bell Rock"],];// Create an info window to share between markers.constinfoWindow=newgoogle.maps.InfoWindow();// Create the markers.tourStops.forEach(([position,title],i)=>{constmarker=newgoogle.maps.Marker({position,map,title:`${i+1}.${title}`,label:`${i+1}`,optimized:false,});// Add a click listener for each marker, and set up the info window.marker.addListener("click",()=>{infoWindow.close();infoWindow.setContent(marker.getTitle());infoWindow.open(marker.getMap(),marker);});});}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
JavaScript
// The following example creates five accessible and// focusable markers.functioninitMap(){constmap=newgoogle.maps.Map(document.getElementById("map"),{zoom:12,center:{lat:34.84555,lng:-111.8035},});// Set LatLng and title text for the markers. The first marker (Boynton Pass)// receives the initial focus when tab is pressed. Use arrow keys to// move between markers; press tab again to cycle through the map controls.consttourStops=[[{lat:34.8791806,lng:-111.8265049},"Boynton Pass"],[{lat:34.8559195,lng:-111.7988186},"Airport Mesa"],[{lat:34.832149,lng:-111.7695277},"Chapel of the Holy Cross"],[{lat:34.823736,lng:-111.8001857},"Red Rock Crossing"],[{lat:34.800326,lng:-111.7665047},"Bell Rock"],];// Create an info window to share between markers.constinfoWindow=newgoogle.maps.InfoWindow();// Create the markers.tourStops.forEach(([position,title],i)=>{constmarker=newgoogle.maps.Marker({position,map,title:`${i+1}.${title}`,label:`${i+1}`,optimized:false,});// Add a click listener for each marker, and set up the info window.marker.addListener("click",()=>{infoWindow.close();infoWindow.setContent(marker.getTitle());infoWindow.open(marker.getMap(),marker);});});}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>Marker Accessibility</title> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <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-marker-accessibilityhttps://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.