Khi bật khả năng kéo, người dùng có thể kéo điểm đánh dấu trên bản đồ bằng chuột hoặc phím mũi tên. Để cho phép kéo điểm đánh dấu, hãy đặt thuộc tính AdvancedMarkerElement.gmpDraggable thành true.
Bản đồ ví dụ sau đây cho thấy một điểm đánh dấu có thể kéo, hiển thị vị trí đã cập nhật khi quá trình kéo kết thúc (sự kiện gmp-dragend được kích hoạt):
Cách kéo điểm đánh dấu bằng bàn phím:
- Nhấn phím tab cho đến khi điểm đánh dấu được lấy tiêu điểm.
- Dùng phím mũi tên để di chuyển đến điểm đánh dấu mong muốn.
- Để kích hoạt thao tác kéo, hãy nhấn tổ hợp phím Option + Dấu cách hoặc Option + Enter (máy Mac), Alt + Dấu cách hoặc Alt + Enter (máy Windows).
- Dùng các phím mũi tên để di chuyển điểm đánh dấu.
- Để thả điểm đánh dấu tại vị trí mới, hãy nhấn Phím cách hoặc phím Enter. Thao tác này cũng sẽ tắt tính năng kéo.
- Để tắt tính năng kéo và đưa điểm đánh dấu về vị trí trước đó, hãy nhấn phím Esc.
Xem mã
TypeScript
const mapElement = document.querySelector('gmp-map')!; async function init() { // Request needed libraries. const [{ InfoWindow }, { AdvancedMarkerElement }] = await Promise.all([ google.maps.importLibrary('maps'), google.maps.importLibrary('marker'), ]); const infoWindow = new InfoWindow(); const draggableMarker = new AdvancedMarkerElement({ position: { lat: 37.39094933041195, lng: -122.02503913145092 }, gmpDraggable: true, title: 'This marker is draggable.', }); mapElement.append(draggableMarker); draggableMarker.addEventListener('gmp-dragend', () => { const position = draggableMarker.position as google.maps.LatLngAltitude; infoWindow.close(); infoWindow.setContent( `Pin dropped at: ${String(position.lat)}, ${String(position.lng)}` ); infoWindow.open(draggableMarker.map, draggableMarker); }); } void init();
JavaScript
const mapElement = document.querySelector('gmp-map'); async function init() { // Request needed libraries. const [{ InfoWindow }, { AdvancedMarkerElement }] = await Promise.all([ google.maps.importLibrary('maps'), google.maps.importLibrary('marker'), ]); const infoWindow = new InfoWindow(); const draggableMarker = new AdvancedMarkerElement({ position: { lat: 37.39094933041195, lng: -122.02503913145092 }, gmpDraggable: true, title: 'This marker is draggable.', }); mapElement.append(draggableMarker); draggableMarker.addEventListener('gmp-dragend', () => { const position = draggableMarker.position; infoWindow.close(); infoWindow.setContent( `Pin dropped at: ${String(position.lat)}, ${String(position.lng)}` ); infoWindow.open(draggableMarker.map, draggableMarker); }); } void init();
Đặt văn bản mô tả
Để đặt văn bản mô tả cho điểm đánh dấu mà trình đọc màn hình có thể đọc, hãy sử dụng thuộc tính AdvancedMarkerElement.title như minh hoạ dưới đây:
const markerView = new google.maps.marker.AdvancedMarkerElement({
map,
position: { lat: 37.4239163, lng: -122.0947209 },
title: "Some descriptive text.",
});
Khi bạn đặt thuộc tính title, trình đọc màn hình sẽ nhìn thấy văn bản và văn bản sẽ xuất hiện khi người dùng di chuột qua điểm đánh dấu.