主题
InteractionsFacade — 交互子门面
通过 app.interactions 访问绘制、编辑、交互管理功能。
draw(mode, options)
创建绘制交互。
ts
draw(mode: string, options?: { symbol?: object; snap?: boolean; onFinish?: (geometry: object) => void }): InteractionRef| 参数 | 类型 | 描述 |
|---|---|---|
| mode | string | 绘制模式:'point'、'line'、'polygon' |
| symbol | object | 绘制时使用的符号 |
| snap | boolean | 是否启用吸附 |
| onFinish | function | 绘制完成回调 |
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| 参数 | 类型 | 描述 |
|---|---|---|
| target | FeatureRef | FeatureRef[] | 要编辑的要素(单个或数组) |
| mode | string | 编辑模式 |
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| 参数 | 类型 | 描述 |
|---|---|---|
| target | FeatureRef | 要拖拽的要素 |
| constraint | string | 拖拽约束方向 |
ts
const ref = app.interactions.drag(featureRef, { constraint: 'horizontal' });snap(options)
创建吸附交互。
ts
snap(options?: { layers?: Array<string | LayerRef>; tolerance?: number }): InteractionRef| 参数 | 类型 | 描述 |
|---|---|---|
| layers | Array<string | LayerRef> | 吸附目标图层 |
| tolerance | number | 吸附容差(像素) |
ts
const ref = app.interactions.snap({ tolerance: 10, layers: ['myLayer'] });stop(ref)
停止指定交互。
ts
stop(ref: InteractionRef): voidts
app.interactions.stop(drawRef);stopAll()
停止所有交互。
ts
stopAll(): voidts
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);popup(options)
创建弹窗。
ts
popup(options?: { position?: Coordinate; content?: string | HTMLElement; anchor?: string; autoClose?: boolean }): PopupRef| 参数 | 类型 | 描述 |
|---|---|---|
| position | [number, number] | 弹窗位置 |
| content | string | HTMLElement | 内容 |
| anchor | string | 锚点位置 |
| autoClose | boolean | 是否自动关闭 |
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 }| 参数 | 类型 | 描述 |
|---|---|---|
| content | string | 提示内容 |
| position | [number, number] | 提示位置 |
返回值包含两个方法:
| 方法 | 说明 |
|---|---|
| showFor(feature, content?) | 绑定到指定要素显示提示 |
| hide() | 隐藏提示 |
ts
const tip = app.interactions.tooltip({ content: '详情信息' });
tip.showFor(featureRef, '自定义提示');
tip.hide();InteractionRef
所有交互创建返回 InteractionRef:
| 属性 | 类型 | 说明 |
|---|---|---|
| id | string | 交互唯一标识 |
| stop() | void | 停止交互 |
PopupRef
弹窗返回 PopupRef,支持链式调用:
| 方法 | 返回值 | 说明 |
|---|---|---|
| setContent(content) | PopupRef | 设置内容 |
| open() | PopupRef | 打开弹窗 |
| close() | PopupRef | 关闭弹窗 |