### Install ngx-maplibre-gl and maplibre-gl
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/README.md
Install the necessary packages using npm or yarn.
```bash
npm install @maplibre/ngx-maplibre-gl maplibre-gl
yarn add @maplibre/ngx-maplibre-gl maplibre-gl
```
--------------------------------
### Run ngx-maplibre-gl Showcase App
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/CONTRIBUTING.md
Starts the showcase application for ngx-maplibre-gl. This is useful for testing changes to the library.
```bash
npm run start
```
--------------------------------
### Install Project Dependencies
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/CONTRIBUTING.md
Installs project dependencies using npm ci. Ensure you have a package-lock.json or npm-shrinkwrap.json.
```bash
npm ci
```
--------------------------------
### Map Component with Controls
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/README.md
Import NgxMapLibreGLModule for using multiple map components. This example includes a navigation control.
```typescript
import { Component } from '@angular/core';
import { NgxMapLibreGLModule } from '@maplibre/ngx-maplibre-gl';
@Component({
template: `
`,
styles: [
`
mgl-map {
height: 100%;
width: 100%;
}
`,
],
imports: [NgxMapLibreGLModule]
})
export class AppComponent {}
```
--------------------------------
### Add Vector Tile Source to Map
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Use `mgl-vector-source` to add vector tile data. Configure with a URL or a tiles array. This example shows how to add a source and then a line layer to visualize country boundaries.
```typescript
import { Component, signal } from '@angular/core';
import {
MapComponent,
VectorSourceComponent,
LayerComponent
} from '@maplibre/ngx-maplibre-gl';
import type { LayerSpecification } from 'maplibre-gl';
@Component({
selector: 'app-vector-demo',
template: `
`,
imports: [MapComponent, VectorSourceComponent, LayerComponent]
})
export class VectorDemoComponent {
readonly layouts = signal>({
countries: { visibility: 'visible' }
});
toggleLayer(): void {
this.layouts.update(layouts => ({
...layouts,
countries: {
...layouts['countries'],
visibility: (layouts['countries'] as any)?.visibility === 'visible' ? 'none' : 'visible'
}
}));
}
}
```
--------------------------------
### Configure GeoJSONSourceComponent with Clustering
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
The mgl-geojson-source component handles GeoJSON data. Enable clustering and set parameters like clusterMaxZoom and clusterRadius for grouped markers. This example also shows how to style clustered and unclustered points using mgl-layer components.
```typescript
import { Component, signal } from '@angular/core';
import {
MapComponent,
GeoJSONSourceComponent,
LayerComponent
} from '@maplibre/ngx-maplibre-gl';
@Component({
selector: 'app-geojson-demo',
template: `
`,
imports: [MapComponent, GeoJSONSourceComponent, LayerComponent]
})
export class GeoJSONDemoComponent {
readonly earthquakeData = signal({
type: 'FeatureCollection',
features: [
{ type: 'Feature', geometry: { type: 'Point', coordinates: [-103.59, 40.66] }, properties: { mag: 4.5 } },
{ type: 'Feature', geometry: { type: 'Point', coordinates: [-104.59, 41.66] }, properties: { mag: 3.2 } }
]
});
}
```
--------------------------------
### Add Raster DEM Source for 3D Terrain
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Utilize `mgl-raster-dem-source` to incorporate terrain data for 3D rendering. This example configures a DEM source and a terrain control for interactive exaggeration.
```typescript
import { Component } from '@angular/core';
import {
MapComponent,
RasterDemSourceComponent,
ControlComponent,
TerrainControlDirective
} from '@maplibre/ngx-maplibre-gl';
import type { TerrainSpecification } from 'maplibre-gl';
@Component({
selector: 'app-terrain-demo',
template: `
`,
imports: [MapComponent, RasterDemSourceComponent, ControlComponent, TerrainControlDirective]
})
export class TerrainDemoComponent {
readonly terrainSpec: TerrainSpecification = {
source: 'terrainSource',
exaggeration: 1
};
}
```
--------------------------------
### Register Custom Map Image
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
The `mgl-image` component loads and registers images for use in symbol layers. Images can be loaded from URLs or provided as raw data. This example loads an image from a URL and uses it as an icon in a symbol layer.
```typescript
import { Component, signal } from '@angular/core';
import {
MapComponent,
ImageComponent,
GeoJSONSourceComponent,
LayerComponent
} from '@maplibre/ngx-maplibre-gl';
@Component({
selector: 'app-image-demo',
template: `
@if (imageLoaded()) {
}
`,
imports: [MapComponent, ImageComponent, GeoJSONSourceComponent, LayerComponent]
})
export class ImageDemoComponent {
readonly imageLoaded = signal(false);
readonly pointsData: GeoJSON.FeatureCollection = {
type: 'FeatureCollection',
features: [
{ type: 'Feature', geometry: { type: 'Point', coordinates: [-74.5, 40] }, properties: {} }
]
};
onImageError(error: { status: number }):
void {
console.error('Failed to load image:', error);
}
}
```
--------------------------------
### Initialize MapComponent with Basic Configuration
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Use the mgl-map component as the root for map elements. Configure map style, zoom, center, pitch, bearing, and interaction properties.
```typescript
import { Component } from '@angular/core';
import { MapComponent } from '@maplibre/ngx-maplibre-gl';
import type { Map } from 'maplibre-gl';
@Component({
selector: 'app-map-demo',
template: `
`,
styles: [`
mgl-map {
height: 100%;
width: 100%;
}
`],
imports: [MapComponent]
})
export class MapDemoComponent {
onMapLoad(map: Map): void {
console.log('Map loaded', map);
}
onMapClick(event: any): void {
console.log('Clicked at:', event.lngLat);
}
}
```
--------------------------------
### Generate API Documentation
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/CONTRIBUTING.md
Generates API documentation using typedoc. The documentation is placed in the API folder under dist/showcase.
```bash
npm run docs
```
--------------------------------
### Import Individual Components for Tree-Shaking
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Import individual components like MapComponent, ControlComponent, and NavigationControlDirective for better tree-shaking and a smaller bundle size. This is the recommended approach for standalone components.
```typescript
import { Component } from '@angular/core';
import {
MapComponent,
ControlComponent,
NavigationControlDirective
} from '@maplibre/ngx-maplibre-gl';
@Component({
selector: 'app-root',
template: `
`,
imports: [MapComponent, ControlComponent, NavigationControlDirective]
})
export class AppComponent {}
```
--------------------------------
### Add maplibre-gl CSS to global CSS
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/README.md
Alternatively, import the maplibre-gl CSS file in your global CSS file (e.g., styles.css).
```css
@import '~maplibre-gl/dist/maplibre-gl.css';
```
--------------------------------
### Import NgxMapLibreGLModule for NgModule Apps
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Import the NgxMapLibreGLModule to include all library components at once. This is suitable for NgModule-based Angular applications.
```typescript
import { NgModule } from '@angular/core';
import { NgxMapLibreGLModule } from '@maplibre/ngx-maplibre-gl';
@NgModule({
imports: [NgxMapLibreGLModule]
})
export class AppModule {}
```
--------------------------------
### Conditional Rendering and Iteration in Angular
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/projects/showcase/src/app/demo/demo-index.component.html
Demonstrates conditional rendering with @if and iteration with @for in Angular templates. Useful for dynamically displaying content based on application state or data.
```html
@if (searchTerm()) { close }
@for (cat of categories; track cat) {
### {{ cat }}
@for (route of searchedRoutes()[cat]; track route) { @if (route) { {{ route.data?.label }} } }
}
```
--------------------------------
### Configure tsconfig.json
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/README.md
Adjust tsconfig.json to disable strict null checks and skip library checks if necessary.
```json
"compilerOptions": {
...
"strictNullChecks": false,
"skipLibCheck": true,
}
```
--------------------------------
### HTML Markers for Clustered GeoJSON with mgl-markers-for-clusters
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Renders custom HTML markers for clustered GeoJSON data using `mgl-markers-for-clusters`. Provides `mglPoint` and `mglClusterPoint` directives for templating individual and cluster markers. Ensure relevant components and directives are imported.
```typescript
import { Component } from '@angular/core';
import {
MapComponent,
GeoJSONSourceComponent,
MarkersForClustersComponent,
PointDirective,
ClusterPointDirective
} from '@maplibre/ngx-maplibre-gl';
@Component({
selector: 'app-cluster-markers-demo',
template: `
{{ feature.properties?.point_count }}
`,
imports: [
MapComponent,
GeoJSONSourceComponent,
MarkersForClustersComponent,
PointDirective,
ClusterPointDirective
]
})
export class ClusterMarkersDemoComponent {
readonly pointsData: GeoJSON.FeatureCollection = {
type: 'FeatureCollection',
features: Array.from({ length: 100 }, (_, i) => ({
type: 'Feature' as const,
geometry: { type: 'Point' as const, coordinates: [-100 + Math.random() * 50, 30 + Math.random() * 20] },
properties: { id: i }
}))
};
}
```
--------------------------------
### Basic Map Component Usage
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/README.md
Use the MglMap component in your Angular template to display a map. Import MapComponent for single use.
```typescript
import { Component } from '@angular/core';
import { MapComponent } from '@maplibre/ngx-maplibre-gl';
@Component({
template: `
`,
styles: [
`
mgl-map {
height: 100%;
width: 100%;
}
`,
],
imports: [MapComponent],
})
export class AppComponent {}
```
--------------------------------
### Add and Customize a Line Layer
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Use `mgl-layer` to render a GeoJSON source as a line. Customize line appearance, layout, and visibility with zoom constraints. Event handlers like `layerClick` and `layerMouseEnter` are supported.
```typescript
import { Component, signal } from '@angular/core';
import {
MapComponent,
GeoJSONSourceComponent,
LayerComponent,
FeatureComponent
} from '@maplibre/ngx-maplibre-gl';
import type { MapLayerMouseEvent } from 'maplibre-gl';
@Component({
selector: 'app-layer-demo',
template: `
`,
imports: [MapComponent, GeoJSONSourceComponent, LayerComponent, FeatureComponent]
})
export class LayerDemoComponent {
readonly cursorStyle = signal('');
readonly lineGeometry: GeoJSON.LineString = {
type: 'LineString',
coordinates: [
[-122.483, 37.833],
[-122.483, 37.830],
[-122.486, 37.830]
]
};
onLayerClick(event: MapLayerMouseEvent): void {
console.log('Layer clicked', event.features);
}
}
```
--------------------------------
### Use Standalone Imports with NgxMapLibreGLImports
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
For standalone components, use the NgxMapLibreGLImports array to import all necessary components. This provides a convenient way to include the library's features.
```typescript
import { Component } from '@angular/core';
import { NgxMapLibreGLImports } from '@maplibre/ngx-maplibre-gl';
@Component({
selector: 'app-root',
template: `
`,
imports: [...NgxMapLibreGLImports]
})
export class AppComponent {}
```
--------------------------------
### Displaying Popups on Map with mgl-popup
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Use the mgl-popup component to show informational popups. It can be attached to features or placed at specific coordinates. Ensure the PopupComponent is imported.
```typescript
import { Component, signal } from '@angular/core';
import {
MapComponent,
PopupComponent,
GeoJSONSourceComponent,
LayerComponent
} from '@maplibre/ngx-maplibre-gl';
import type { MapLayerMouseEvent } from 'maplibre-gl';
@Component({
selector: 'app-popup-demo',
template: `
@if (selectedPlace(); as place) {
{{ place.properties?.['title'] }}
{{ place.properties?.['description'] }}
}
Static Popup
`,
imports: [MapComponent, PopupComponent, GeoJSONSourceComponent, LayerComponent]
})
export class PopupDemoComponent {
readonly selectedPlace = signal(null);
readonly cursorStyle = signal('');
readonly placesData: GeoJSON.FeatureCollection = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: { type: 'Point', coordinates: [-77.04, 38.907] },
properties: { title: 'Washington DC', description: 'Capital of the United States' }
}
]
};
selectPlace(event: MapLayerMouseEvent): void {
this.selectedPlace.set(event.features![0]);
}
}
```
--------------------------------
### Add and Customize HTML Markers
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Use `mgl-marker` to add HTML-based markers to the map. Markers can be configured as draggable and can contain custom Angular content. Popups can also be attached.
```typescript
import { Component, signal } from '@angular/core';
import {
MapComponent,
MarkerComponent,
PopupComponent,
ControlComponent
} from '@maplibre/ngx-maplibre-gl';
import type { Marker } from 'maplibre-gl';
@Component({
selector: 'app-marker-demo',
template: `
Custom HTML
@if (coordinates(); as coords) {
Longitude: {{ coords[0] }}
Latitude: {{ coords[1] }}
}
`,
imports: [MapComponent, MarkerComponent, ControlComponent]
})
export class MarkerDemoComponent {
readonly coordinates = signal(null);
onDragEnd(marker: Marker): void {
this.coordinates.set(marker.getLngLat().toArray());
}
}
```
--------------------------------
### Add maplibre-gl CSS to angular.json
Source: https://github.com/maplibre/ngx-maplibre-gl/blob/main/README.md
Include the maplibre-gl CSS file in your angular.json styles array for proper map rendering.
```json
"styles": [
...,
"./node_modules/maplibre-gl/dist/maplibre-gl.css"
],
```
--------------------------------
### Integrating Map Controls with mgl-control
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
The mgl-control component serves as a container for various map controls like navigation, fullscreen, geolocate, and scale. Import the relevant control directives. Custom controls can also be implemented using Angular content.
```typescript
import { Component } from '@angular/core';
import {
MapComponent,
ControlComponent,
NavigationControlDirective,
FullscreenControlDirective,
GeolocateControlDirective,
ScaleControlDirective
} from '@maplibre/ngx-maplibre-gl';
import type { Position } from '@maplibre/ngx-maplibre-gl';
@Component({
selector: 'app-controls-demo',
template: `
`,
imports: [
MapComponent,
ControlComponent,
NavigationControlDirective,
FullscreenControlDirective,
GeolocateControlDirective,
ScaleControlDirective
]
})
export class ControlsDemoComponent {
onGeolocate(position: Position): void {
console.log('User location:', position.coords.latitude, position.coords.longitude);
}
zoomIn(): void {
// Custom zoom logic
}
zoomOut(): void {
// Custom zoom logic
}
}
```
--------------------------------
### Enable Draggable Features with DraggableDirective
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Use the mglDraggable directive to enable drag interactions on point features. It emits drag events and automatically updates feature coordinates.
```typescript
import { Component, signal } from '@angular/core';
import {
MapComponent,
GeoJSONSourceComponent,
FeatureComponent,
LayerComponent,
DraggableDirective
} from '@maplibre/ngx-maplibre-gl';
import type { MapMouseEvent } from 'maplibre-gl';
@Component({
selector: 'app-draggable-demo',
template: `
Current position: {{ pointGeometry().coordinates | json }}
`,
imports: [
MapComponent,
GeoJSONSourceComponent,
FeatureComponent,
LayerComponent,
DraggableDirective
]
})
export class DraggableDemoComponent {
readonly pointGeometry = signal({
type: 'Point',
coordinates: [-122.4, 37.8]
});
onDragStart(event: MapMouseEvent): void {
console.log('Drag started');
}
onDrag(event: MapMouseEvent): void {
// Feature coordinates update automatically
}
onDragEnd(event: MapMouseEvent): void {
console.log('Drag ended at:', event.lngLat);
}
}
```
--------------------------------
### Declarative GeoJSON Features with mgl-feature
Source: https://context7.com/maplibre/ngx-maplibre-gl/llms.txt
Use `mgl-feature` within `mgl-geojson-source` to declaratively define GeoJSON features. Supports dynamic updates via signals. Ensure `FeatureComponent` is imported.
```typescript
import { Component, signal } from '@angular/core';
import {
MapComponent,
GeoJSONSourceComponent,
FeatureComponent,
LayerComponent
} from '@maplibre/ngx-maplibre-gl';
@Component({
selector: 'app-feature-demo',
template: `
@for (point of points(); track point.id) {
}
`,
imports: [MapComponent, GeoJSONSourceComponent, FeatureComponent, LayerComponent]
})
export class FeatureDemoComponent {
readonly points = signal;
}>>([
{ id: 1, geometry: { type: 'Point', coordinates: [-122.4, 37.8] }, properties: { color: 'red' } },
{ id: 2, geometry: { type: 'Point', coordinates: [-122.41, 37.81] }, properties: { color: 'blue' } }
]);
addPoint(): void {
const newId = this.points().length + 1;
this.points.update(pts => [...pts, {
id: newId,
geometry: { type: 'Point', coordinates: [-122.4 + Math.random() * 0.02, 37.8 + Math.random() * 0.02] },
properties: { color: 'green' }
}]);
}
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.