Essentials · Angular, React and OpenLayers

View padding for content over the map

Offset an OpenLayers view optical center permanently with the view padding option so fit, setCenter, zoom controls and rotation respect UI overlays.

Southern Poland at zoom 6, centred on Kraków in the area that dark translucent bands along the map's edges leave free, with the zoom buttons moved inside that area.
A screenshot of the example. Administrative boundaries © GUGiK. Map data © OpenStreetMap contributors.

What this example shows

Southern Poland opens at zoom 6 with translucent dark bands covering 170 pixels at the top, 50 on the right, 30 at the bottom and 150 on the left, while zoom buttons are shifted inward to the top-left corner of the clear window. Unlike per-call fit padding, the camera’s permanent optical centre sits over Kraków right in the middle of the uncovered rectangle.

Fitting geometries without extra arguments, calling setCenter, clicking zoom buttons or rotating with Alt+Shift and drag all pivot naturally around that offset centre.

When you need it

Geoportals, fleet consoles and property explorers with persistent headers, sidebars or bottom sheets need the entire camera system to treat the uncovered sub-window as the true centre of the map.

Configuring [padding]="padding" once on the view eliminates repetitive inset arguments on every camera call and fixes wheel/button zoom and rotation pivots while still rendering seamless tiles underneath translucent UI panels.

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.

view-padding.ts
import { ChangeDetectionStrategy, Component, viewChild } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceGeoJSONComponent,
  SourceOsmComponent,
  StyleComponent,
  StyleFillComponent,
  StyleStrokeComponent,
  ViewComponent,
} from 'cc-openlayers';
import type SimpleGeometry from 'ol/geom/SimpleGeometry';
import { fromLonLat } from 'ol/proj';

/** A view whose padding keeps its centre and its fitted extents clear of the bands that stand for content over the map's edges. */
@Component({
  selector: 'app-view-padding',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceGeoJSONComponent,
    SourceOsmComponent,
    StyleComponent,
    StyleFillComponent,
    StyleStrokeComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <div class="padded-map" [style]="edges">
      <aol-map height="480px">
        <aol-interaction-default />
        <aol-control-defaults />
        <aol-view [zoom]="6" [padding]="padding">
          <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
        </aol-view>
        <aol-layer-tile><aol-source-osm /></aol-layer-tile>
        <aol-layer-vector>
          <aol-style>
            <aol-style-fill color="rgba(255, 255, 255, 0.6)" />
            <aol-style-stroke color="#319FD3" [width]="1" />
          </aol-style>
          <aol-source-geojson url="assets/poland/poland.geojson" featureProjection="EPSG:3857" />
        </aol-layer-vector>
      </aol-map>
      <div class="padding-band top"></div>
      <div class="padding-band right"></div>
      <div class="padding-band bottom"></div>
      <div class="padding-band left"></div>
    </div>
    <div class="example-controls">
      <button type="button" (click)="zoomToPoland()">Zoom to Poland</button>
      <button type="button" (click)="centerOnKrakow()">Center on Kraków</button>
      <p class="example-hint">
        The bands stand for content over the map's edges: the view's padding of 170 px at the top, 50 px on the right,
        30 px at the bottom and 150 px on the left keeps its centre and its fitted extents clear of them. Hold Alt+Shift
        and drag to rotate around the shifted centre.
      </p>
    </div>
  `,
})
export class ViewPadding {
  protected readonly padding = [170, 50, 30, 150];
  /** The padding as the sizes of the bands that stand for content over the map's edges. */
  protected readonly edges = {
    '--top': `${this.padding[0]}px`,
    '--right': `${this.padding[1]}px`,
    '--bottom': `${this.padding[2]}px`,
    '--left': `${this.padding[3]}px`,
  };

  private readonly view = viewChild.required(ViewComponent);
  private readonly source = viewChild.required(SourceGeoJSONComponent);

  protected zoomToPoland(): void {
    const geometry = this.source().instance.getFeatures()[0]?.getGeometry();
    if (geometry) this.view().instance.fit(geometry as SimpleGeometry);
  }

  protected centerOnKrakow(): void {
    this.view().instance.setCenter(fromLonLat([19.9368, 50.0614]));
  }
}

Architecture and OpenLayers mechanics

Binding [padding]="padding" ([170, 50, 30, 150]) on the OpenLayers view shifts its internal viewport center away from the masked edges in CSS pixels. Every subsequent setCenter call places its coordinate at that shifted midpoint, fit(geometry as SimpleGeometry) frames features inside the clear rectangle automatically, and rotation gestures orbit the offset centre.

Shared CSS variables position both the visual overlay bands and the OpenLayers control containers so on-map buttons never hide behind covering panels.

How it works

  • [padding]="padding" applies 170, 50, 30 and 150 pixel insets (top, right, bottom, left) directly to the view, moving its optical centre to the middle of the free area.
  • fit(geometry as SimpleGeometry) needs no per-call padding option because a padded view constrains fitted extents to its uncovered region automatically.
  • setCenter, +/- zoom buttons and Alt+Shift rotation all pivot around the shifted centre of the free window.
  • CSS variables fed by the same padding tuple align both the translucent edge bands and the repositioned map controls.

Pitfalls

  • With view padding active, the view’s centre coordinate corresponds to the middle of the uncovered area rather than the middle of the <canvas> DOM box.
  • Passing per-call padding to fit adds on top of the view’s own padding and double-insets the geometry—specify the inset in one place only.
  • Default HTML controls do not move themselves when view padding is set; offset .ol-zoom and .ol-attribution in CSS so sidebars and bottom sheets do not cover them.

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.