Building a custom view
A custom view is an HTML template served by the view API. It loads the libraries and a locale, and builds its page around mapviews. Start from this page, or copy _default.html when the view needs the full map application.
Example
A map with the locale from the locale URL hook, its plugins and layers.
html
<!doctype html>
<html lang="{{language}}">
<head data-dir="{{dir}}">
<title>{{title}}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- OpenLayers must be loaded before MAPP. -->
<script src="https://cdn.jsdelivr.net/npm/ol@v10.8.0/dist/ol.js"
integrity="sha384-MW7s77hf0z6FJ3pLxOJ3STIrnMERs4FRQEDpqDrHpJX1mG0qr5Zd9glUb5XC4dA0"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="{{dir}}/public/css/mapp.css">
<link rel="stylesheet" href="{{dir}}/public/css/ui.css">
<script type="module" src="{{dir}}/public/js/lib/mapp.js"></script>
<script type="module" src="{{dir}}/public/js/lib/ui.js"></script>
<style>
html, body { height: 100%; margin: 0; }
#Map { position: relative; height: 100%; }
#OL { position: absolute; inset: 0; }
</style>
</head>
<body>
<div id="Map">
<div id="OL"></div>
<div id="mapButton" class="btn-column"></div>
</div>
<script type="module">
mapp.language = mapp.hooks.current.language || 'en';
if (mapp.language !== 'en') await mapp.utils.loadDictionary(mapp.language);
const key = mapp.hooks.current.locale || 'locale';
const locale = await mapp.utils.xhr(
`${mapp.host}/api/workspace/locale?locale=${key}&layers=true`);
if (locale instanceof Error) throw locale;
// Plugins are only loaded and run if syncPlugins is not empty.
locale.syncPlugins ??= ['zoomBtn'];
const mapview = await mapp.Mapview({
target: document.getElementById('OL'),
locale,
hooks: true,
loadPlugins: true,
scrollWheelZoom: true,
mapButton: document.getElementById('mapButton'),
attribution: { target: document.getElementById('Map') },
});
await mapview.addLayer(locale.layers);
mapview.interactions.highlight();
</script>
</body>
</html>Add it to the workspace templates and open it at ${DIR}/view/my_view:
json
{ "templates": { "my_view": { "src": "file:/public/views/my_view.html" } } }src also takes https: and ${NAME} placeholders for SRC_NAME variables; see templates. A template named default_view replaces the default view.
Steps
- Head.
data-dir=""becomesmapp.host, the prefix of every API request.[1] Use/public/…for the library files. Loadui.jsaftermapp.js; it addsmapp.uiand the ui plugins.[2] Keep the OpenLayers version andintegrityhash in step with_default.html; MAPP warns about an older OpenLayers. - Script. Use a module script after the body. Module scripts run in order after parsing, so
mappandmapp.uiexist and top levelawaitworks. - User. Plugins such as
loginandadminreadmapp.user. Set it from the response of/api/user/cookie, as the default view does; the response is anErrorwithout an ACL. - Locale.
layers=truereturnslocale.layersas an array of composed layers the user may see.[3] Without itlocale.layersis an object of uncomposed layers by key. Load single layers from/api/workspace/layer?locale=…&layer=…. - Mapview.
mapp.Mapviewneedslocaleandtarget. It returns a promise when plugins or SVG templates must load, so alwaysawaitit.view: { lat, lng, z }overrideslocale.view; URL hooks override both.hooks: truewrites the map state to the URL.[4] See locale view. - Layers.
addLayerdecorates clones of the JSON layers. Usemapview.layers[key]afterwards, not the objects passed in.[5] - Interactions. No interaction runs until the view starts one, eg.
mapview.interactions.highlight()to select locations by click. - Attribution.
attribution.targetis the container. The mapview adds the.map-attributionand.attribution-linkselements itself, withlinksand theattributionof displayed layers.[6]
Pitfalls
mapp.Mapviewonly loads and runs plugins whenlocale.syncPluginsis not empty or the locale hassvgTemplates. The default view setssyncPlugins; a custom view must too.loadPlugins: trueimports thelocale.pluginsURLs. Ui plugin buttons need themapButtonelement. The map target needs a height.- The globals are
mappandui, in lower case. - After creation, change the view with
mapview.fitView(extent)ormapview.Map.getView(), inEPSG:3857unlesslocale.sriddiffers.