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.

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.tsimport { 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 {} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
translate-features.react.tsximport { useState } from 'react';
import type OLSelect from 'ol/interaction/Select.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
GeoJSONSource,
Map,
SelectInteraction,
TranslateInteraction,
VectorLayer,
View,
} from 'cc-openlayers/react';
/** Voivodeships that are selected with a click (Shift adds more) and dragged elsewhere on the map. */
export function TranslateFeatures() {
// The select interaction is made after the first commit, so the state carries it to the translate interaction.
const [select, setSelect] = useState<OLSelect | null>(null);
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<SelectInteraction ref={setSelect} />
{select ? <TranslateInteraction features={select.getFeatures()} /> : null}
<View zoom={7}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<VectorLayer background="white">
<GeoJSONSource url="assets/poland/voivodeships.geojson" featureProjection="EPSG:3857" />
</VectorLayer>
</Map>
<div className="example-controls">
<p className="example-hint">Click a voivodeship, or hold Shift to select several, then drag the selection.</p>
</div>
</>
);
}How the React version works
Voivodeships selected with a click (Shift adds more) and dragged elsewhere with the translate interaction.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection.
From Angular to React
| Angular | React |
|---|---|
<aol-source-geojson> with the voivodeships | <GeoJSONSource> |
<aol-interaction-translate [features]> from viewChild of the select | <TranslateInteraction features> from the select taken through ref={setSelect} into useState |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular.
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-translatebinds directly toselect.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-translatewithout restrictingfeaturesorlayersmakes 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:4326degrees before saving moved features to a geographic backend.
Related examples
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.