JavaScript API
Mapplic map data is stored as JSON, or optionally, location data can be stored as CSV. It’s therefore possible to dynamically generate and serve JSON or CSV data, establishing a connection between Mapplic and third-party applications.
Once embedded, a map can be controlled and observed at runtime through its store — the object that holds the map’s data and state and exposes the methods below.
Embedding the map
A map is embedded with the <mapplic-map> web component plus the Mapplic
script. Assign an id (or class) so the element can be targeted from JavaScript —
here the id is my-map.
<mapplic-map
id="my-map"
data-json="https://mapplic.com/getMapData?id=6AAA3HVY6u05TYjJvG42"
><script type="text/javascript" id="mapplic-script" src="https://mapplic.com/mapplic.js"></script></mapplic-map>The element is configured through data-* attributes:
| Attribute | Description |
|---|---|
data-json * | URL or path to the map data JSON. Required. |
data-settings | JSON string of setting overrides, merged over the map’s own settings. |
data-location | Id of a location to open on load. The ?location= URL parameter takes precedence. |
data-fixedfrom | Fixed wayfinding origin (location id). |
id / class | Standard HTML attributes; id is used to target the element. |
Using the store
mapReady event
Attach a mapReady event listener to the map element. It fires once the map has
initialized. To prevent errors, ensure all API operations happen after mapReady.
const map = document.getElementById('my-map');
map.addEventListener('mapReady', e => {
const store = map.store; // also available as e.detail.store
});Accessing the store
The store lets you read properties with getState(), call methods, or
subscribe to changes.
map.addEventListener('mapReady', e => {
const store = map.store;
// read state
console.log(store.getState().location);
// call a method
store.getState().openLocation('s125');
// react to layer changes
store.subscribe(state => state.layer, (layer, prev) => {
console.log(`Layer switched from ${prev} to ${layer}.`);
});
});Store properties
The most useful state fields:
| Property | Type | Description |
|---|---|---|
location | string | undefined | Id of the currently open location. |
layer | string | Id of the active layer. |
lang | string | Active language code. |
theme | 'light' | 'dark' | Active color theme. |
from | string | null | Wayfinding origin location id. |
to | string | null | Wayfinding destination location id. |
pos | { x, y, scale } | Current viewport position — x/y are normalized 0–1, scale is the zoom level. |
hovered | string | false | Id of the hovered location, or false. |
directoryClosed | boolean | Whether the directory is collapsed. |
filtersOpened | boolean | Whether the filters panel is open. |
routesOpened | boolean | Whether the wayfinding panel is open. |
dataLoaded | boolean | Whether the map data (including CSV) has finished loading. |
data | MapData | The full map data object. |
Methods
Locations
| Method | Description |
|---|---|
openLocation(id?, duration?) | Focus a location and animate to it. Omit id to reuse the focused one; duration sets the animation length in seconds. |
closeLocation(reset?) | Close the focused location. Pass true to also reset the view. |
updateLocation(id, changes) | Merge changes into the location with the given id. |
Layers
| Method | Description |
|---|---|
switchLayer(id) | Switch to the layer with the given id. |
Language and theme
| Method | Description |
|---|---|
setLang(code) | Switch language. The code must exist in data.languages. |
setTheme(theme) | Set the color theme: 'light' or 'dark'. |
Wayfinding
| Method | Description |
|---|---|
setFrom(id) | Set the route origin (or null to clear). |
setTo(id) | Set the route destination (or null to clear). |
setAny(id) | Set the destination if empty, otherwise the origin; opens the wayfinding panel. |
setFixedFrom(id) | Lock a fixed route origin (or null to clear). |
setRoutesOpened(value) | Show or hide the wayfinding panel. |
Filters
| Method | Description |
|---|---|
setSearchFilter(text) | Set the search query. |
setAlphabeticFilter(letter) | Filter locations by their initial letter. |
setGroupFilter(group, enable, single) | Toggle a group/tag filter. single keeps only one group active. |
setInstanceFilter(id, value) | Set the value of a custom filter by id. |
clearFilters() | Reset all filters. |
toggleFilters(turn?) | Show or hide the filters panel (toggles when turn is omitted). |
Interface
| Method | Description |
|---|---|
toggleDirectory(turn?) | Show or hide the directory (toggles when turn is omitted). |
setEnlargedMedia(index) | Open the popup media at index full-size, or false to close. |
setHovered(id) | Set the hovered location (false to clear). |
Position
| Method | Description |
|---|---|
setPos({ x, y, scale }) | Set the viewport position directly. |
setTarget({ x?, y?, scale? }) | Animate the viewport toward a target position. |