Dragging
Let users move markers with mouse, touch or keyboard.
Set draggable and listen to the drag callbacks. Each one receives the DOM event and { latLng }.
The map loads when you scroll to it.
State idleLive …Saved 45.46420, 9.19000
Keyboard: Tab to the pin, Alt + Enter to grab it, arrow keys to move, Enter to drop, Esc to cancel.
const [position, setPosition] = useState({ lat: 45.4642, lng: 9.19 })
<Marker
lat={position.lat}
lng={position.lng}
anchor="bottom"
draggable
onDragStart={(event, { latLng }) => {}}
onDrag={(event, { latLng }) => {}}
onDragEnd={(event, { latLng }) => setPosition(latLng)}
>
<Pin />
</Marker>- Save the position in
onDragEnd. If you do not updatelat/lng, the marker stays where it was dropped until the props change. - A press that moves less than 3 pixels is a click, not a drag. A click that ends a drag does not fire
onClick. - While dragging, the map does not pan, and touch dragging does not scroll the page.
- Keyboard: focus the marker, press Alt + Enter to grab it, move it with the arrow keys (Shift for bigger steps), Enter to drop, Esc to cancel. The same callbacks fire. Give the marker a
title: it is the name screen readers announce. See Accessibility.
With the advanced renderer Google handles dragging natively (gmpDraggable). With the overlay renderer the library uses pointer events with pointer capture. The callbacks are the same.