主题
SelectionFacade — 选择子门面
通过 app.selection 访问要素选择和高亮功能。
pick(at, options)
空间拾取:在指定位置选取要素。
ts
pick(at: Coordinate | [number, number], options?: { layers?: Array<string | LayerRef>; radius?: number; mode?: 'first' | 'all' }): PickResult[]| 参数 | 类型 | 描述 |
|---|---|---|
| at | Coordinate | 拾取位置坐标或像素坐标 |
| layers | Array<string | LayerRef> | 限制拾取的图层 |
| radius | number | 拾取容差(像素) |
| mode | string | '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[]| 参数 | 类型 | 描述 |
|---|---|---|
| bbox | Extent | 选择范围 [minX, minY, maxX, maxY] |
| layers | Array<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[]| 参数 | 类型 | 描述 |
|---|---|---|
| geometry | GeoJSON Polygon | 选择区域几何 |
| layers | Array<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[]| 参数 | 类型 | 描述 |
|---|---|---|
| layer | string | LayerRef | 目标图层 |
| where | string | 属性过滤表达式 |
ts
const features = app.selection.byAttribute('myLayer', 'name = "test"');选择集管理
set(features, options)
设置选择集(替换现有选择)。
ts
set(features: FeatureRef[], options?: { highlight?: boolean }): void| 参数 | 类型 | 描述 |
|---|---|---|
| features | FeatureRef[] | 要选择的要素数组 |
| highlight | boolean | 是否同时高亮 |
ts
app.selection.set([feature1, feature2]);
app.selection.set([feature1], { highlight: true });add(feature)
向选择集中添加要素。
ts
add(feature: FeatureRef): voidts
app.selection.add(feature3);remove(feature)
从选择集中移除要素。
ts
remove(feature: FeatureRef): voidts
app.selection.remove(feature1);clear()
清空选择集。
ts
clear(): voidts
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| 参数 | 类型 | 描述 |
|---|---|---|
| features | FeatureRef[] | 要高亮的要素 |
| color | string | 高亮颜色 |
| opacity | number | 不透明度 (0-1) |
ts
app.selection.highlight([feature1, feature2], { color: '#ff0000', opacity: 0.8 });clearHighlight()
清除高亮。
ts
clearHighlight(): voidts
app.selection.clearHighlight();