Skip to content

InteractionsFacade — 交互子门面

通过 app.interactions 访问绘制、编辑、交互管理功能。

draw(mode, options)

创建绘制交互。

ts
draw(mode: string, options?: { symbol?: object; snap?: boolean; onFinish?: (geometry: object) => void }): InteractionRef
参数类型描述
modestring绘制模式:'point''line''polygon'
symbolobject绘制时使用的符号
snapboolean是否启用吸附
onFinishfunction绘制完成回调
ts
const ref = app.interactions.draw('polygon');
// ref.id 形如 'draw_1234567890'

app.interactions.draw('line', {
  symbol: { color: '#ff0000', width: 2 },
  onFinish: (geom) => console.log('completed', geom),
});

edit(target, options)

创建编辑交互。

ts
edit(target: FeatureRef | FeatureRef[], options?: { mode?: string }): InteractionRef
参数类型描述
targetFeatureRef | FeatureRef[]要编辑的要素(单个或数组)
modestring编辑模式
ts
const ref = app.interactions.edit(featureRef);

// 编辑多个要素
const ref2 = app.interactions.edit([feature1, feature2]);

drag(target, options)

创建拖拽交互。

ts
drag(target: FeatureRef, options?: { constraint?: 'none' | 'horizontal' | 'vertical' | 'surface' }): InteractionRef
参数类型描述
targetFeatureRef要拖拽的要素
constraintstring拖拽约束方向
ts
const ref = app.interactions.drag(featureRef, { constraint: 'horizontal' });

snap(options)

创建吸附交互。

ts
snap(options?: { layers?: Array<string | LayerRef>; tolerance?: number }): InteractionRef
参数类型描述
layersArray<string | LayerRef>吸附目标图层
tolerancenumber吸附容差(像素)
ts
const ref = app.interactions.snap({ tolerance: 10, layers: ['myLayer'] });

stop(ref)

停止指定交互。

ts
stop(ref: InteractionRef): void
ts
app.interactions.stop(drawRef);

stopAll()

停止所有交互。

ts
stopAll(): void
ts
app.interactions.stopAll();

setInput(input, enabled)

启用或禁用内置地图输入行为。

ts
setInput(input: string, enabled: boolean): void
输入类型说明
'dragPan'拖拽平移
'scrollZoom'滚轮缩放
'doubleClickZoom'双击缩放
'pinchZoom'捏合缩放
'rotate'旋转
'pitch'俯仰
'boxZoom'框选缩放
'keyboard'键盘导航
ts
app.interactions.setInput('dragPan', false);
app.interactions.setInput('scrollZoom', false);
app.interactions.setInput('rotate', true);

创建弹窗。

ts
popup(options?: { position?: Coordinate; content?: string | HTMLElement; anchor?: string; autoClose?: boolean }): PopupRef
参数类型描述
position[number, number]弹窗位置
contentstring | HTMLElement内容
anchorstring锚点位置
autoCloseboolean是否自动关闭
ts
const popup = app.interactions.popup({
  position: [116, 39],
  content: '<b>Hello</b>',
});

popup.setContent('New content');
popup.open();
popup.close();

tooltip(options)

创建提示框。

ts
tooltip(options?: { content?: string; position?: Coordinate }): { showFor(feature: FeatureRef, content?: string): void; hide(): void }
参数类型描述
contentstring提示内容
position[number, number]提示位置

返回值包含两个方法:

方法说明
showFor(feature, content?)绑定到指定要素显示提示
hide()隐藏提示
ts
const tip = app.interactions.tooltip({ content: '详情信息' });
tip.showFor(featureRef, '自定义提示');
tip.hide();

InteractionRef

所有交互创建返回 InteractionRef

属性类型说明
idstring交互唯一标识
stop()void停止交互

PopupRef

弹窗返回 PopupRef,支持链式调用:

方法返回值说明
setContent(content)PopupRef设置内容
open()PopupRef打开弹窗
close()PopupRef关闭弹窗

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