Asynchronous Loading | MapGL | 2GIS Documentation

Asynchronous loading

To load the map asynchronously, you can use the async and defer script attributes. You can specify the function that will be called when the script has finished loading by using the callback parameter in the URL.

<script src="https://mapgl.2gis.com/api/js/v1?callback=initMap" async defer></script>
<script>
    function initMap() {
        const map = new mapgl.Map('container', {
            center: [55.31878, 25.23584],
            zoom: 13,
            key: 'Your API access key',
        });
    }
</script>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>2GIS Map API</title>
        <meta name="description" content="Example of an async script loading" />
        <style>
            html,
            body,
            #container {
                margin: 0;
                width: 100%;
                height: 100%;
                overflow: hidden;
            }
        </style>
    </head>
    <body>
        <div id="container"></div>
        <script>
            function initMap() {
                const map = new mapgl.Map('container', {
                    center: [55.31878, 25.23584],
                    zoom: 13,
                    key: 'Your API access key',
                });
            }
        </script>
        <script src="https://mapgl.2gis.com/api/js/v1?callback=initMap" async defer></script>
    </body>
</html>