Skip to Content
ReferenceJavaScript API

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.

Embed code
<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:

AttributeDescription
data-json *URL or path to the map data JSON. Required.
data-settingsJSON string of setting overrides, merged over the map’s own settings.
data-locationId of a location to open on load. The ?location= URL parameter takes precedence.
data-fixedfromFixed wayfinding origin (location id).
id / classStandard 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.

mapReady event listener
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.

Reading, calling and subscribing
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:

PropertyTypeDescription
locationstring | undefinedId of the currently open location.
layerstringId of the active layer.
langstringActive language code.
theme'light' | 'dark'Active color theme.
fromstring | nullWayfinding origin location id.
tostring | nullWayfinding destination location id.
pos{ x, y, scale }Current viewport position — x/y are normalized 0–1, scale is the zoom level.
hoveredstring | falseId of the hovered location, or false.
directoryClosedbooleanWhether the directory is collapsed.
filtersOpenedbooleanWhether the filters panel is open.
routesOpenedbooleanWhether the wayfinding panel is open.
dataLoadedbooleanWhether the map data (including CSV) has finished loading.
dataMapDataThe full map data object.

Methods

Locations

MethodDescription
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

MethodDescription
switchLayer(id)Switch to the layer with the given id.

Language and theme

MethodDescription
setLang(code)Switch language. The code must exist in data.languages.
setTheme(theme)Set the color theme: 'light' or 'dark'.

Wayfinding

MethodDescription
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

MethodDescription
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

MethodDescription
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

MethodDescription
setPos({ x, y, scale })Set the viewport position directly.
setTarget({ x?, y?, scale? })Animate the viewport toward a target position.

Demo



Last updated on