ngx-maplibre-builder
v0.2.0
Published
A flexible Angular library for building MapLibre GL maps with a simple builder pattern API
Maintainers
Readme
ngx-maplibre-builder
A flexible Angular library for building MapLibre GL maps with a simple and intuitive builder pattern API.
Features
- 🏗️ Builder Pattern API - Clean and chainable API for map configuration
- 🎯 Type-Safe - Full TypeScript support with comprehensive type definitions
- 🎨 Flexible - Support for custom styles, controls, layers, and sources
- 📦 Lightweight - Minimal dependencies and tree-shakeable
- 🔧 Easy Integration - Simple setup with Angular standalone components
- 🗺️ Full MapLibre Support - Access to all MapLibre GL features
Installation
npm install ngx-maplibre-builder maplibre-gl@^6.16.0Quick Start
1. Import the Component
import { Component, OnInit, signal } from '@angular/core';
import { NgxMaplibreBuilderComponent, NgxMaplibreBuilderApiBuilder, NgxMaplibreBuilderApi } from 'ngx-maplibre-builder';
import * as maplibregl from 'maplibre-gl';
@Component({
selector: 'app-root',
standalone: true,
imports: [NgxMaplibreBuilderComponent],
template: `
<ngx-maplibre-builder [config]="mapConfig()" style="display: block; height: 400px" />
`
})
export class AppComponent implements OnInit {
mapConfig = signal<NgxMaplibreBuilderApi | null>(null);
ngOnInit(): void {
const builder = new NgxMaplibreBuilderApiBuilder();
const config = builder
.setOptions({
center: [51.389, 35.6892], // Tehran coordinates
zoom: 10
})
.setStyles({
version: 8,
sources: {},
layers: []
})
.build();
this.mapConfig.set(config);
}
}2. Add MapLibre CSS
Add to your angular.json or import in your global styles:
{
"styles": [
"node_modules/maplibre-gl/dist/maplibre-gl.css"
]
}Or in your global CSS file:
@import 'maplibre-gl/dist/maplibre-gl.css';3. Configure the MapLibre worker (Angular CLI)
In your application's build assets in angular.json, add:
{
"glob": "maplibre-gl-worker.mjs",
"input": "node_modules/maplibre-gl/dist",
"output": "assets"
}Before bootstrapping your application in main.ts:
import {getVersion, setWorkerUrl} from 'maplibre-gl';
setWorkerUrl(new URL(`assets/maplibre-gl-worker.mjs?v=${getVersion()}`, document.baseURI).href);The worker must come from the same installed MapLibre version as the main bundle. For other bundlers, follow the MapLibre installation guide.
Initialization errors
The component emits (mapError) and displays an accessible error message when map initialization fails, including devices without WebGL2. WebGL1 fallback is unavailable in MapLibre 6.
Migrating event handlers from version 5
Nested GeoJSON properties remain objects; do not JSON.parse them. Missing style images must be supplied through map.setMissingStyleImageResolver, rather than an old styleimagemissing handler. Vector overscaling defaults also changed; consumers requiring version 5 behavior can set zoomLevelsToOverscale: undefined in their options.
Advanced Usage
Adding Controls
builder.setControls([
{
control: new maplibregl.NavigationControl(),
position: 'top-right'
},
{
control: new maplibregl.ScaleControl({ unit: 'metric' }),
position: 'bottom-right'
},
{
control: new maplibregl.FullscreenControl(),
position: 'top-right'
}
]);Adding Event Handlers
builder.setEventHandlers([
{
eventName: 'load',
handler: () => {
console.log('Map loaded!');
}
},
{
eventName: 'click',
handler: (event: maplibregl.MapMouseEvent) => {
console.log('Clicked at:', event.lngLat);
}
}
]);Adding Sources and Layers
builder
.setSources([
{
id: 'points',
source: {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [51.389, 35.6892]
},
properties: {
title: 'Tehran'
}
}
]
}
}
}
])
.setLayers([
{
layer: {
id: 'points-layer',
type: 'circle',
source: 'points',
paint: {
'circle-radius': 8,
'circle-color': '#007cbf'
}
}
}
]);API Reference
NgxMaplibreBuilderApiBuilder
Methods
setOptions(options: MapOptions): this- Set map initialization optionssetStyles(styles: StyleSpecification): this- Set map style configurationsetControls(controls: MapControl[]): this- Add map controlssetEventHandlers(handlers: MapEventHandler[]): this- Add event handlerssetSources(sources: MapSource[]): this- Add data sourcessetLayers(layers: MapLayer[]): this- Add map layersbuild(): NgxMaplibreBuilderApi- Build and return the final configuration
Types
interface MapOptions {
center?: [number, number];
zoom?: number;
pitch?: number;
bearing?: number;
// ... other MapLibre options
}
interface MapControl {
control: IControl;
position: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
}
interface MapEventHandler {
eventName: string;
handler: (event?: any) => void;
}
interface MapSource {
id: string;
source: SourceSpecification;
}
interface MapLayer {
layer: LayerSpecification;
}Requirements
- Angular
^17.3.0 - MapLibre GL
^6.16.0(ES modules only) - WebGL2-capable browser/device
- Node.js 22 or newer for installation/build tooling
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues and questions, please use the GitHub issue tracker.
