Introduction

@arenarium/maps is a high performance TypeScript library designed for applications that need to display a large number of complex markers without cluttering the map. Its purpose is to simplify visualizing and searching map related information.
You must have an access token to use the library. To get a token, go to the sign in page and sign in to your account. Then go to the dashboard and create a new token.

Installation

The @arenarium/maps library for MapLibre has two packages: the main package and the MapLibre integration package. To install them with pnpm, run this command in your project directory:
bash
pnpm install @arenarium/maps @arenarium/maps-integration-maplibre

Map

Add a container element to your HTML. The map shows in this element.
app.html
<div id="map"></div>
Import MapLibrary from the main package. Import MaplibreProvider and the CSS file from the integration package.
To set up a map, create the provider with MaplibreProvider.create and load the library with MapLibrary.load. Then adapt the provider with library.provider.adapt and create the map manager with library.manager.create.
MaplibreProvider.create takes a maplibregl.MapOptions object with an optional theme: 'light' or 'dark'. The promise resolves when the map is loaded.
app.ts
import { MapLibrary } from '@arenarium/maps'; import { MaplibreProvider } from '@arenarium/maps-integration-maplibre'; import '@arenarium/maps-integration-maplibre/main.css'; // Create the MapLibre provider const provider = await MaplibreProvider.create({ container: 'map', // (Optional) The theme: 'light' or 'dark' theme: 'light', // Other MapLibre map options... }); // Load the library with your key const library = await MapLibrary.load('YOUR_ARENARIUM_MAPS_KEY'); // Adapt the provider and create the map manager on it const manager = await library.manager.create(await library.provider.adapt(provider));
To get the MapLibre map of the provider, use provider.getMap. You can then use the MapLibre API on the map directly.
To use the MapLibre classes on this map, for example bounds, markers and controls, import maplibregl from the integration package. Do not install maplibre-gl separately. The provider creates the map with this maplibregl. Thus, the map and the classes always have the same version.
app.ts
import { maplibregl } from '@arenarium/maps-integration-maplibre'; // Get the MapLibre map of the provider const map = provider.getMap(); // Use a MapLibre class on the map map.fitBounds(new maplibregl.LngLatBounds([-74.1, 40.6], [-73.8, 40.9]));
Map rotation and tilt are disabled. The library cannot place the markers correctly on a rotated or tilted map.

Markers

A marker has a pin, a tooltip and an optional popup:
  • The pin shows the location of the marker and basic data, for example an icon or a color.
  • The tooltip shows more data that the user must be able to read. This data can be small or large, as your application necessitates.
  • The popup shows when the user clicks the marker. Use it for data that is not necessary in the tooltip, or that is too large for the tooltip.
Each marker shows its pin or its tooltip. The marker shows its pin when there is not sufficient space for its tooltip. When the user zooms in, more space becomes available and more tooltips show. The rank of the marker primarily controls the sequence: a marker with a higher rank shows its tooltip earlier.
To add markers to the map, make an array of MapMarkerOptions objects. Each object has a unique id, the coordinates and the HTMLElement of the tooltip. Then give the array to the insertMarkers method of the map manager.
app.ts
import type { MapMarkerOptions } from '@arenarium/maps'; // Make 100 markers with random coordinates, a rank and a tooltip element const markers = new Array<MapMarkerOptions>(); for (let i = 0; i < 100; i++) { const id = i.toString(); markers.push({ id: id, lat: 39.8283 + (Math.random() - 0.5) * 10, lng: -98.5795 + (Math.random() - 0.5) * 10, rank: i + 1, tooltip: { element: getTooltipElement(id) } }); } // Insert the markers on the map await manager.insertMarkers(markers);
If a marker with the same id is already on the map, insertMarkers keeps it. Thus, you can use the same method to update the markers continuously. Insertions and removals run in the sequence of the calls. If you make a call while a different call runs, the new call waits until the first call is complete.
A marker can also have a pin element and a popup element. Each element is an HTMLElement with these options:
  • dimensions: the size of the element.
  • style: the appearance of the element.
  • initialize: a callback that initializes the element when it shows for the first time.
If you do not set the dimensions, the library measures each element. When you have many complex elements, set the dimensions manually to skip the measurement and make the markers load faster.
When markers overlap, the library compares the optional layer first and then the rank. This example sets all the options:
app.ts
import { MapMarkerOrientation, type MapMarkerOptions } from '@arenarium/maps'; // Make 100 markers with random coordinates, a rank, a layer, an orientation, // and a tooltip, a pin and a popup with all their options const markers = new Array<MapMarkerOptions>(); for (let i = 0; i < 100; i++) { const id = i.toString(); markers.push({ id: id, lat: 39.8283 + (Math.random() - 0.5) * 10, lng: -98.5795 + (Math.random() - 0.5) * 10, rank: i + 1, layer: 0, orientation: MapMarkerOrientation.Dynamic, tooltip: { element: getTooltipElement(id), dimensions: { size: { height: 32 + (i % 16), width: 64 + (i % 16) }, padding: 6, margin: 1 }, style: { background: '#ffffff', filter: 'drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3))', radius: 8 }, initialize: async (id, element) => {} }, pin: { element: getPinElement(), dimensions: { radius: 8, stroke: 1 }, style: { background: '#8b9483', stroke: '#000000', shadow: '0px 1px 2px rgba(0, 0, 0, 0.3)' }, initialize: async (id, element) => {} }, popup: { element: getPopupElement(id), dimensions: { size: { height: 96 + (i % 16), width: 128 + (i % 16) }, padding: 6 }, style: { background: '#ffffff', filter: 'drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3))', radius: 8 }, initialize: async (id, element) => {} } }); } // Insert the markers on the map await manager.insertMarkers(markers);
The functions below make the tooltip, pin and popup elements of the demo on this page. You can make the elements manually. You can also use a frontend library that can create and change DOM elements, for example React, Vue or Svelte.
app.ts
const getTooltipElement = (id: string) => { const element = document.createElement('div'); element.style = 'width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; color: #000000; font-size: 12px; font-family: var(--default-font-family);'; element.innerHTML = 'Tooltip ' + id; return element; }; const getPinElement = () => { const element = document.createElement('div'); element.style = 'width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; border-radius: 50%;'; return element; }; const getPopupElement = (id: string) => { const element = document.createElement('div'); element.style = 'width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; color: #000000; font-size: 12px; font-family: var(--default-font-family);'; element.innerHTML = 'Popup ' + id; return element; };
To remove markers from the map, give their ids to the removeMarkers method. To remove all the markers, use the clearMarkers method. This method also removes the markers of the insertions that you called before it.
app.ts
// Remove the markers with these ids from the map await manager.removeMarkers(ids); // Remove all the markers from the map await manager.clearMarkers();
To move the map to a marker, use the showTooltip method. The map moves to the marker and zooms in until the tooltip of the marker shows. To open or close the popup of a marker, use the showPopup and hidePopup methods.
app.ts
// Move the map to the marker with this id and show its tooltip manager.showTooltip(id); // Open the popup of the marker with this id manager.showPopup(id); // Close the open popup manager.hidePopup();
The marker options must obey these rules:
  • Each marker must have a unique id and unique coordinates.
  • The padding of the tooltip and the popup must be small in relation to their size.
  • The pin stroke must not be larger than the pin radius.

Events

The map manager and the library send events. To add a listener, use on. To remove the listener, use off with the same listener function.
The map manager sends events when the user interacts with the map and when the map changes. Each event gives a value to its listeners. Use these events to keep the page synchronized with the map. For example:
  • Load the markers of the visible area when the map stops.
  • Update a list of places while the map moves.
  • Open a panel with the data of a marker when the user clicks its tooltip.
The 'error' event reports the errors that occur while the manager responds to the map, outside of your calls. You cannot catch these errors in your code. If the cleared value is true, the manager removed all the markers to recover. You must then insert the markers again.
app.ts
// Sent when the user clicks the map, after the popups close manager.on('map_click', () => {}); // Sent when the map stops after a move, with the bounds of the visible area manager.on('map_idle', ({ bounds }) => {}); // Sent while the map moves, with the bounds of the visible area manager.on('map_move', ({ bounds }) => {}); // Sent when the user clicks the tooltip of a marker, with the id of the marker, after its popup opens (if it has a popup) manager.on('marker_click', ({ id }) => {}); // Sent on an error outside of a call; if cleared is true, the markers were removed and you must insert them again manager.on('error', ({ error, cleared }) => {});
The library sends events about its connection to the API. These events give no value to their listeners.
  • The 'redeploy' event occurs one time, when the API starts to serve a newer version of the library than the version that the page loaded. To use the new version, load the library again and create the map manager again. Alternatively, reload the page.
  • The 'offline' event occurs when a request to the API fails because of a network error, for example when the device loses its connection. Use it to tell the user that the markers cannot update.
app.ts
// Sent one time, when the API serves a newer version of the library: load the library and create the map manager again, or reload the page library.on('redeploy', () => {}); // Sent when a request to the API fails because of a network error library.on('offline', () => {});

Advanced

To customize the behavior of the map and the markers, give a MapManagerConfiguration object to library.manager.create. The configuration controls the pin fade, the popup pan and how the markers move to make space for each other.
app.ts
import type { MapManagerConfiguration } from '@arenarium/maps'; // Make a configuration object for the map manager const configuration: MapManagerConfiguration = { // (Optional) The pin configuration pin: { // How much the pins fade for each zoom level fadeout: { scale: 0.1, color: 0.2 }, // The zoom levels below the marker threshold in which the pin circle (container) and the pin element (content) show depth: { container: 3, content: 0.5 } }, // (Optional) The popup configuration popup: { // Pan the map to show the popup when it opens, with this padding from the map edges pan: { enabled: true, padding: { top: 16, bottom: 16, left: 16, right: 16 } } }, // (Optional) The marker placement configuration states: { // How freely the markers turn away from each other: 'low', 'medium' (default) or 'high' movement: 'medium', // What selects the marker that collapses in an overlap: 'rank', 'balanced' (default) or 'layout' priority: 'balanced' } }; // Create the map manager with the configuration const manager = await library.manager.create(await library.provider.adapt(provider), configuration);
The library loads the map manager from the API the first time that you call library.manager.create. To load the map manager together with the library, give the preload option to MapLibrary.load.
app.ts
// Load the library and the map manager together const library = await MapLibrary.load('YOUR_ARENARIUM_MAPS_KEY', { preload: { manager: true } });
When you do not need the map anymore, dispose the map manager. This removes its markers and releases all that it holds on the map. The map manager does not dispose the provider. Dispose the provider separately.
app.ts
// Dispose the map manager (you cannot use it after this) manager.dispose(); // Dispose the provider and its map provider.dispose();

Examples

To start a new project quickly, use one of these starter examples. Each example is a minimal application that shows a map with markers. The examples do not include access tokens, create your own token and the keys of your map provider before you run an example.