---
metadata:
  - name: generator
    content: Diplodoc Platform v5.39.1
alternate:
  - https://yandex.com.tr/dev/jsapi-v2-1/doc/en/v2-1/examples/cases/custom_control.md
  - https://yandex.com.tr/dev/jsapi-v2-1/doc/ru/v2-1/examples/cases/custom_control.md
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com.tr/dev/jsapi-v2-1/doc/en/llms.txt

# Custom control

<iframe id="LIVE-EXAMPLE" style="width:100%;height: 400px;border-radius: 8px;border: 1px solid rgba(92, 94, 102, 0.14);" frameBorder="0" src="https://yastatic.net/s3/front-maps-static/maps-front-jsapi-v2-1/examples/2/out/s3-cases/en/custom_control/index.html" allow="fullscreen"></iframe>

<a target="_blank" rel="noopener noreferrer" style="display: inline-block;margin-top: 10px;cursor: pointer;border-radius: 4px;padding: 9px 12px;background: #151515;color: white;text-decoration: none;font-weight: 500" href="https://codesandbox.io/p/sandbox/mjcvkl?file=index.html">Open in CodeSandbox</a>

<div id="references">
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/ready">ready</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/Map">Map</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/collection.Item">collection.Item</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/collection.Item">IControl</a>
</div>
<p>
    The example shows how to create custom controls. The control class
	must implement the <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/collection.Item">IControl</a> interface.
</p>
<p>
    For faster development, you can inherit the control class from the <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/collection.Item">collection.Item</a> class.
</p>

{% list tabs %}

-   index.html

    ```html
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Examples. Custom control.</title>
            <meta
                http-equiv="Content-Type"
                content="text/html; charset=UTF-8"
            />
            <!--
            Set your own API-key. Testing key is not valid for other web-sites and services.
            Get your API-key on the Developer Dashboard: https://developer.tech.yandex.ru/keys/
        -->
            
            
            
            
        </head>
        <body>
            <div id="map"></div>
        </body>
    </html>
    ```

-   custom_control.js

    ```js
    ymaps.ready(function () {
        /**
         * An example implementation of a custom control based on inheritance from collection.Item.
         * The control displays the name of the object that is in the center of the map.
         */
        var map = new ymaps.Map("map", {
                center: [55.819543, 37.611619],
                zoom: 6,
                controls: [],
            }),
            // Creating a custom class.
            CustomControlClass = function (options) {
                CustomControlClass.superclass.constructor.call(this, options);
                this._$content = null;
                this._geocoderDeferred = null;
            };
        // And inheriting it from the collection.Item.
        ymaps.util.augment(CustomControlClass, ymaps.collection.Item, {
            onAddToMap: function (map) {
                CustomControlClass.superclass.onAddToMap.call(this, map);
                this._lastCenter = null;
                this.getParent()
                    .getChildElement(this)
                    .then(this._onGetChildElement, this);
            },

            onRemoveFromMap: function (oldMap) {
                this._lastCenter = null;
                if (this._$content) {
                    this._$content.remove();
                    this._mapEventGroup.removeAll();
                }
                CustomControlClass.superclass.onRemoveFromMap.call(
                    this,
                    oldMap
                );
            },

            _onGetChildElement: function (parentDomContainer) {
                // Creating an HTML element with the text.
                this._$content = $(
                    '<div class="customControl"></div>'
                ).appendTo(parentDomContainer);
                this._mapEventGroup = this.getMap().events.group();
                // Requesting data after changing the position of the map.
                this._mapEventGroup.add(
                    "boundschange",
                    this._createRequest,
                    this
                );
                // Immediately requesting the name of the place.
                this._createRequest();
            },

            _createRequest: function () {
                var lastCenter = (this._lastCenter = this.getMap()
                    .getCenter()
                    .join(","));
                // Requesting information about the place by the coordinates of the map center.
                ymaps
                    .geocode(this._lastCenter, {
                        // Specifying that the response should be in JSON format.
                        json: true,
                        // Setting a limit on the number of records in the response.
                        results: 1,
                    })
                    .then(function (result) {
                        // We will only process the response from the last request.
                        if (lastCenter == this._lastCenter) {
                            this._onServerResponse(result);
                        }
                    }, this);
            },

            _onServerResponse: function (result) {
                /**
                 * The data has been received from the server and now it must be displayed.
                 * Description of the response in JSON format.
                 */
                var members = result.GeoObjectCollection.featureMember,
                    geoObjectData =
                        members && members.length ? members[0].GeoObject : null;
                if (geoObjectData) {
                    this._$content.text(
                        geoObjectData.metaDataProperty.GeocoderMetaData.text
                    );
                }
            },
        });

        var customControl = new CustomControlClass();
        map.controls.add(customControl, {
            float: "none",
            position: {
                top: 10,
                left: 10,
            },
        });
    });
    ```

{% endlist %}
