Interactions · Angular, React and OpenLayers

Select features on hover

Highlight Polish voivodeship polygons dynamically on pointer hover in OpenLayers by configuring Select interaction condition as pointerMove.

A map of Poland's voivodeships in flat colours with Małopolska outlined under the pointer and named below the map.
A screenshot of the example. Voivodeships: Państwowy Rejestr Granic © GUGiK.

What this example shows

Across a categorical map of Poland’s 16 voivodeships coloured by TERYT code, gliding the mouse across provincial borders highlights whichever voivodeship sits beneath the cursor with a crisp white outline while preserving its base fill colour and displaying its Polish name below the map.

When you need it

Choropleth dashboards, statistical atlases and region pickers use instant hover highlighting to confirm which polygon the cursor is over before the user clicks to drill down.

Configuring an OpenLayers Select interaction with pointerMove delegates hit-testing, previous-feature deselection and highlight rendering entirely to OpenLayers.

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.

select-hover-features.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerVectorComponent,
  MapComponent,
  SelectInteractionComponent,
  SourceGeoJSONComponent,
  ViewComponent,
} from 'cc-openlayers';
import { pointerMove } from 'ol/events/condition';
import type { FeatureLike } from 'ol/Feature';
import type { SelectEvent } from 'ol/interaction/Select';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';

/** The colour of each voivodeship, by its position in code order. */
const PALETTE = [
  '#4e79a7',
  '#f28e2b',
  '#e15759',
  '#76b7b2',
  '#59a14f',
  '#edc948',
  '#b07aa1',
  '#ff9da7',
  '#9c755f',
  '#bab0ac',
  '#86bcb6',
  '#d37295',
  '#fabfd2',
  '#b6992d',
  '#499894',
  '#79706e',
];

const fillOf = (feature: FeatureLike) => new Fill({ color: PALETTE[Number(feature.get('code')) / 2 - 1] ?? '#eeeeee' });

/** Voivodeships selected by hovering: the one under the pointer gets a white outline and is named below the map. */
@Component({
  selector: 'app-select-hover-features',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerVectorComponent,
    MapComponent,
    SelectInteractionComponent,
    SourceGeoJSONComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-interaction-select [condition]="pointerMove" [style]="hoverStyle" (olSelect)="select($event)" />
      <aol-view [zoom]="6"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
      <aol-layer-vector background="white" [style]="colorStyle">
        <aol-source-geojson url="assets/poland/voivodeships.geojson" featureProjection="EPSG:3857" />
      </aol-layer-vector>
    </aol-map>
    <div class="example-controls">
      <span>
        Voivodeship <output>{{ name() || '–' }}</output>
      </span>
    </div>
  `,
})
export class SelectHoverFeatures {
  readonly name = signal('');
  protected readonly pointerMove = pointerMove;
  protected readonly colorStyle = (feature: FeatureLike) => new Style({ fill: fillOf(feature) });
  protected readonly hoverStyle = (feature: FeatureLike) =>
    new Style({
      fill: fillOf(feature),
      stroke: new Stroke({ color: 'rgba(255, 255, 255, 0.7)', width: 2 }),
    });

  protected select(event: SelectEvent): void {
    this.name.set(event.selected[0]?.get('name') ?? '');
  }
}

Architecture and OpenLayers mechanics

Passing OpenLayers’ pointerMove event predicate into [condition] switches the Select interaction from click-driven to continuous cursor tracking—selecting the polygon under the pointer and deselecting the previous one automatically.

A custom hoverStyle function preserves each voivodeship’s TERYT palette fill while adding a contrasting white border stroke, and olSelect writes the active province’s name into the name signal (clearing it when moving onto empty background).

How it works

  • Setting [condition] to pointerMove makes Select follow cursor motion automatically instead of waiting for clicks.
  • hoverStyle retains each polygon’s TERYT-derived fill colour while adding a high-contrast white outline.
  • olSelect updates the name signal with the hovered voivodeship’s name and clears it when leaving the country.
  • Deterministic TERYT codes (02, 04, ...) map each province to its consistent categorical colour.

Pitfalls

  • Touch screens emit no passive hover events without touching and dragging the screen—always pair hover previews with a click/tap interaction when supporting mobile and tablet users.
  • Hover selection clears the instant the cursor moves away: mount a second click-conditioned Select interaction whenever users also need to pin a chosen region while interacting with a side panel.

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.