เมื่อเปิดใช้ความสามารถในการลาก ผู้ใช้จะลากเครื่องหมายบนแผนที่ได้โดยใช้เมาส์
หรือปุ่มลูกศร หากต้องการทำให้เครื่องหมายลากได้ ให้ตั้งค่าพร็อพเพอร์ตี้ AdvancedMarkerElement.gmpDraggable
เป็น true
แผนที่ตัวอย่างต่อไปนี้แสดงเครื่องหมายที่ลากได้ซึ่งแสดงตำแหน่งที่อัปเดตแล้วเมื่อลากเสร็จ (ระบบจะทริกเกอร์เหตุการณ์ dragend)
วิธีย้ายเครื่องหมายด้วยแป้นพิมพ์
- กดปุ่ม Tab จนกว่าจะโฟกัสเครื่องหมาย
- ใช้แป้นลูกศรเพื่อไปยังเครื่องหมายที่ต้องการ
- หากต้องการเปิดใช้งานการลาก ให้กด Option + Space หรือ Option + Enter (Mac), Alt + Space หรือ Alt + Enter (Windows)
- ใช้ปุ่มลูกศรเพื่อย้ายเครื่องหมาย
- หากต้องการวางเครื่องหมายในตำแหน่งใหม่ ให้กด Space หรือ Enter การดำเนินการนี้จะปิดการลากด้วย
- หากต้องการปิดการลากและย้ายเครื่องหมายกลับไปยังตำแหน่งก่อนหน้า ให้กด Esc
ดูรหัส
TypeScript
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; async function initMap() { // Request needed libraries. const { Map, InfoWindow } = (await google.maps.importLibrary( 'maps' )) as google.maps.MapsLibrary; const { AdvancedMarkerElement } = (await google.maps.importLibrary( 'marker' )) as google.maps.MarkerLibrary; 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.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); }); } initMap();
JavaScript
const mapElement = document.querySelector('gmp-map'); async function initMap() { // Request needed libraries. const { Map, InfoWindow } = (await google.maps.importLibrary('maps')); const { AdvancedMarkerElement } = (await 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.addListener('dragend', (event) => { const position = draggableMarker.position; infoWindow.close(); infoWindow.setContent(`Pin dropped at: ${position.lat}, ${position.lng}`); infoWindow.open(draggableMarker.map, draggableMarker); }); } initMap();
ตั้งค่าข้อความอธิบาย
หากต้องการตั้งค่าข้อความอธิบายเครื่องหมายซึ่งโปรแกรมอ่านหน้าจออ่านได้ ให้ใช้แอตทริบิวต์ AdvancedMarkerElement.title ดังที่แสดงที่นี่
const markerView = new google.maps.marker.AdvancedMarkerElement({
map,
position: { lat: 37.4239163, lng: -122.0947209 },
title: "Some descriptive text.",
});
เมื่อตั้งค่าแอตทริบิวต์ title แล้ว ข้อความจะปรากฏต่อโปรแกรมอ่านหน้าจอ และจะปรากฏเมื่อวางเมาส์เหนือเครื่องหมาย