Handling map events
Register an event lister for events that a map object dispatches.
Overview
MapKit JS uses events that developers can implement to hook into points in the life cycle of the map, as well as user interactions. Your code can respond to events by registering an event listener.
Events have a target property. The target of an event is the object dispatching that event. When you add an event listener to a map, the target is the map itself. The select and deselect events have an annotation or overlay property that reports the selected or deselected item. The single-tap, double-tap, and long-press events have pointOnPage and domEvents properties that provide more data about the user interactions.
Event listeners receive a single argument that is an event object. The event objects are similar to native event objects, though calls to event.preventDefault() or event.stopPropagation() don’t halt some actions that trigger these events. Scrolling, zooming, and panning, as well as zooming or rotating after a long press, provide methods to halt or prevent interaction.
Respond to map display events
MapKit JS sends the following MapKitEvent events that allow you to respond to changes in the map display and a person’s interactions with the map and its controls.
region-change-startThe map’s visible region is about to change.
region-change-endThe map’s visible region finishes changing.
rotation-startThe map’s rotation is about to change.
rotation-endThe map’s rotation finishes changing.
scroll-startThe map is about to scroll as a result of user interaction.
scroll-endThe map finishes scrolling as a result of user interaction.
zoom-startThe map is about to zoom as a result of user interaction.
zoom-endThe map finishes zooming as a result of user interaction.
map-type-changeA program event or a user interaction causes the map’s
mapType(MapType) to change.
Respond to annotation and overlay events
MapKit JS sends MapAnnotationSelectionEvent,MapOverlaySelectionEvent, and MapAnnotationDragEvent events when a person interacts with the annotations and overlays you place on the map. Respond to these interactions by updating the map or providing more information about the overlay or annotation the person interacts with.
selectThe framework sends a MapAnnotationSelectionEvent or MapOverlaySelectionEventwhen someone selects an annotation or an overlay.
deselectThe framework sends a MapAnnotationSelectionEvent or MapOverlaySelectionEventwhen someone deselects an annotation or an overlay.
drag-startThe framework sends a MapAnnotationDragEvent when someone starts to drag an annotation.
draggingThe framework send a MapAnnotationDragEvent when someone drags an annotation. The event has two properties.
drag-endThe framework send a MapAnnotationDragEvent when someone finishes dragging an annotation.
Respond to user location events
MapKit JS sends MapUserLocationChangeEvent or MapUserLocationErrorEvent events to indicate changes in a person’s location. Use these events to adjust what the map displays or to trigger other responses in your app.
user-location-changeThe framework sends a MapUserLocationChangeEvent event when showsUserLocation is
trueand the map acquires the user’s location, or after an automatic update.user-location-errorThe framework sends a (MapUserLocationErrorEvent) when MapKit JS is unable to acquire the user’s location.
Respond to map interaction events
MapKit JS sends MapEvent events when someone taps or presses on the map view. Use these events to adjust the map view, respond to the deselecting of overlays, or drag or deselect annotations.
single-tapThe framework sends an event when a single tap occurs on the map outside an annotation or an overlay. If an annotation or an overlay is in a selected state when a single tap occurs, MapKit JS deselects the annotation or the overlay and dispatches a single-tap event.
double-tapThe framework sends an event when a double tap occurs on the map without zooming the map.
long-pressThe framework send an event when a long press occurs on the map outside an annotation. A long press may be the beginning of a panning or pinching gesture on the map. You can prevent the gesture from starting by calling the preventDefault() method of the event. Annotations need to be draggable to dispatch long-press events.