Create and delete the map
vanilla.html
common.css
common.ts
react.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://unpkg.com/@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="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
import {LOCATION} from './common';
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},
// Add a map scheme layer
[new MMapDefaultSchemeLayer({})]
);
}
mappable.ready.then(() => {
const toggleBtn: HTMLButtonElement = document.querySelector('.toggle-btn');
toggleBtn.addEventListener('click', () => {
toggleBtn.classList.toggle('removed');
toggleBtn.innerText = toggleBtn.classList.contains('removed') ? 'Create Map' : 'Delete Map';
// To delete the map, use map.destroy() and reset the variable
if (map) {
map.destroy();
map = null;
} else {
main();
}
});
});
</script>
<!-- prettier-ignore -->
<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" />
</head>
<body>
<div id="app"></div>
<div class="menu">
<button class="toggle-btn">Delete Map</button>
</div>
</body>
</html>
.menu {
position: absolute;
z-index: 1000;
bottom: 0;
left: 50%;
transform: translateX(-50%);
padding: 20px;
display: flex;
justify-content: center;
gap: 15px;
}
.toggle-btn {
border: none;
cursor: pointer;
padding: 10px 15px;
color: rgb(255, 255, 255);
font-size: 16px;
background-color: rgba(253, 100, 102, 0.9);
border-radius: 10px;
transition: background-color 0.2s;
}
.toggle-btn:hover {
background-color: rgb(253, 80, 80);
}
.toggle-btn:active {
background-color: rgb(253, 100, 102);
}
.removed {
background-color: rgba(0, 122, 252, 0.9);
}
.removed:hover {
background-color: rgb(0, 110, 252);
}
.removed:active {
background-color: rgb(0, 122, 252);
}
import type {MMapLocationRequest} from '@mappable-world/mappable-types';
export const LOCATION: MMapLocationRequest = {
center: [55.44279, 25.24613], // starting position [lng, lat]
zoom: 9 // starting zoom
};
<!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://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<script crossorigin src="https://unpkg.com/@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="react, typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
import {LOCATION} from './common';
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} ref={(x) => (map = x)}>
{/* Add a map scheme layer */}
<MMapDefaultSchemeLayer />
</MMap>
)}
<div className="menu">
<button className={`toggle-btn ${!showMap ? 'removed' : ''}`} onClick={() => toggleShowMap(!showMap)}>
{showMap ? 'Delete Map' : 'Create Map'}
</button>
</div>
</React.Fragment>
);
}
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('app')
);
}
</script>
<!-- prettier-ignore -->
<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" />
</head>
<body>
<div id="app"></div>
</body>
</html>