Create and delete the map

Open in CodeSandbox

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
        <!-- To make the map appear, you must add your apikey -->
        <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="../variables.ts"
        ></script>
        <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
            import {LOCATION} from '../variables';
            
            window.map = null;
            
            main();
            async function main() {
                // Waiting for all api elements to be loaded
                await mappable.ready;
                const {MMap, MMapDefaultSchemeLayer} = mappable;
                // Initialize the map
                map = new MMap(
                    // Pass the link to the HTMLElement of the container
                    document.getElementById('app'),
                    // Pass the map initialization parameters
                    {location: LOCATION, showScaleInCopyrights: true},
                    // Add a map scheme layer
                    [new MMapDefaultSchemeLayer({})]
                );
            }
            
            mappable.ready.then(() => {
                const toggleBtn: HTMLButtonElement = document.querySelector('.toggle-btn');
                const emptyFrame: HTMLButtonElement = document.querySelector('.empty_frame');
                toggleBtn.addEventListener('click', () => {
                    toggleBtn.classList.toggle('removed');
                    toggleBtn.innerText = toggleBtn.classList.contains('removed') ? 'Create Map' : 'Delete Map';
                    emptyFrame.classList.toggle('hide');
            
                    // To delete the map, use map.destroy() and reset the variable
                    if (map) {
                        map.destroy();
                        map = null;
                    } else {
                        main();
                    }
                });
            });
        </script>

        <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
        <link rel="stylesheet" href="./common.css" />
        <link rel="stylesheet" href="../variables.css" />
    </head>
    <body>
        <div id="app">
            <div class="empty_frame hide"></div>
        </div>
        <div class="menu">
            <button class="toggle-btn">Delete Map</button>
        </div>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <script crossorigin src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
        <!-- To make the map appear, you must add your apikey -->
        <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="../variables.ts"
        ></script>
        <script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
            import {LOCATION} from '../variables';
            
            window.map = null;
            
            main();
            async function main() {
                // For each object in the JS API, there is a React counterpart
                // To use the React version of the API, include the module @mappable-world/mappable-reactify
                const [mappableReact] = await Promise.all([mappable.import('@mappable-world/mappable-reactify'), mappable.ready]);
                const reactify = mappableReact.reactify.bindTo(React, ReactDOM);
                const {MMap, MMapDefaultSchemeLayer} = reactify.module(mappable);
            
                function App() {
                    const [showMap, toggleShowMap] = React.useState(true);
            
                    return (
                        <React.Fragment>
                            {/* Create or delete the map using conditional rendering */}
                            {showMap && (
                                // Initialize the map and pass initialization parameters
                                <MMap location={LOCATION} showScaleInCopyrights={true} ref={(x) => (map = x)}>
                                    {/* Add a map scheme layer */}
                                    <MMapDefaultSchemeLayer />
                                </MMap>
                            )}
                            {!showMap && <div className="empty_frame" />}
                            <div className="menu">
                                <button className="toggle-btn" onClick={() => toggleShowMap(!showMap)}>
                                    {showMap ? 'Delete Map' : 'Create Map'}
                                </button>
                            </div>
                        </React.Fragment>
                    );
                }
            
                ReactDOM.render(
                    <React.StrictMode>
                        <App />
                    </React.StrictMode>,
                    document.getElementById('app')
                );
            }
        </script>

        <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
        <link rel="stylesheet" href="./common.css" />
        <link rel="stylesheet" href="../variables.css" />
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <script crossorigin src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
        <!-- To make the map appear, you must add your apikey -->
        <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="../variables.ts"
        ></script>
        <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
            import {LOCATION} from '../variables';
            
            window.map = null;
            
            async function main() {
                // For each object in the JS API, there is a Vue counterpart
                // To use the Vue version of the API, include the module @mappable-world/mappable-vuefy
                const [mappableVue] = await Promise.all([mappable.import('@mappable-world/mappable-vuefy'), mappable.ready]);
                const vuefy = mappableVue.vuefy.bindTo(Vue);
                const {MMap, MMapDefaultSchemeLayer} = vuefy.module(mappable);
            
                const app = Vue.createApp({
                    components: {MMap, MMapDefaultSchemeLayer},
                    setup() {
                        const refMap = (ref) => {
                            window.map = ref?.entity;
                        };
                        const showMap = Vue.ref(true);
                        const toggleShowMap = () => {
                            showMap.value = !showMap.value;
                        };
            
                        return {LOCATION, refMap, showMap, toggleShowMap};
                    },
                    template: `
                        <MMap :location="LOCATION" :showScaleInCopyrights="true" :ref="refMap" v-if="showMap">
                            <MMapDefaultSchemeLayer />
                        </MMap>
                        <div v-if="!showMap" class="empty_frame"/>
                        <div class="menu">
                            <button class="toggle-btn" :class="{removed: !showMap}" @click="toggleShowMap">
                                {{showMap ? 'Delete Map' : 'Create Map'}}
                            </button>
                        </div>`
                });
                app.mount('#app');
            }
            main();
        </script>

        <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
        <link rel="stylesheet" href="./common.css" />
        <link rel="stylesheet" href="../variables.css" />
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>
import type {MMapLocationRequest} from '@mappable-world/mappable-types';

export const LOCATION: MMapLocationRequest = {
    center: [54.3769, 24.4868], // starting position [lng, lat]
    zoom: 13.3 // starting zoom
};
:root {
    --linear-gradient-color-start: rgba(238, 253, 125, 0.4);
    --linear-gradient-color-end: rgba(238, 253, 125, 0);
}
.menu {
    position: absolute;
    z-index: 1000;
    top: 0;
    right: 0;

    display: flex;
    justify-content: center;

    padding: 20px;
    gap: 15px;
}

.empty_frame {
    align-content: center;

    width: 100%;
    height: 100vh;

    background: linear-gradient(to bottom, var(--linear-gradient-color-start), var(--linear-gradient-color-end)), url('./empty-text.svg') repeat-x 50%;
}

.empty_frame.hide {
    display: none;
}

.toggle-btn {
    width: 154px;
    padding: 12px 15px;

    font-size: 14px;
    font-weight: 500;
    cursor: pointer;

    color: #34374a;
    border: none;
    border-radius: 12px;
    background-color: white;
    box-shadow: 0 2px 4px 0 #5f698333;
}