Interaction Components

Interaction components handle user input and enable functionality like feature selection, drawing, and editing. They provide a reactive interface to OpenLayers’ interaction system with automatic reactivity for seamless bidirectional updates.

Interaction.Select

Allows users to select features by clicking or other methods.

Basic Usage

<script>
	import { Map, Layer, Feature, Interaction } from 'svelte-openlayers';

	let selectedFeatures: ReactiveCollection | null = $state(null);

	const selectedStyle = createCircleStyle({
		radius: 12,
		fill: '#ef4444',
		stroke: '#ffffff',
		strokeWidth: 3
	});
</script>

<Map.Root>
	<Map.View center={[0, 0]} zoom={2} />
	<Layer.Tile source="osm" />
	<Layer.Vector>
		<Feature.Point coordinates={[-74.0, 40.7]} properties={{ name: 'New York' }} />
		<Feature.Point coordinates={[-118.2, 34.0]} properties={{ name: 'Los Angeles' }} />
	</Layer.Vector>

	<Interaction.Select bind:selectedFeatures style={selectedStyle} />
</Map.Root>

Props

PropTypeDefaultDescription
styleStyleLikeundefinedStyle for selected features
layersLayer[]undefinedLayers to select from
filteranyundefinedFeature filter function
multibooleanfalseAllow multiple selection
hitTolerancenumberundefinedHit detection tolerance in pixels
addConditionanyundefinedCondition for adding to selection
removeConditionanyundefinedCondition for removing from selection
toggleConditionanyundefinedCondition for toggling selection
onSelect(features: Feature[]) => voidundefinedSelection callback
interactionanyundefinedBindable interaction instance (read-only)
selectedFeaturesReactiveCollection<Feature>undefinedBindable reactive selected features collection
reactivebooleantrueEnable automatic reactivity (set to false for performance)

Interaction.Hover

Detects when the pointer hovers over features and provides callbacks for hover events.

Basic Usage

<script>
	import { Map, Layer, Feature, Interaction } from 'svelte-openlayers';

	let hoveredFeature = null;

	const handleHover = (feature, coordinate) => {
		hoveredFeature = feature;
	};

	const handleHoverEnd = () => {
		hoveredFeature = null;
	};
</script>

<Map.Root>
	<Map.View center={[0, 0]} zoom={2} />
	<Layer.Tile source="osm" />
	<Layer.Vector>
		<Feature.Point coordinates={[-74.0, 40.7]} properties={{ name: 'New York' }} />
		<Feature.Point coordinates={[-118.2, 34.0]} properties={{ name: 'Los Angeles' }} />
	</Layer.Vector>

	<Interaction.Hover onHover={handleHover} onHoverEnd={handleHoverEnd} />
</Map.Root>

Props

PropTypeDefaultDescription
onHover(feature: Feature | null, coordinate?: Coordinate) => voidundefinedHover event callback
onHoverEnd() => voidundefinedHover end callback
layersLayer[]undefinedLayers to detect hover on
hitTolerancenumberundefinedHit detection tolerance in pixels
interactionany | nullnullBindable interaction instance (read-only)

Coming Soon: Additional interaction types including Interaction.Draw, Interaction.Modify, and Interaction.Translate are planned for future releases.