Vector data · Angular, React and OpenLayers

Use a MapboxVectorLayer with a local style

Mount a standalone MapboxVectorLayer from a local Mapbox GL v8 style URL in OpenLayers to render municipality vector tiles alongside layers.

PRG municipality boundaries around Kraków drawn by a MapboxVectorLayer using a local style document.
A screenshot of the example. Municipal boundaries: Państwowy Rejestr Granic © GUGiK.

What this example shows

PRG municipal boundaries around Kraków render through ol-mapbox-style’s dedicated MapboxVectorLayer class pointed at a local GL v8 style URL—behaving as a single composable layer inside an Angular or React OpenLayers map.

When you need it

Unlike standalone basemap viewers where a Mapbox GL document owns the entire map, enterprise GIS apps usually manage their own OpenLayers View, controls and thematic WMS/GeoJSON layers—needing a GL-styled vector tileset to participate as just one layer in the stack.

MapboxVectorLayer encapsulates source creation and GL v8 styling inside a single Layer instance whose opacity, visibility and z-index your application controls like any other OpenLayers layer.

The code

The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.

mapbox-vector-layer.html
<aol-map height="480px" ariaLabel="PRG municipality vector layer around Kraków">
  <aol-interaction-default />
  <aol-control-defaults [attributionOptions]="attributionOptions" />
  <aol-view projection="EPSG:3857" [zoom]="10" [minZoom]="0" [maxZoom]="maxZoom">
    <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
  </aol-view>
</aol-map>

@if (styleError()) {
  <p role="status">The local map style could not be loaded.</p>
}
mapbox-vector-layer.scss
:host {
  display: block;
}
mapbox-vector-layer.ts
import { AfterViewInit, ChangeDetectionStrategy, Component, OnDestroy, signal, viewChild } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  MapComponent,
  ViewComponent,
} from 'cc-openlayers';
import { municipalityAttributionOptions, ownMunicipalityVectorLayer } from '../mapbox-style/mapbox-style.shared';
import { municipalityMaxZoom } from '../canvas-tiles-tms/canvas-tiles-tms.shared';

/** Uses ol-mapbox-style's MapboxVectorLayer with the local finite PRG source. */
@Component({
  selector: 'app-mapbox-vector-layer',
  imports: [CoordinateComponent, DefaultControlComponent, DefaultInteractionComponent, MapComponent, ViewComponent],
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: './mapbox-vector-layer.html',
  styleUrl: './mapbox-vector-layer.scss',
})
export class MapboxVectorLayerExample implements AfterViewInit, OnDestroy {
  protected readonly attributionOptions = municipalityAttributionOptions;
  protected readonly maxZoom = municipalityMaxZoom;
  protected readonly styleError = signal(false);
  private readonly map = viewChild.required(MapComponent);
  private releaseLayer?: () => void;

  ngAfterViewInit(): void {
    this.releaseLayer = ownMunicipalityVectorLayer(this.map().instance, () => this.styleError.set(true));
  }

  ngOnDestroy(): void {
    this.releaseLayer?.();
  }
}

Architecture and OpenLayers mechanics

Instantiating MapboxVectorLayer from ol-mapbox-style with a local style URL automatically reads the JSON style document, builds its internal vector-tile source targeting the local TMS 512-pixel PRG municipality pyramid, and renders the declared municipalities source layer while preserving required data attributions.

How it works

  • MapboxVectorLayer from ol-mapbox-style encapsulates both vector-tile source setup and GL v8 styling inside one composable OpenLayers layer.
  • Pointing the style URL at a local JSON asset requires no cloud API access token.
  • The style document references the municipalities MVT source layer and retains GUGiK PRG attribution metadata.

Pitfalls

  • Only proprietary mapbox:// protocol URLs require a Mapbox access token; standard relative or HTTPS style URLs work keyless against self-hosted tiles.
  • Ensure the style document’s tile URL scheme (such as south-origin TMS {-y} vs north-origin {y}) matches the served MVT files.

The same feature in the OpenLayers examples .

Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.