A controller that allows the user to zoom the map.

For a list of events handled by this controller, see isZoomEvent.

You can customize this controller's behavior by overriding its hooks:

class CustomZoomController extends ZoomController {

getZoomFactor(gestureEvent: GestureEvent): ZoomFactor {
let zoomFactor = super.getZoomFactor(gestureEvent);

// turbo scroll zooming: zoom in and out twice as much as the default scroll behavior,
// when the CTRL key is pressed
if (gestureEvent.type === GestureEventType.SCROLL && gestureEvent.modifier === ModifierType.CTRL) {
if (zoomFactor.x > 1) {
zoomFactor = {
x: zoomFactor.x * 2,
y: zoomFactor.y * 2,
};
} else {
zoomFactor = {
x: zoomFactor.x / 2,
y: zoomFactor.y / 2
}
}
}

return zoomFactor;
}

}


const RIGHT_MOUSE_BUTTON = 2; // cf. https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
const ZOOM_BASE_FACTOR = 2;
const ZOOM_DRAG_DAMPENING_FACTOR = 50;

/**
* A controller that zooms in and out when you hold the right mouse button and drag the mouse up and down.
*/
class MouseDragZoomController extends ZoomController {

getZoomFactor(gestureEvent: GestureEvent): { x: number; y: number } {
let factor = 1;

if (this.isZoomEvent(gestureEvent)) {
const dragEvent = gestureEvent as DragEvent;
const deltaY = dragEvent.viewPoint.y - dragEvent.downEvent.viewPoint.y;
factor = Math.pow(ZOOM_BASE_FACTOR, deltaY / ZOOM_DRAG_DAMPENING_FACTOR);
}

return {x: factor, y: factor};
}

isZoomEvent(gestureEvent: GestureEvent): boolean {
const isRightMouseButtonDrag = gestureEvent.inputType === "mouse" &&
gestureEvent.type === GestureEventType.DRAG &&
((gestureEvent as DragEvent).downEvent.domEvent as MouseEvent).button === RIGHT_MOUSE_BUTTON;
return isRightMouseButtonDrag || this.isZoomEndEvent(gestureEvent);
}

isZoomEndEvent(gestureEvent: GestureEvent): boolean {
// only the DRAG_END event is an end event
return gestureEvent.inputType === "mouse" &&
gestureEvent.type === GestureEventType.DRAG_END &&
((gestureEvent as DragEvent).downEvent.domEvent as MouseEvent).button === RIGHT_MOUSE_BUTTON;
}
}

map.controller = new MouseDragZoomController();

Since

2022.1

Hierarchy

Constructors

Accessors

  • get cursor(): null | string
  • The CSS cursor to use on the map, for this controller. If null, the map will fall back to the previous cursor that was set on the map.

    Note that changing this cursor will update the cursor on the map's DOM node. When using multiple controllers (e.g. in a CompositeController), the controller that updates the cursor last (to a non-null value), will override any other non-null cursors of active controllers on the map.

    Returns null | string

    See

    Map.cursorManager

    Since

    2022.1

  • set cursor(cssCursor): void
  • Parameters

    • cssCursor: null | string

    Returns void

  • get map(): null | Map
  • The map on which this controller is currently active or null if this controller is not currently active. This property is read-only.

    Returns null | Map

  • set map(_value): void
  • Parameters

    • _value: null | Map

    Returns void

Methods

  • A hook to determine the zoom factor for a given gesture event. Factors larger than 1 zoom the map in, factors smaller than one zoom the map out. If 1 is returned, no zooming is performed.

    For georeferenced maps, the x and y factor should be the same. For cartesian maps, the x factor can be different from the y factor, so you can zoom on just 1 axis. For example, on a timeline (which is a cartesian map), you may want to zoom on just the x-axis.

    For incremental zooms, the factor is relative to the start of the zoom operation. For example, when the pinch started. For non-incremental zooms, the factor is relative to the current state of the map.

    The default implementation:

    • For scroll events, it keeps track how much the mouse wheel was scrolled during the current zoom animation, and returns a factor based on that. If the shift key is down, it zooms slower.
    • For double click events, it zooms out (factor < 1) if the right mouse button was used, otherwise it zooms in (factor > 1).
    • For pinch events, it uses PinchEvent.scaleFactorFromStart.

    An example of getZoomFactor customization is inverting the original zooming, so the map zooms in when the original controller would've zoomed out:

    /**
    * A controller that inverts the original zooming, ie. it zooms out when the original controller would've zoomed in.
    */
    class InvertedZoomController extends ZoomController {

    getZoomFactor(gestureEvent: GestureEvent): ZoomFactor {
    const originalFactors = super.getZoomFactor(gestureEvent);
    return {
    x: 1 / originalFactors.x,
    y: 1 / originalFactors.y
    }
    }

    }

    map.controller = new InvertedZoomController();

    Parameters

    • gestureEvent: GestureEvent

      The gesture event to determine a zoom factor for

    Returns ZoomFactor

  • A hook to customize the zoom target for the given gesture event. This can be a point in view (pixel), model or the map's reference. If it's a point in view reference, a zoom target underneath that view point is determined automatically. For incremental zooms, the target is determined at the start of the interaction. For example, at the start of a touch pinch gesture.

    Parameters

    • gestureEvent: GestureEvent

      The gesture event to determine a zoom target point for

    Returns null | Point

    a zoom target point, or null if no point could be determined

  • Call this method to indicate that the controller's appearance has changed. Calling this method ensures the onDraw will be called during the next rendering pass.

    Returns void

  • A hook to customize if the zooming should snap to scale levels. The default implementation snaps to scale levels if CTRL is held down.

    Parameters

    • gestureEvent: GestureEvent

      The gesture event to determine if zooming should snap to scale levels.

    Returns boolean

  • Callback that is invoked when this controller is activated on a map. This method allows controller implementations to perform setup work.

    Parameters

    • map: Map

      the map on which the controller has been activated

    Returns void

  • Callback that is invoked when this controller is deactivated on a map. This method allows controller implementations to perform cleanup work. This method must return either any resolved value or a promise to indicate completion of deactivation. This allows controller implementation to perform asynchronous deactivation work. During the period between this method being called and the resolution of the deactivation promise, this controller will no longer receive input events, but will still get the opportunity to draw itself.

    Parameters

    • map: Map

      the map on which the controller has been deactivated

    Returns any

    a concrete value to indicate immediate deactivation or a deactivation promise.

  • Callback that allows controller implementations to perform custom drawing on the map. Controller shapes and icons are drawn on top of all other content in the map. Note that the map may perform caching which may cause this method to only be invoked once. When a controller implementation's appearance changes the implementation should call invalidate on itself.

    Parameters

    • geoCanvas: GeoCanvas

      the GeoCanvas on which the controller can draw shapes.

    Returns void

  • Callback that allows controller implementations to draw labels on the map. Note that the map may perform caching which may cause this method to only be invoked once. When a controller implementation's appearance changes the implementation should call invalidate on itself.

    Parameters

    • labelCanvas: LabelCanvas

      the LabelCanvas on which the controller can draw labels.

    Returns void

  • Called when a key event has been received. This method must return a HandleEventResult value to indicate if the event was handled or not, If this method returns EVENT_IGNORED, the map will be given the opportunity to perform default key event behaviour. If default event handling is not desired, this method should return EVENT_HANDLED. (See the Controller class description for the default behavior.)

    Parameters

    • keyEvent: KeyEvent

      The key event to be handled. Note that this is a KeyEvent and not a DOMEvent. You can access the corresponding DOMEvent through KeyEvent.domEvent.

    Returns HandleEventResult

    The key event handling result.

Events

"Activated" event

  • on("Activated", callback: ((map) => void), context?: any) : Handle
  • An event indicating that this Controller has been activated. Activated means that the controller is active on the map, and the controller's onActivate has been called.

    You can use this event to set up UI elements or other listeners related to the controller and the controller's map.

    Parameters

    • event: "Activated"
    • callback: ((map) => void)
        • (map): void
        • Parameters

          Returns void

    • Optional context: any

    Returns Handle

    Since

    2021.0

"Deactivated" event

  • on("Deactivated", callback: ((map) => void), context?: any) : Handle
  • An event indicating that this Controller has been deactivated. Deactivated means that the controller has been removed from the map, and the controller's onDeactivate has been called.

    You can use this event to clean up UI elements or other listeners related to the controller and the controller's map.

    Parameters

    • event: "Deactivated"
    • callback: ((map) => void)
        • (map): void
        • Parameters

          Returns void

    • Optional context: any

    Returns Handle

    Since

    2021.0

"Invalidated" event

  • on("Invalidated", callback: (() => void), context?: any) : Handle
  • An event indicating that this Controller is invalidated. Invalidated means that the Controller requests for its onDraw to be called during the next rendering pass (because its appearance has changed). This event fires when invalidate is called.

    Parameters

    • event: "Invalidated"
    • callback: (() => void)
        • (): void
        • Returns void

    • Optional context: any

    Returns Handle