Skip to content

SelectionFacade — 选择子门面

通过 app.selection 访问要素选择和高亮功能。

pick(at, options)

空间拾取:在指定位置选取要素。

ts
pick(at: Coordinate | [number, number], options?: { layers?: Array<string | LayerRef>; radius?: number; mode?: 'first' | 'all' }): PickResult[]
参数类型描述
atCoordinate拾取位置坐标或像素坐标
layersArray<string | LayerRef>限制拾取的图层
radiusnumber拾取容差(像素)
modestring'first' 返回第一个匹配,'all' 返回所有匹配
ts
const results = app.selection.pick([400, 300]);
// results: PickResult[]

const filtered = app.selection.pick([400, 300], {
  layers: ['myLayer'],
  radius: 10,
  mode: 'all',
});

byBox(bbox, options)

矩形框选:选取范围内的要素。

ts
byBox(bbox: Extent, options?: { layers?: Array<string | LayerRef> }): FeatureRef[]
参数类型描述
bboxExtent选择范围 [minX, minY, maxX, maxY]
layersArray<string | LayerRef>限制选择的图层
ts
const features = app.selection.byBox([116, 39, 122, 42]);

byPolygon(geometry, options)

多边形选择:选取多边形内的要素。

ts
byPolygon(geometry: unknown, options?: { layers?: Array<string | LayerRef> }): FeatureRef[]
参数类型描述
geometryGeoJSON Polygon选择区域几何
layersArray<string | LayerRef>限制选择的图层
ts
const features = app.selection.byPolygon({
  type: 'Polygon',
  coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
});

byAttribute(layer, where)

属性查询选择。

ts
byAttribute(layer: string | LayerRef, where: string): FeatureRef[]
参数类型描述
layerstring | LayerRef目标图层
wherestring属性过滤表达式
ts
const features = app.selection.byAttribute('myLayer', 'name = "test"');

选择集管理

set(features, options)

设置选择集(替换现有选择)。

ts
set(features: FeatureRef[], options?: { highlight?: boolean }): void
参数类型描述
featuresFeatureRef[]要选择的要素数组
highlightboolean是否同时高亮
ts
app.selection.set([feature1, feature2]);
app.selection.set([feature1], { highlight: true });

add(feature)

向选择集中添加要素。

ts
add(feature: FeatureRef): void
ts
app.selection.add(feature3);

remove(feature)

从选择集中移除要素。

ts
remove(feature: FeatureRef): void
ts
app.selection.remove(feature1);

clear()

清空选择集。

ts
clear(): void
ts
app.selection.clear();

get()

获取当前选择集的副本。

ts
get(): FeatureRef[]

返回值为副本数组,修改不影响内部选择集。

ts
const selected = app.selection.get();
// selected.length === 2
selected.push({ id: 99 }); // 不影响内部状态
app.selection.get().length; // 仍为 2

高亮

highlight(features, options)

高亮指定要素。

ts
highlight(features: FeatureRef[], options?: { color?: string; opacity?: number }): void
参数类型描述
featuresFeatureRef[]要高亮的要素
colorstring高亮颜色
opacitynumber不透明度 (0-1)
ts
app.selection.highlight([feature1, feature2], { color: '#ff0000', opacity: 0.8 });

clearHighlight()

清除高亮。

ts
clearHighlight(): void
ts
app.selection.clearHighlight();

四川省交通运输综合地理服务平台 地图开发框架