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

# Creating maps on demand

<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/request_map/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/xf2m8w?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>
</div>
<p>
    In this example, the map is created when the "Show map" button is clicked.
</p>
<p>
    At first, an empty div container is placed on the page. The bind() function is used for the "button" element
    to assign a handler for the click event, which creates the map and puts it in the specified container when called.
</p>
<p>
    If the map has already been created when the button is clicked, the destroy() method is called and it destroys the map.
</p>

{% list tabs %}

-   index.html

    ```html
      <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Examples. Creating maps on demand.</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/
            -->
            
            <!--
                Enabling JQuery.
                Yandex provides hosting of JavaScript libraries:
            -->
            
            
        </head>

        <body>
            <input type="button" value="Show map" id="toggle"/>
            <p>
                <div id="map" style="width: 400px; height: 300px"></div>
            </p>
        </body>
    </html>
    ```

-   request_map.js

    ```js
    // As soon as the API is loaded and DOM is ready, let's perform the initialization
    ymaps.ready(init);

    // Initialization and destruction of the map when the button is clicked.
    function init() {
        var myMap;

        $("#toggle").bind({
            click: function () {
                if (!myMap) {
                    myMap = new ymaps.Map(
                        "map",
                        {
                            center: [55.010251, 82.958437], // Novosibirsk
                            zoom: 9,
                        },
                        {
                            searchControlProvider: "yandex#search",
                        }
                    );
                    $("#toggle").attr("value", "Hide map");
                } else {
                    myMap.destroy(); // Destructor of the map
                    myMap = null;
                    $("#toggle").attr("value", "Show the map again");
                }
            },
        });
    }
    ```

{% endlist %}
