Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add an Aria Label to a Google Maps marker?

I would like to add an aria-label on a marker object. Currently I have a function that loads all the markers and want to put an aria-label as a property of the marker. Currently I am putting the aria label as a property when I create the marker object but I think this may be wrong. How could I add an aria label to a marker?

loadLocationMarkers({ lat, lng }, idx) {
    const markerIcon = this.createIcon(idx);

    const markerObj = new google.maps.Marker({
      icon: markerIcon,
      index: idx,
      selected: idx === this.selected,
      map: this.map,
      position: new google.maps.LatLng(lat, lng),
      optimized: false,
      zIndex: this.calculateZIndex(idx),
      'aria-label': 'Location Marker',
    });

    if (markerObj.selected) {
      this.selectedMarkerObj = markerObj;
    }

    markerObj.addListener('click', () => {
      const index = markerObj.get('index');
      this.dispatch(updateSelected(index), this.handleClick(markerObj));
    });
    return markerObj;
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use title which will set the aria-label.

https://developers.google.com/maps/documentation/javascript/reference/marker#MarkerOptions.title

new google.maps.Marker({
      icon: markerIcon,
      index: idx,
      selected: idx === this.selected,
      map: this.map,
      position: new google.maps.LatLng(lat, lng),
      optimized: false,
      zIndex: this.calculateZIndex(idx),
      title: 'Location Marker', // <--- added this
    });

From the output of the simple-markers sample with title="Hello World!".

<div
  aria-label="Hello World!"
  role="img"
  tabindex="-1"
  ...
>
  <img
    ...
  /><map
    ><area
      tabindex="-1"
      title="Hello World!"
      ...
  /></map>
</div>

Note: tabindex is -1 since there is no event listener on the marker.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading