当标记可点击或可拖动时,可以响应键盘和鼠标输入。您可以使用鼠标或键盘在标记之间移动,以及在标记可拖动的情况下移动标记。title
选项中指定的文本对屏幕阅读器可见。
- 如需将标记设为可点击,请添加点击事件处理脚本。
- 如需使标记可拖动,请将
AdvancedMarkerView.draggable
属性设置为true
。 - 如要为标记设置描述性文本,请使用
AdvancedMarkerView.title
选项。
将标记设置为可点击
以下示例展示了一个带有五个可点击且可聚焦标记的地图:
如要使用键盘浏览标记,请执行以下操作:
- 使用 Tab 键将焦点移至第一个标记;如果同一地图上有多个标记,可使用箭头键在标记之间循环切换。
- 如果标记可点击,请按 Enter 键进行“点击”。如果某个标记包含信息窗口,您可以通过点击或按 Enter 键或空格键将其打开。关闭信息窗口后,焦点将返回到关联的标记。
- 再按一次 Tab 键,可以继续在其余地图控件之间移动。
查看代码
TypeScript
function initMap() { const map = new google.maps.Map(document.getElementById("map") as HTMLElement, { zoom: 12, center: { lat: 34.84555, lng: -111.8035 }, mapId: '4504f8b37365c3d0', }); // 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. const tourStops = [ { position: { lat: 34.8791806, lng: -111.8265049 }, title: "Boynton Pass" }, { position: { lat: 34.8559195, lng: -111.7988186 }, title: "Airport Mesa" }, { position: { lat: 34.832149, lng: -111.7695277 }, title: "Chapel of the Holy Cross" }, { position: { lat: 34.823736, lng: -111.8001857 }, title: "Red Rock Crossing" }, { position: { lat: 34.800326, lng: -111.7665047 }, title: "Bell Rock" }, ]; // Create an info window to share between markers. const infoWindow = new google.maps.InfoWindow(); // Create the markers. tourStops.forEach(({position, title}, i) => { const pinView = new google.maps.marker.PinView({ glyph: `${i + 1}`, }); const marker = new google.maps.marker.AdvancedMarkerView({ position, map, title: `${i + 1}. ${title}`, content: pinView.element, }); // Add a click listener for each marker, and set up the info window. marker.addListener('click', ({ domEvent, latLng }) => { const { target } = domEvent; infoWindow.close(); infoWindow.setContent(marker.title); infoWindow.open(marker.map, marker); }); }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 12, center: { lat: 34.84555, lng: -111.8035 }, mapId: "4504f8b37365c3d0", }); // 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. const tourStops = [ { position: { lat: 34.8791806, lng: -111.8265049 }, title: "Boynton Pass", }, { position: { lat: 34.8559195, lng: -111.7988186 }, title: "Airport Mesa", }, { position: { lat: 34.832149, lng: -111.7695277 }, title: "Chapel of the Holy Cross", }, { position: { lat: 34.823736, lng: -111.8001857 }, title: "Red Rock Crossing", }, { position: { lat: 34.800326, lng: -111.7665047 }, title: "Bell Rock", }, ]; // Create an info window to share between markers. const infoWindow = new google.maps.InfoWindow(); // Create the markers. tourStops.forEach(({ position, title }, i) => { const pinView = new google.maps.marker.PinView({ glyph: `${i + 1}`, }); const marker = new google.maps.marker.AdvancedMarkerView({ position, map, title: `${i + 1}. ${title}`, content: pinView.element, }); // Add a click listener for each marker, and set up the info window. marker.addListener("click", ({ domEvent, latLng }) => { const { target } = domEvent; infoWindow.close(); infoWindow.setContent(marker.title); infoWindow.open(marker.map, 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; } [class$=api-load-alpha-banner] { display: none; }
HTML
<html> <head> <title>Advanced Marker Accessibility</title> <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> <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 callback 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&libraries=marker&v=beta" defer ></script> </body> </html>
试用示例
若要使标记不再可点击,请移除点击事件监听器:
// Adding the listener.
const clickListener = markerView.addListener('click', () => {...});
// Removing the listener.
google.maps.event.removeListener(clickListener);
使标记可拖动
启用可拖动性后,用户可以使用鼠标或箭头键拖动地图上的标记。如需使标记可拖动,请将 AdvancedMarkerView.draggable
属性设置为 true
。
以下示例地图展示了一个可拖动的标记,该标记会在拖动完成时显示其更新后的位置(触发 dragend
事件):
如要使用键盘拖动标记,请执行以下操作:
- 持续按 Tab 键,直至标记对焦。
- 使用箭头键移动到所需的标记。
- 如要启用拖动功能,请按 Option + Space 或 Option + Enter (Mac)、ALT + Space 或 Alt + Enter (Windows)。
- 使用箭头键可移动标记。
- 要将标记放在新位置,请按空格键或 Enter。此操作也将关闭拖动功能。
- 如需关闭拖动功能并将标记移回之前的位置,请按 Esc。
查看代码
TypeScript
function initMap() { const map = new google.maps.Map(document.getElementById('map') as HTMLElement, { center: {lat: 37.39094933041195, lng: -122.02503913145092}, zoom: 14, mapId: '4504f8b37365c3d0', }); const infoWindow = new google.maps.InfoWindow(); const draggableMarker = new google.maps.marker.AdvancedMarkerView({ map, position: {lat: 37.39094933041195, lng: -122.02503913145092}, draggable: true, title: "This marker is draggable.", }); draggableMarker.addListener('dragend', (event) => { const position = draggableMarker.position as google.maps.LatLng; infoWindow.close(); infoWindow.setContent(`Pin dropped at: ${position.lat()}, ${position.lng()}`); infoWindow.open(draggableMarker.map, draggableMarker); }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
function initMap() { const map = new google.maps.Map(document.getElementById("map"), { center: { lat: 37.39094933041195, lng: -122.02503913145092 }, zoom: 14, mapId: "4504f8b37365c3d0", }); const infoWindow = new google.maps.InfoWindow(); const draggableMarker = new google.maps.marker.AdvancedMarkerView({ map, position: { lat: 37.39094933041195, lng: -122.02503913145092 }, draggable: true, title: "This marker is draggable.", }); draggableMarker.addListener("dragend", (event) => { const position = draggableMarker.position; infoWindow.close(); infoWindow.setContent( `Pin dropped at: ${position.lat()}, ${position.lng()}` ); infoWindow.open(draggableMarker.map, draggableMarker); }); } 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; } [class$=api-load-alpha-banner] { display: none; }
HTML
<html> <head> <title>Draggable Advanced Marker</title> <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> <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 callback 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&libraries=marker&v=beta" defer ></script> </body> </html>
试用示例
设置说明性文字
如需设置可供屏幕阅读器读取的标记的说明性文字,请使用 AdvancedMarkerView.title
属性,如下所示:
const markerView = new google.maps.marker.AdvancedMarkerView({
map,
position: { lat: 37.4239163, lng: -122.0947209 },
title: "Some descriptive text.",
});
设置 title
属性后,文本内容对屏幕阅读器可见,并且将鼠标悬停在标记上时会显示文本。