Events
The Javascript API uses a developed mechanism for working with events. The Javascript API event model is used for working with browser, browser window, and DOM tree events, as well as API objects.
To use events, set up handlers — change in the state of the MMapListener class istance that is added as a child of the MMap map's root object.
Initializing the Listener
const clickCallback = () => alert('Oh, the event!');
const mouseMoveCallback = () => console.log('I am moving the mouse...');
// Creating a Listener object.
const mapListener = new MMapListener({
layer: 'any',
// Adding handlers to the Listener.
onClick: clickCallback,
onMouseMove: mouseMoveCallback
});
// Adding the Listener to the map.
map.addChild(mapListener);
Interface of the passed handler
Handlers passed to the Listener have the following interfaces, depending on the type of the event they handle:
MapEventHandler
The MapEventHandler is used for processing map events.
The handler is called with the following parameters:
-
for
onUpdatemethod:type—update.camera— MMapCamera object.location— MMapLocation object.mapInAction— Flag of the map animation at the handler call time.
-
for
onResizemethod:type—resize.size— Map container size in PixelCoordinates.mapInAction— Flag of the map animation at the handler call time.
Usage example:
const updateHandler = ({type, camera, location}) => {
console.log(type, camera.tilt);
console.log(location.zoom, location.center);
};
DomEventHandler
The DomEventHandler is used for processing DOM events.
The handler is called with the following parameters:
object: Object in which the event occurred. It contain fieldstypeandentity. Depends ontypefield,entityfield may be MMapFeatue, MMapMarker or MMapHotspot. If the click was made elsewhere apart from the map, theundefinedvalue is passed.event— Map event. It contain bothcoordinatesandscreenCoordinatesfields - geographic and screen coordinates of the location on the map that corresponds to the DOM point where the event occurred.
Usage example:
const clickHandler = (layer, coordinates, object) => {
if (object?.type === 'hotspot') {
console.log('Clicked on hotspot!');
}
};
When adding the DOM events handler to the listener, you need to set the layer — the ID of the layer an event occurs on, after which the corresponding handler is triggered.
// Creating a new Listener.
const mapListener = new MMapListener({
layer: 'any',
onClick: clickHandler
});
// Updating the existing Listener.
mapListener.update({
onClick: anotherClickHandler
});
// Updating the ID if necessary
// or if the value wasn't passed during initialization.
mapListener.update({
layer: 'markers',
onClick: clickHandler
});
BehaviorEventHandler
The BehaviorEventHandler handler is used for distinguishing between the types of events you can work with in the JS Maps API.
The handler is called with the following parameters:
type— Type of behavior, that occurred on the map.location— MMapLocation object.camera— MMapCamera object.
Usage example:
const behaviorHandler = ({type}) => {
if (type === 'pinchRotate') {
console.log('The user rotates the map with gestures!');
}
};
mapListener.update({onActionStart: behaviorHandler});
The behavior handler linked to onActionStart is called before all the MapEventHandler and DomEventHandler handlers. Behavior handlers linked to onActionEnd are called last:
const behaviorStartHandler = ({type}) => {
if (type === 'dblClick') {
console.log('1. Started double click.');
}
};
const behaviorEndHandler = ({type}) => {
if (type === 'dblClick') {
console.log('3. Finished double click.');
}
};
const dblClickHandler = () => console.log('2. Double click.');
mapListener.update({
onActionStart: behaviorStartHandler,
onActionEnd: behaviorEndHandler,
dblClick: dblClickHandler
});
Deleting handlers
All handlers are autmatically unlinked when MMapListener stops being the MMap child:
map.removeChild(mapListener); // mapListener is a MMapListener class instance.
You can also unlink specific handlers:
mapListener.update({
onClick: clickHandler,
dblClick: dblClickHandler,
onFastClick: fastClickHandler
});
// Later, when a handler is no longer needed.
// Deleting the dblClick handler.
mapListener.update({
dblClick: null
});
Supported events
The MMapListener class object constructor and the update() method take on an object with the properties listed below. Each of these properties is a handler called when a corresponding event is triggered.
MapEventHandler
onUpdate: Event activated when the map status changes.onResize: Event activated when the map size changes.onIndoorPlansChangedis an event triggered when changing the internal display of the building.
DomEventHandler
onTouchStartis an event similar to the'touchstart'native event.onTouchMoveis an event similar to the'touchmove'native event.onTouchEndis an event similar to the'touchend'native event.onTouchCancelis an event similar to the'touchcancel'native event.onPointerDownis an event similar to the'pointerdown'native event.onPointerMoveis an event similar to the'pointermove'native event.onPointerUpis an event similar to the'pointerup'native event.onPointerCancelis an event similar to the'pointercancel'native event.onClickis an event triggered when the left mouse button is clicked after a little pause (the pause is necessary to distinguish this click from double click).onDblClickis an event triggered when double-clicking the left mouse button.onFastClickis an event similar to the'click'native event.onRightDblClickis an event triggered when double-clicking the right mouse button.onMouseDownis an event similar to the'mousestart'native event.onMouseEnteris an event similar to the'mouseenter'native event.onMouseLeaveis an event similar to the'mouseleave'native event.onMouseMoveis an event similar to the'mousemove'native event.onContextMenuis an event similar to the'contextmenu'native event after a little pause (the pause is necessary to distinguish this click from double click).
BehaviorEventHandler
onActionStartis an event triggered when a map-related action is started.onActionEndis an event triggered when a map-related action is ended.
Behavior types
drag: Dragging the object.pinchZoom: Zoom in/Zoom out with a gesture (using two fingers).scrollZoom: Zoom in/Zoom out with the mouse scroll.dblClick: Double mouse click.magnifier: Selecting a rectangular map area by holding the right mouse button and zooming the map in on the selected rectangle according to the following principle: the length of the selected rectangle is 100% of the map container's viewport width after zooming.oneFingerZoom: Zoom in/Zoom out with one finger.mouseRotate: Rotate the map with the mouse.mouseTilt: Tilt the map with the mouse.pinchRotate: Rotate the map with the gesture.panTilt: Pinch the map with the gesture.