---
title: "JavaScript"
description: "Create a Gebeta map with @gebeta/js, pick a style, add controls, and use MapLibre underneath."
canonical_url: "http://0.0.0.0:6006/sdk/js"
markdown_url: "http://0.0.0.0:6006/sdk/js.md"
last_updated: "2026-11-17"
x_farming_labs_generated_preamble: true
---

# JavaScript
URL: /sdk/js
LLM index: /llms.txt
Description: Create a Gebeta map with @gebeta/js, pick a style, add controls, and use MapLibre underneath.

# JavaScript

`@gebeta/js` creates the map and gives you managers for directions, geocoding, clustering, geofencing and navigation. Using React? See [React](/sdk/react).

## Create a map

```javascript
import "@gebeta/js/style.css";
import { GebetaMaps } from "@gebeta/js";

const gebeta = new GebetaMaps({ auth: { accessToken, refreshToken } });

const map = gebeta.init({
  container: "map",
  center: [38.7578, 9.0192],
  zoom: 15,
});
```

Creating the map takes two steps. The constructor sets up auth and options; `init()` draws the map and returns the [MapLibre map](https://maplibre.org/maplibre-gl-js/docs/API/classes/Map/).

### Constructor options

| Option           | Type                            | Description                                                                                            |
| ---------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------ |
| `auth`           | `{ accessToken, refreshToken }` | Tokens from your backend. See [Authentication](/sdk/authentication).                                   |
| `apiKey`         | `string`                        | Your API key, instead of `auth`.                                                                       |
| `clustering`     | `object`                        | Turns on [marker clustering](/sdk/markers). It can only be set here.                                   |
| `enableClientId` | `boolean`                       | Sends a stable `X-Device-ID` header with every request, stored in `localStorage`. Defaults to `false`. |

### `init()` options

| Option                      | Type                    | Description                                                                   |
| --------------------------- | ----------------------- | ----------------------------------------------------------------------------- |
| `container`                 | `string \| HTMLElement` | The element's id, without `#`, or the element itself. Give it a height.       |
| `center`                    | `[lng, lat]`            | Where the map starts. Longitude first. Without it the map starts at `[0, 0]`. |
| `zoom`                      | `number`                | The starting zoom, from 0 to 22.                                              |
| `styleUrl`                  | `string`                | A style URL. Defaults to the standard style. See [Map styles](#map-styles).   |
| `style`                     | `object`                | A whole style object, instead of `styleUrl`.                                  |
| `navigationControl`         | `boolean`               | Shows zoom and compass buttons. Defaults to `false`.                          |
| `navigationControlPosition` | `string`                | `top-left`, `top-right` (the default), `bottom-left` or `bottom-right`.       |

Any other [MapLibre map option](https://maplibre.org/maplibre-gl-js/docs/API/type-aliases/MapOptions/), such as `pitch`, `bearing`, `minZoom` or `maxBounds`, is passed through to MapLibre.

## Wait for the map to load

The managers (`gebeta.fencing`, `gebeta.clustering`, `gebeta.geocodingManager`, directions and navigation) are created once the style has loaded. Before that, they throw a `PlatformError`, or return `null` for directions methods. Put your code in the map's `load` event:

```javascript
const map = gebeta.init({ container: "map", center: [38.7578, 9.0192], zoom: 13 });

map.on("load", async () => {
  const route = await gebeta.getDirections(
    { lat: 8.989022, lng: 38.79036 },
    { lat: 9.03045, lng: 38.7653 }
  );
  gebeta.displayRoute(route);
});
```

## Map styles

| Style     | URL                                                             |
| --------- | --------------------------------------------------------------- |
| Standard  | `https://tiles.gebeta.app/styles/standard/style.json`           |
| Satellite | `https://tiles.gebeta.app/styles/raster/raster.json`            |
| Terrain   | `https://tiles.gebeta.app/styles/standard/terrain/terrain.json` |

Pick one when you create the map:

```javascript
import { API } from "@gebeta/api";

gebeta.init({
  container: "map",
  styleUrl: API.Map.Constants.STYLES.satellite.url,
});
```

To switch later, call MapLibre's `setStyle()`. Requests keep their auth:

```javascript
map.setStyle(API.Map.Constants.STYLES.terrain.url);
```

[Try it](/examples/map-styles).

## Controls

Pass `navigationControl: true` to `init()`, or add the buttons later:

```javascript
gebeta.addNavigationControls("bottom-right");
```

Add scale and fullscreen controls:

```javascript
import { ScaleControl, FullscreenControl } from "@gebeta/js";

map.addControl(new ScaleControl(), "bottom-left");
map.addControl(new FullscreenControl(), "top-left");
```

With the CDN script, take them from `window.gebeta` instead: `const { ScaleControl, FullscreenControl } = window.gebeta;`. [Try it](/examples/navigation-controls).

## Add markers and popups

Use `gebeta.addMarker()` and `gebeta.addPopup()` once the map has loaded. Both take a `lngLat` and return the marker or popup they added.

```javascript
// Marker with a popup that opens on click
gebeta.addMarker({
  lngLat: [38.7613, 9.0106],
  anchor: "bottom",
  popup: "Meskel Square",
});

// Custom element
const el = document.createElement("div");
el.className = "pin";
gebeta.addMarker({ lngLat: [38.7578, 9.0192], element: el, anchor: "bottom" });

// Draggable
gebeta.addMarker({
  lngLat: [38.7578, 9.0192],
  draggable: true,
  onDragEnd: ({ lng, lat }) => console.log(lng, lat),
});

// Standalone popup
gebeta.addPopup({ lngLat: [38.7613, 9.0106], text: "Hello" });
```

### `addMarker` options

| Option      | Type                           | Description                                                   |
| ----------- | ------------------------------ | ------------------------------------------------------------- |
| `lngLat`    | `[lng, lat]` or `{lng, lat}`   | Position. **Required.**                                       |
| `element`   | `HTMLElement`                  | Custom element. Wins over `imageUrl` and `color`.             |
| `imageUrl`  | `string`                       | URL for the marker icon.                                      |
| `size`      | `[width, height]`              | Pixels when using `imageUrl` (default `[30, 30]`).            |
| `color`     | `string`                       | Pin color when using the default pin.                         |
| `anchor`    | `string`                       | Which part of the element is at `lngLat`. Default `'center'`. |
| `offset`    | `[x, y]`                       | Pixel offset.                                                 |
| `draggable` | `boolean`                      | Allow dragging.                                               |
| `rotation`  | `number`                       | Degrees.                                                      |
| `popup`     | `string \| HTMLElement`        | Popup opened on click. A string is shown as plain text.       |
| `onClick`   | `(marker, event) => void`      | Click handler.                                                |
| `onDragEnd` | `({lng, lat}, marker) => void` | Called after dragging stops.                                  |

### `addPopup` options

| Option        | Type                         | Description                         |
| ------------- | ---------------------------- | ----------------------------------- |
| `lngLat`      | `[lng, lat]` or `{lng, lat}` | Position. **Required.**             |
| `text`        | `string`                     | Plain text (safe for user data).    |
| `html`        | `string`                     | HTML markup (trusted only).         |
| `element`     | `HTMLElement`                | DOM element content.                |
| `closeButton` | `boolean`                    | Show close button (default `true`). |
| `anchor`      | `string`                     | Popup direction.                    |
| `offset`      | `number \| [number, number]` | Pixel offset.                       |
| `maxWidth`    | `string`                     | CSS max-width (default `'240px'`).  |

## Map building blocks

`@gebeta/js` exports the map building blocks, so app code only imports from Gebeta:

```javascript
import {
  ScaleControl,
  FullscreenControl,
  GeolocateControl,
  Marker,
  LngLatBounds,
} from "@gebeta/js";

// Controls
map.addControl(new ScaleControl(), "bottom-left");
map.addControl(new FullscreenControl(), "top-left");
map.addControl(new GeolocateControl(), "top-right");

// A marker you build yourself (gebeta.addMarker() covers most cases)
new Marker({ color: "#e63946" }).setLngLat([38.7613, 9.0106]).addTo(map);

// Fit the map to some points
const bounds = new LngLatBounds();
coordinates.forEach((c) => bounds.extend(c));
map.fitBounds(bounds, { padding: 64 });
```

With the CDN script they're on `window.gebeta`. Take what you need before you create your map, because the examples name the map instance `gebeta` too:

```javascript
const { Marker, LngLatBounds } = window.gebeta;
const gebeta = new GebetaMaps({ apiKey: "{your-api-key}" });
```

`window.maplibregl` is still available for backwards compatibility.

## Use MapLibre directly

`init()` returns the MapLibre map, and `gebeta.getMap()` returns it again later (or `null` before `init()`). Camera moves, events, sources and layers all work as they do in MapLibre:

```javascript
map.on("click", (event) => {
  console.log(event.lngLat);
});

map.flyTo({ center: [38.7613, 9.0106], zoom: 15 });
```

The [examples](/examples) include [flying to a place](/examples/fly-to), [tilting the map](/examples/tilt-and-rotate), [reading coordinates on click](/examples/click-coordinates) and [adding a GeoJSON layer](/examples/geojson-layer).

## Methods

| Member                                         | Description                                                                                                 |
| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| `init(options)`                                | Draws the map and returns the MapLibre map.                                                                 |
| `getMap()`                                     | The MapLibre map, or `null` before `init()`.                                                                |
| `addNavigationControls(position?)`             | Adds zoom and compass buttons.                                                                              |
| `getDirections(origin, destination, options?)` | Gets a route. See [Directions](/sdk/directions).                                                            |
| `displayRoute(route, options?)`                | Draws a route and fits the map to it.                                                                       |
| `updateRouteStyle(style)`                      | Changes the drawn route's color, width or opacity.                                                          |
| `clearRoute()`                                 | Removes the drawn route and its markers.                                                                    |
| `getCurrentRoute()`                            | The last route from `getDirections()`, or `null`.                                                           |
| `addMarker(options)`                           | Creates a marker, adds it to the map and returns it. See [Add markers and popups](#add-markers-and-popups). |
| `addPopup(options)`                            | Creates a popup, opens it on the map and returns it.                                                        |
| `geocodingManager`                             | Forward and reverse geocoding. See [Geocoding](/sdk/geocoding).                                             |
| `clustering`                                   | The clustering manager, or `null` if clustering is off. See [Markers](/sdk/markers).                        |
| `fencing`                                      | Draw and show geofences. See [Geofencing](/sdk/fencing).                                                    |
| `navigation`                                   | Turn-by-turn navigation. See [Navigation](/sdk/navigation).                                                 |

## Smaller bundles

The full package includes the map renderer. If you only need geocoding, import it from its own subpath and your bundler leaves the map out. It doesn't need a browser, so it also runs in Node:

```javascript
import { GeocodingManager, AuthManager } from "@gebeta/js/geocoding";
```

| Import                  | Exports                                                         |
| ----------------------- | --------------------------------------------------------------- |
| `@gebeta/js`            | `GebetaMaps`, `BrowserLocationProvider` and every manager class |
| `@gebeta/js/geocoding`  | `GeocodingManager`, `AuthManager`                               |
| `@gebeta/js/directions` | `DirectionsManager`                                             |
| `@gebeta/js/clustering` | `ClusteringManager`                                             |
| `@gebeta/js/fencing`    | `FenceManager`                                                  |
| `@gebeta/js/navigation` | `NavigationManager`, `BrowserLocationProvider`                  |

The directions, clustering, fencing and navigation classes draw on a map, so they take the map adapters from `gebeta.getPlatform()`. Most apps use the managers on a `GebetaMaps` instance instead.

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md).
Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).
