Interactions · Angular, React and OpenLayers

Select features with a click

Select vector features on click in OpenLayers restricted to a target marker layer via a layers filter and track selected feature IDs in signals.

A map of Kraków with the selected point on the Main Square, and Selected features 1 and Feature IDs marker-1 below it.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Over central Kraków, clicking a point marker on the Main Square highlights it in OpenLayers’ selection style and updates reactive status readouts below the map with the selected count and exact feature ID (marker-1).

When you need it

Real-world WebGIS screens stack graticules, administrative borders, search radii and clickable POI markers simultaneously—where clicking a marker must inspect only the POI layer without accidentally selecting background reference polygons.

Scoping OpenLayers’ Select interaction with an explicit layer predicate isolates click selection cleanly to business features.

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-interaction.ts
import { ChangeDetectionStrategy, Component, computed, signal, viewChild } from '@angular/core';
import type { SelectEvent } from 'ol/interaction/Select';
import type Layer from 'ol/layer/Layer';
import {
  CoordinateComponent,
  DefaultInteractionComponent,
  FeatureComponent,
  GeometryPointComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SelectInteractionComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';

/** Features selected with the select interaction, limited to the markers layer, with their IDs listed. */
@Component({
  selector: 'app-select-interaction',
  imports: [
    CoordinateComponent,
    DefaultInteractionComponent,
    FeatureComponent,
    GeometryPointComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SelectInteractionComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-interaction-select [layers]="isMarkerLayer" (olSelect)="select($event)" />
      <aol-view [zoom]="13">
        <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-source-vector [wrapX]="false">
          <aol-feature id="marker-1">
            <aol-geometry-point><aol-coordinate [x]="19.9373" [y]="50.0617" srid="EPSG:4326" /></aol-geometry-point>
          </aol-feature>
        </aol-source-vector>
      </aol-layer-vector>
    </aol-map>
    <div class="example-controls">
      <p class="example-hint">Click the point to select it, click the background to clear the selection.</p>
      <span>
        Selected features <output>{{ selectedIds().length }}</output>
      </span>
      <span>
        Feature IDs <output>{{ selectionLabel() }}</output>
      </span>
    </div>
  `,
})
export class SelectInteraction {
  readonly selectedIds = signal<Array<string | number>>([]);
  readonly selectionLabel = computed(() => this.selectedIds().join(', ') || 'None');
  private readonly markers = viewChild.required(LayerVectorComponent);

  protected readonly isMarkerLayer = (layer: Layer) => layer === this.markers().instance;

  protected select(event: SelectEvent): void {
    const removed = new Set(event.deselected.map((feature) => feature.getId()));
    const added = event.selected.map((feature) => feature.getId()).filter((id) => id !== undefined);
    this.selectedIds.update((ids) => [...new Set([...ids.filter((id) => !removed.has(id)), ...added])]);
  }
}

Architecture and OpenLayers mechanics

aol-interaction-select restricts hit-testing via its [layers] predicate, comparing candidate layers against the target marker layer queried with viewChild.required(LayerVectorComponent).

On every olSelect event, the component reads the event’s selected and deselected features, extracts each feature’s OpenLayers ID (assigned from its id attribute, carefully preserving numeric 0 while filtering out undefined), and writes the ID list into a signal.

How it works

  • aol-interaction-select emits olSelect whenever features enter or leave selection, syncing their IDs into a reactive signal.
  • Passing a predicate function to [layers] restricts click selection strictly to the viewChild.required(LayerVectorComponent) marker layer.
  • Setting the id input on a feature assigns its canonical OpenLayers feature identifier (getId()).
  • Explicitly checking against undefined ensures valid numeric ID 0 is never discarded as falsy.

Pitfalls

  • Omitting [layers] lets aol-interaction-select pick features from every visible vector layer on the map—including decorative boundary masks or measurement sketches.
  • Assign stable domain IDs to selectable business features up front so selected records can be correlated reliably with external tables and API calls.

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.