Skip to content

JavaScript SDK ​

The JavaScript SDK gives you full programmatic control over an embedded Layota map.

Installation ​

bash
npm install @layota/map-sdk

Or use the CDN (UMD):

html
<script src="https://unpkg.com/@layota/map-sdk"></script>
<script>
  const map = new LayotaMap.LayotaMap({ ... })
</script>

Quick Start ​

typescript
import { LayotaMap } from '@layota/map-sdk'

const map = new LayotaMap({
  container: '#map',
  projectId: 'your-project-id',
})

await map.ready()

map.selectArea('coffee-shop')
   .focusOn('coffee-shop')

Configuration ​

typescript
const map = new LayotaMap({
  container: '#map',              // Required — CSS selector or HTMLElement
  projectId: 'proj-uuid',         // Required — published project UUID
  apiKey: 'sk_...',               // Required if project has allowed domains
  theme: 'dark',                  // 'light' | 'dark' | 'blue' | 'auto'
  language: 'en',                 // Language code
  levelId: 'floor-2',             // Start on this level (id, key, or name)
  areaId: 'cafe',                 // Select this area on load, card open (id, key, or title)
  markerId: 'exit-north',         // Select this marker on load, card open (id, key, or title)
  from: 'reception',              // Open with a route from here (id, key, or title, any level)
  to: 'pitch-74',                 // … to here. Only one of the two: the route builder opens with it
  ui: { search: 'hidden', zoomControls: 'hidden' },  // This embed's controls (see below)
  view: '2d',                     // Open flat ('2d') or in 3D ('3d') — for a project with 3D on
  analytics: 'off',               // Visits are not counted in analytics (staging, tests)
  baseUrl: 'https://app.layota.app',  // Override for self-hosted installations (this is the default)
})

from / to work like the embed's route link: with both, the route is drawn on load and routeBuilt fires; with one, the route builder opens with that end filled in.

ui sets this embed's search bar, zoom buttons, language button and Share button, whatever the project's Appearance says — like the web component's search / zoom-controls / language-switcher attributes. search: 'bar', 'button' or 'hidden'; zoomControls: 'visible' or 'hidden'; languageSwitcher: 'visible' or 'hidden' (the visitor's language button above the zoom buttons, shown only when the project has two or more languages). shareButton: 'visible' or 'hidden' — the Share button on a place's card and next to a built route. firstView: 'fit' or 'fill' — how this embed first opens, the whole plan or the plan filling the block (First view). Leave a key out for the project's setting. Hiding search keeps place cards and routes.

view picks the view the map opens in when the project has 3D on: '2d' the flat map, '3d' the 3D view; left out, such a map opens in 3D. map.setView('2d' | '3d' | null) switches later, without a reload (null = the project's own view).

analytics: 'off' keeps this map's visits out of the project's analytics — for a staging site, automated tests, your own checks. Your on() listeners still get every event. See Visits that are not counted.

Methods ​

Lifecycle ​

typescript
// Wait for the map to finish loading
await map.ready()

// Clean up the iframe and all event listeners
map.destroy()

When the map cannot load — it is not published, your page's domain is not in its Allowed Domains, the API key is not valid, too many requests, no answer — ready() rejects with a LayotaMapLoadError, and so does every request waiting for the map (getLevels(), getProject(), …); requests made after that reject at once. The visitor sees the map's own "Couldn't load the map" card.

typescript
import { LayotaMap, LayotaMapLoadError } from '@layota/map-sdk'

try {
  await map.ready()
} catch (err) {
  if (err instanceof LayotaMapLoadError) {
    // err.status: 401 key, 403 domain, 404 not published, 429 too many requests, 5xx;
    // none when no answer came. err.canRetry: the map's card offers Reload.
    console.warn('Map not loaded:', err.status, err.message)
  }
}

A request also gives up when the map has not loaded within 30 seconds, and at most the last 100 commands are kept for a map that is not ready yet.

Level Control ​

typescript
// Switch level by id, key (slug), or default name (case-insensitive)
map.selectLevel('floor-2')
map.selectLevel('Block 2 · Floor 1')

// Fetch all visible levels
const levels: Level[] = await map.getLevels()
// Level: { id: string, name: string }

Area & Marker Selection ​

typescript
// Select by id, key (slug), or title — opens the info card
map.selectArea('cafe')
map.selectArea('cafe', 'floor-1')   // specify level for disambiguation

map.selectMarker('exit-north')
map.selectMarker('exit-north', 'floor-1')

// Close the info card and deselect
map.clearSelection()
typescript
// Pan and zoom to an area or marker — switches level if needed
map.focusOn('cafe')

// Reset zoom and pan to default
map.resetView()

// Set zoom level (scale must be > 0)
map.setZoom(1.5)
typescript
// Open the search panel and filter results by query
map.search('coffee')

// Clear the search
map.search('')

Routing ​

typescript
// Each end by id, key or title — found on any level
map.buildRoute('entrance', 'cafe')

// Or exactly: the place's type and level
const from = { id: 'entrance', type: 'area', levelId: 'floor-1' }
const to   = { id: 'cafe',     type: 'area', levelId: 'floor-2' }
map.buildRoute(from, to)

// How it is travelled: 'walk', 'drive' or 'stepfree' (wheelchair, stroller).
// Without it the map's default route mode applies.
map.buildRoute('entrance', 'site-64', { mode: 'drive' })

// Remove the displayed route
map.clearRoute()

Language ​

typescript
// Get available languages — shipped ones and any custom language the project added (see Localization guide)
const { languages, current } = await map.getLanguages()
// Language: { code: string, name: string }
// current: currently active language code

// Switch language at runtime
map.setLanguage('de')

Legend filter ​

typescript
// Keep only objects carrying one of these tags — tag names (case-insensitive,
// any project language) or tag ids; everything else is dimmed and not
// interactive, search is limited too. Unknown values are skipped and
// reported as an `error` event.
map.setFilter(['comfort', 'huisdieren'])
map.setFilter(['TAG_ID_1', 'TAG_ID_2'])

// Show everything again
map.clearFilter()

map.on('filterChange', ({ tags }) => {
  // tags: [{ id, name }] currently active, names in the map's language
})

// Changing level keeps the active tags the new level has and switches off the
// others (a filterChange follows); with none left the filter is off.

Theme ​

typescript
// Switch map theme at runtime: 'light', 'dark', 'blue', or 'auto'
map.setTheme('dark')

Controls ​

typescript
// Show or hide this embed's search bar, zoom buttons and language button at runtime, without a reload
map.setUi({ search: 'bar', zoomControls: 'hidden', languageSwitcher: 'visible' })

// null puts a control back on the project's setting; a key left out stays as it is
map.setUi({ search: null })

Areas & Markers Data ​

typescript
// Get all areas across all levels
const allAreas: Area[] = await map.getAreas()

// Get areas for a specific level (by id, key, or name)
const floorAreas: Area[] = await map.getAreas('floor-1')

// Same for markers
const allMarkers: Marker[] = await map.getMarkers()
const floorMarkers: Marker[] = await map.getMarkers('floor-1')
// Area: { id, title, levelId, key }
// Marker: { id, title, levelId, key }

Project Data ​

typescript
const project: ProjectInfo = await map.getProject()
// ProjectInfo: { id, settings, levels: ProjectLevel[] }
// ProjectLevel: { id, name, areas: Area[], markers: Marker[] }

Method Chaining ​

All commands return this for chaining:

typescript
map.selectLevel('floor-1')
   .selectArea('cafe')
   .focusOn('cafe')
   .setZoom(1.5)
   .search('cafe')

Events ​

Subscribe ​

typescript
map.on('areaClick', (area) => {
  console.log('Clicked:', area.title)
})

map.on('levelChange', (level) => {
  // level.name = the default-language name (the identity); level.displayName = in the map's language
  console.log('Switched to:', level.displayName ?? level.name)
})

Subscribe Once ​

typescript
map.once('ready', () => {
  console.log('Map loaded')
})

Unsubscribe ​

typescript
const handler = (area) => console.log(area)
map.on('areaClick', handler)
map.off('areaClick', handler)

Event Reference ​

EventPayloadDescription
ready—Map fully loaded
levelChangeLevelUser or SDK switched levels
areaClickAreaUser clicked an area
markerClickMarkerUser clicked a marker
areaSelectAreaArea selected programmatically
markerSelectMarkerMarker selected programmatically
routeBuilt{ route: RouteResult, levelId: string }Route calculated
routeNotFound—No route found
languageChange{ language: string }Language switched — via setLanguage() or by the visitor's language button
shareShareEvent — { kind: 'place', placeType, id, url, method } or { kind: 'route', from, to, mode?, url, method }The visitor shared a place or a route with the map's Share button. method: native, clipboard, manual or qr. With the SDK the link is the map's own address
filterChange{ tags: { id, name }[] }Legend filter changed — by the visitor, setFilter() or a ?tags= deep link
errorstringSomething the map could not apply (an unknown value, a place it cannot find) — or the map could not load at all; then ready() rejects as well (see Lifecycle)

TypeScript Types ​

All types are exported from the package:

typescript
import type {
  LayotaMapOptions,
  EmbedUi,
  Level,
  Language,
  Area,
  Marker,
  RouteEntity,
  RoutePoint,
  RouteSegment,
  RouteResult,
  ProjectLevel,
  ProjectInfo,
  SdkEventMap,
} from '@layota/map-sdk'

Architecture ​

The SDK creates a hidden <iframe> pointing to https://app.layota.app/embed/{projectId} and communicates with it via window.postMessage. Commands sent before the map is ready are queued and flushed automatically once the READY event fires — or dropped, with their requests rejected, when the map reports that it could not load.

Layota Documentation