Skip to content

Selection

Overview

useSelection(name, options) extends the core useSelection composable with map-specific selection behaviour. Items in the selection store represent { location, feature, layer } triplets. The composable registers click, box-selection, and cluster-selection handlers on the current map activity and keeps the selection in sync with real-time service events on visible layers.

Usage

javascript
import { useSelection } from '@kalisio/kdk/map.client'

const {
  selection, selectItem, clearSelection,
  hasSelectedFeature, getSelectedFeature, getSelectedLayer,
  hasSelectedLocation, getSelectedLocation,
  centerOnSelection, getWidgetForSelection
} = useSelection('myMap', { boxSelection: true })

Parameters

ParameterTypeDefaultDescription
namestringUnique selection store name within the application.
options.multiplestring'ctrlKey'Name of the keyboard modifier property that activates multi-selection (e.g. 'ctrlKey', 'shiftKey').
options.buffernumber10Selection buffer radius in pixels used in buffer selection mode.
options.showBufferbooleanfalseWhen true, briefly renders the buffer circle on the map after a buffer-mode click.
options.showBufferDelaynumber250Duration in milliseconds to show the buffer circle.
options.boxSelectionbooleantrueWhen true, enables rectangular box-selection on the map.
options.clusterSelectionbooleanfalseWhen true, selects all features of a cluster when a cluster marker is clicked (spiderfied).
options.handlerFunctionCustom selection handler (items, clearSelection). When provided, replaces the built-in handler.

Exposed

All values from the core useSelection are exposed, plus:

NameTypeDescription
getSelectionOptions()FunctionReturns the resolved options object stored in the selection store.
setCurrentActivity(activity)FunctionBinds the composable to a new map activity instance. Removes listeners from the previous activity.
hasSelectedFeature()FunctionReturns true if the last selected item has a feature property.
getSelectedFeature()FunctionReturns the feature of the last selected item.
getSelectedFeatures()FunctionReturns all selected features (items that have a feature).
getSelectedFeatureCollection()FunctionReturns selected features as a GeoJSON FeatureCollection.
getSelectedFeaturesByLayer()FunctionReturns selected features grouped by layer as [{ layer, features }].
hasSelectedLayer()FunctionReturns true if the last selected item has a layer property.
getSelectedLayer()FunctionReturns the layer of the last selected item.
getSelectedLayers()FunctionReturns all layers associated with selected items.
hasSelectedLocation()FunctionReturns true if the last selected item has a location property.
getSelectedLocation()FunctionReturns the location of the last selected item.
getWidgetForSelection()FunctionReturns the widget type string to display for the current selection (e.g. 'time-series', 'information-box', 'mapillary-viewer').
centerOnSelection()FunctionCenters the map view on the currently selected feature or location.
setBoxSelectionEnabled(enabled)FunctionDynamically enables or disables box selection.
setClusterSelectionEnabled(enabled)FunctionDynamically enables or disables cluster selection.
setBufferWidth(width)FunctionSets the buffer selection radius in pixels.

Lifecycle

  • Registers click, boxselectionend, and spiderfied engine event handlers on the active map activity.
  • Listens to layer-added / layer-removed events to track real-time service events on feature layers.
  • Automatically unselects features that belong to a hidden layer (layer-hidden event).
  • When the activity changes via setCurrentActivity, all listeners are migrated to the new activity.