Control elements

Both visual controls and a set of user-map interaction behaviors can be used to change the state of the map.

Visual elements

Visual elements are buttons and other graphic elements by means of which users can perform actions with the map.
For example, zoom in and out, plot routes, and so on. Visual elements are usually placed in the map viewport
and are fixed (this means that users can't move them).

In the library, visual objects are represented by classes with the Control postfix: YMapZoomControl, YMapGeolocationControl.
They are based on lower-level elements such as YMapControl, YMapControlButton.
Elements can be used together or separately.
In the former case, it's easier to manage their shared location.
To do this, add them to the shared YMapControls container.

More about visual elements

Positioning

The following directives are used to set element positioning:

  • left
  • right
  • bottom
  • top

Directives can be used together or separately in any order.

No. Example Value
1 top Placing the elements at the top horizontally in a line.
2 right Placing the elements on the right vertically in a line.
3 top left Placing the elements in the upper left corner (orientation is set separately).
4 right bottom Placing the elements in the lower right corner (orientation is set separately).

Orientation

The following directives are used to set element orientation:

  • horizontal
  • vertical

The orientation can be either derived from the position:

  • horizontal for top and bottom
  • vertical for left and right

or set separately (for all other positioning choices). By default horizontal is accepted.

Usage example

const map = new YMap(element, {
  location: {center: [25.229762, 55.289311], zoom: 14}
});
const controls = new YMapControls({position: 'top left', orientation: 'vertical'});
controls.addChild(new YMapGeolocationControlI({}));
controls.addChild(new YMapZoomControlI({}));
map.addChild(controls);

Creating custom elements

You can create custom visual elements.

Example

Adding a button that moves the map center to Moscow:

const map = new YMap(element, {
  location: {center: [37.588144, 55.733842], zoom: 14}
});

const controls = new YMapControls({position: 'top left'});
const button = new YMapControlButton({
  text: 'Moscow',
  onClick: () => {
    map.setLocation({
      center: [37.588144, 55.733842],
      zoom: 5
    });
  }
});

controls.addChild(button);
map.addChild(controls);

Behaviors

In addition to the visual controls, the map also has a set of behaviors: reaction to the mouse wheel,
dragging the map using the left mouse button, and so on. It is configured through the map's behaviors field.

const map = new YMap(element, {
  location: { center: [25.229762, 55.289311], zoom: 14},
  behaviors: ['drag', 'pinchZoom', 'mouseTilt']
});

To see the full list of available behaviors, go to Events.