マーカーは、クリック可能またはドラッグ可能に設定することで、キーボードやマウスでの入力に反応させることができます。クリック可能なマーカーはマウスやキーボードで選択でき、ドラッグ可能であれば動かすこともできます。title
オプションで指定したテキストは、スクリーン リーダーの読み取り対象となります。
- マーカーをクリック可能にするには、クリック イベント ハンドラを追加します。
- マーカーをドラッグ可能にするには、
AdvancedMarkerView.draggable
プロパティをtrue
に設定します。 - マーカーの説明テキストを指定するには、
AdvancedMarkerView.title
オプションを使用します。
マーカーをクリック可能にする
次の例では、クリック可能、フォーカス可能なマーカーが地図に 5 つ表示されています。
キーボードでマーカーを操作する方法:
- Tab キーを押して 1 つ目のマーカーにフォーカスを合わせましょう。地図内にマーカーが複数ある場合、矢印キーでマーカー間を移動できます。
- マーカーがクリック可能な場合、Enter キーで「クリック」することができます。情報ウィンドウを持つマーカーは、クリックするか、Enter または Space キーを押せばウィンドウが表示されます。情報ウィンドウが閉じると、関連付けられたマーカーにフォーカスが戻ります。
- 地図上の他のコントロール間を移動するには 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://cdnjs.cloudflare.com/polyfill/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 キーを押します。
- 矢印キーで目的のマーカーにフォーカスを移します。
- ドラッグ操作モードを開始するには、Mac の場合は Option + Space または Option + Enter、Windows の場合は Alt + Space または Alt + Enter を押します。
- 矢印キーでマーカーをドラッグすることができます。
- マーカーを新しい位置にドロップするには、Space または 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://cdnjs.cloudflare.com/polyfill/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
属性が設定されていると、そのテキストはスクリーン リーダーの読み取り対象となり、マーカーにマウスカーソルを合わせた際にも表示されます。