All integrations MapLibre Leaflet OpenLayers React Vue Svelte Angular React Native WordPress Webflow Squarespace Wix Shopify API docs

ExpressMaps in Angular

Two ways: a standalone component around MapLibre GL JS, or @maplibre/ngx-maplibre-gl if you prefer to build the map in templates. Both produce the map below, shown here in the Navigator style.

1. Install and serve the MapLibre worker

The examples are for current Angular (standalone components and signal inputs) and MapLibre GL JS 6.

npm install maplibre-gl

MapLibre 6 parses tiles in a web worker loaded from a separate file, which Angular's build does not copy for you. Add the worker and the file it imports to the build assets, and the MapLibre stylesheet to the global styles, in angular.json. Without the worker, the map stays blank.

"build": {
  "options": {
    "assets": [
      { "glob": "**/*", "input": "public" },
      { "glob": "maplibre-gl-worker.mjs", "input": "node_modules/maplibre-gl/dist", "output": "/" },
      { "glob": "maplibre-gl-shared.mjs", "input": "node_modules/maplibre-gl/dist", "output": "/" }
    ],
    "styles": ["node_modules/maplibre-gl/dist/maplibre-gl.css", "src/styles.css"]
  }
}

On MapLibre 5 or earlier only the stylesheet is needed; leave out the two worker entries and the setWorkerUrl call below.

Option 1: a standalone component

Create the map in afterNextRender, which only runs in the browser once the view exists, and remove it in ngOnDestroy. Replace YOUR_API_KEY with a key from the console, or read it from your environment file.

// express-map.component.ts
import { Component, ElementRef, OnDestroy, afterNextRender, effect, input, viewChild } from "@angular/core";
import { MapLibreMap, Marker, NavigationControl, Popup, setWorkerUrl } from "maplibre-gl";

const KEY = "YOUR_API_KEY";
const styleUrl = (id: string) => `https://tiles.expressmaps.com/styles/${id}.json?key=${KEY}`;

@Component({
  selector: "app-express-map",
  template: `<div #container class="map"></div>`,
  styles: `.map { width: 100%; height: 400px; }`
})
export class ExpressMapComponent implements OnDestroy {
  readonly mapStyle = input("standard");
  readonly center = input<[number, number]>([-0.1276, 51.5072]);
  readonly zoom = input(12);
  readonly popup = input("");

  private readonly container = viewChild.required<ElementRef<HTMLDivElement>>("container");
  private map?: MapLibreMap;
  private shown = "";

  constructor() {
    afterNextRender(() => {
      setWorkerUrl(new URL("maplibre-gl-worker.mjs", document.baseURI).href);
      this.shown = this.mapStyle();
      this.map = new MapLibreMap({
        container: this.container().nativeElement,
        style: styleUrl(this.shown),
        center: this.center(),
        zoom: this.zoom()
      });
      this.map.addControl(new NavigationControl(), "top-right");

      const marker = new Marker().setLngLat(this.center()).addTo(this.map);
      if (this.popup()) marker.setPopup(new Popup().setText(this.popup()));
    });

    // Follow changes to the mapStyle input
    effect(() => {
      const id = this.mapStyle();
      if (this.map && id !== this.shown) {
        this.map.setStyle(styleUrl(id));
        this.shown = id;
      }
    });
  }

  ngOnDestroy() {
    this.map?.remove();
  }
}

Add ExpressMapComponent to the imports of the component that uses it:

<app-express-map [center]="[-0.1276, 51.5072]" [zoom]="12" popup="Our office" />

The worker URL is resolved against document.baseURI, so it stays correct when the app is deployed under a sub-path with --base-href. MapLibreMap is MapLibre's alias for its Map class, which avoids shadowing JavaScript's built-in Map.

Option 2: ngx-maplibre-gl

ngx-maplibre-gl is the MapLibre project's Angular library: maps, markers, popups, sources, layers and controls as components in your templates. The current release targets Angular 22 and MapLibre GL JS 6; older Angular versions need an older release of the library. Keep the angular.json entries from step 1, then:

npm install @maplibre/ngx-maplibre-gl maplibre-gl

Provide the worker URL in your application config instead of calling setWorkerUrl yourself:

// app.config.ts
import { ApplicationConfig } from "@angular/core";
import { provideMaplibreWorker } from "@maplibre/ngx-maplibre-gl/config";

export const appConfig: ApplicationConfig = {
  providers: [provideMaplibreWorker("maplibre-gl-worker.mjs")]
};
// office-map.component.ts
import { Component } from "@angular/core";
import {
  ControlComponent, MapComponent, MarkerComponent, NavigationControlDirective, PopupComponent
} from "@maplibre/ngx-maplibre-gl";

@Component({
  selector: "app-office-map",
  imports: [MapComponent, MarkerComponent, PopupComponent, ControlComponent, NavigationControlDirective],
  template: `
    <mgl-map [mapStyle]="styleUrl" [center]="[-0.1276, 51.5072]" [zoom]="12">
      <mgl-control mglNavigation position="top-right" />
      <mgl-marker #office [lngLat]="[-0.1276, 51.5072]" />
      <mgl-popup [marker]="office">Our office</mgl-popup>
    </mgl-map>
  `,
  styles: `mgl-map { display: block; height: 400px; }`
})
export class OfficeMapComponent {
  styleUrl = "https://tiles.expressmaps.com/styles/standard.json?key=YOUR_API_KEY";
}

The popup opens when the marker is clicked.

Markers, popups and your own data

In option 1 you are writing ordinary MapLibre code once the map exists. Add GeoJSON layers after the map has loaded, inside the afterNextRender callback. MapLibre uses [lng, lat] order.

this.map.on("load", () => {
  const map = this.map!;
  map.addSource("stores", { type: "geojson", data: "/stores.geojson" });
  map.addLayer({
    id: "stores",
    type: "circle",
    source: "stores",
    paint: { "circle-radius": 6, "circle-color": "#1a73e8", "circle-stroke-width": 2, "circle-stroke-color": "#fff" }
  });
  map.on("click", "stores", (e) => {
    new Popup().setLngLat(e.lngLat).setText(e.features![0].properties["name"]).addTo(map);
  });
});

With ngx-maplibre-gl, the same thing is an <mgl-geojson-source> and an <mgl-layer> inside <mgl-map>.

Switching style

The option 1 component follows its mapStyle input, so binding it to a signal (for example [mapStyle]="dark() ? 'darklands' : 'standard'") tracks your app's theme. With ngx-maplibre-gl, bind [mapStyle] on <mgl-map> to the full style URL instead. Any of the ten styles work: standard, basic, enhanced, darklands, greyscale, navigator, subtle, autobahn, lithium and mulberry.

map.setStyle() replaces the whole style, including layers you added with addLayer. For a style picker that keeps your own sources and layers, add the StyleSwitcher control from the optional expressmaps package to the map in option 1:

npm install expressmaps maplibre-gl
import { StyleSwitcher } from "expressmaps";

// in afterNextRender, after creating the map
this.map.addControl(new StyleSwitcher({ key: KEY, styles: ["standard", "subtle", "darklands"] }), "top-left");

Add "node_modules/expressmaps/dist/expressmaps.css" to the styles array in angular.json for the picker's styling.

Server-side rendering

With Angular SSR or prerendering, the map must only be created in the browser. afterNextRender never runs on the server, so option 1 needs nothing more. For ngx-maplibre-gl, or to keep MapLibre out of the initial bundle, wrap the map in a @defer block, which renders only its placeholder on the server:

@defer (on viewport) {
  <app-office-map />
} @placeholder {
  <div style="height: 400px"></div>
}

Things to know

  • The key ends up in your client bundle, which is normal for web maps. Restrict it to your domains in the console; requests from other sites are refused.
  • One map load is counted each time the map is created. Switching style on an existing map does not count as another load, and panning and zooming are free. Destroying and recreating the component (for example in an @if block or on route changes) counts again.
  • The free plan includes 100,000 map loads a month.
  • Keep the attribution control on. The OpenStreetMap credit comes from the style and is required by the data licence.
  • If the map area is blank with a 404 for maplibre-gl-worker.mjs in the network tab, the asset entries in angular.json are missing or the dev server needs a restart.

New to MapLibre? The MapLibre GL JS guide covers the map API in more detail. Sign up for a free API key to get started.

Get a free API key MapLibre guide