Interactions · Angular, React and OpenLayers

Move selected features with a drag

Drag single or Shift-selected OpenLayers polygons to new locations as rigid shapes by binding aol-interaction-translate to selected features.

Voivodeship outlines in blue with Małopolska selected and dragged south-east over Subcarpathia, leaving a gap where it was.
A screenshot of the example. Voivodeships: Państwowy Rejestr Granic © GUGiK.

What this example shows

Clicking a Polish voivodeship (or Shift-clicking several) highlights the selection in blue; starting a mouse or touch drag anywhere inside a selected voivodeship slides the entire multi-polygon group smoothly across the map as a rigid unit while dragging unselected space pans the camera as normal.

When you need it

Layout planners, tactical symbol boards and CAD import tools frequently need users to reposition whole building footprints, equipment zones or imported schematic groups without accidentally bending individual vertices.

Scoping Translate strictly to the Select interaction’s live collection ensures unselected background polygons never slide out of place during normal map panning.

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.

translate-features.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerVectorComponent,
  MapComponent,
  SelectInteractionComponent,
  SourceGeoJSONComponent,
  ViewComponent,
} from 'cc-openlayers';
import { TranslateInteractionComponent } from 'cc-openlayers/editing';

/** Voivodeships that are selected with a click (Shift adds more) and dragged elsewhere on the map. */
@Component({
  selector: 'app-translate-features',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerVectorComponent,
    MapComponent,
    SelectInteractionComponent,
    SourceGeoJSONComponent,
    TranslateInteractionComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-interaction-select #select />
      <aol-interaction-translate [features]="select.instance.getFeatures()" />
      <aol-view [zoom]="7"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
      <aol-layer-vector background="white">
        <aol-source-geojson url="assets/poland/voivodeships.geojson" featureProjection="EPSG:3857" />
      </aol-layer-vector>
    </aol-map>
    <div class="example-controls">
      <p class="example-hint">Click a voivodeship, or hold Shift to select several, then drag the selection.</p>
    </div>
  `,
})
export class TranslateFeatures {}

Architecture and OpenLayers mechanics

aol-interaction-select (#select) manages single clicks and default Shift-click multi-selection, exposing its live Collection<Feature> via select.instance.getFeatures().

Passing that exact collection to aol-interaction-translate tells OpenLayers to intercept pointer drags strictly when the press starts atop a currently selected feature, translating all selected geometries together while letting presses elsewhere bubble to DragPan.

How it works

  • aol-interaction-translate binds directly to select.instance.getFeatures() exposed through template reference #select.
  • Plain clicks select one voivodeship while Shift+click toggles additional provinces into the movable group.
  • Drags starting on a selected feature translate the entire selection together; drags starting elsewhere pan the map.

Pitfalls

  • Mounting aol-interaction-translate without restricting features or layers makes every vector feature on the map immediately draggable on touch or click—hijacking normal map panning over dense polygon layers.
  • Translated vertex coordinates mutate in place in the map’s Web Mercator projection—reproject back to EPSG:4326 degrees before saving moved features to a geographic backend.

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.