my-openlayer 2.4.8 → 2.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +212 -102
- package/MyOl.js +2 -0
- package/README.md +156 -156
- package/core/Point.js +21 -3
- package/docs/.vitepress/config.mts +57 -57
- package/docs/ConfigManager.md +71 -71
- package/docs/ErrorHandler.md +106 -106
- package/docs/EventManager.md +142 -142
- package/docs/Line.md +187 -187
- package/docs/MapBaseLayers.md +198 -198
- package/docs/MapTools.md +172 -172
- package/docs/MeasureHandler.md +87 -87
- package/docs/MyOl.md +247 -247
- package/docs/Point.md +165 -165
- package/docs/Polygon.md +241 -241
- package/docs/RiverLayerManager.md +187 -187
- package/docs/SelectHandler.md +147 -147
- package/docs/ValidationUtils.md +83 -83
- package/docs/VueTemplatePoint.md +214 -214
- package/docs/index.md +78 -78
- package/package.json +1 -2
- package/types.d.ts +2 -0
- package/AI_CONTEXT.md +0 -147
package/docs/EventManager.md
CHANGED
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
# EventManager 事件管理
|
|
2
|
-
|
|
3
|
-
`EventManager` 类用于统一管理地图事件,提供简单的 API 来监听、移除和触发地图事件。它对 OpenLayers 的原生事件系统进行了封装,提供了类型安全的事件处理。
|
|
4
|
-
|
|
5
|
-
## 导入
|
|
6
|
-
|
|
7
|
-
```typescript
|
|
8
|
-
import { EventManager, MapEventData } from 'my-openlayer';
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## 接口定义
|
|
12
|
-
|
|
13
|
-
### MapEventData 接口
|
|
14
|
-
|
|
15
|
-
统一的事件数据结构,回调函数会接收此类型的参数。
|
|
16
|
-
|
|
17
|
-
| 属性名 | 类型 | 描述 |
|
|
18
|
-
| :--- | :--- | :--- |
|
|
19
|
-
| `type` | `MapEventType` | 事件类型 |
|
|
20
|
-
| `originalEvent` | `Event` | 原始 DOM 事件对象 (可选) |
|
|
21
|
-
| `coordinate` | `number[]` | 发生事件的地理坐标 (可选) |
|
|
22
|
-
| `pixel` | `Pixel` | 发生事件的屏幕像素坐标 (可选) |
|
|
23
|
-
| `features` | `FeatureLike[]` | 事件位置处的要素列表 (可选) |
|
|
24
|
-
| `feature` | `FeatureLike` | 事件位置处的第一个要素 (可选) |
|
|
25
|
-
| `zoom` | `number` | 当前地图缩放级别 (可选) |
|
|
26
|
-
| `[key: string]` | `any` | 其他自定义属性 |
|
|
27
|
-
|
|
28
|
-
### MapEventType 类型
|
|
29
|
-
|
|
30
|
-
支持的事件类型字符串:
|
|
31
|
-
`'click'` | `'dblclick'` | `'hover'` | `'moveend'` | `'zoomend'` | `'pointermove'` | `'rendercomplete'` | `'error'`
|
|
32
|
-
|
|
33
|
-
## 构造函数
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
constructor(map: Map)
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
| 参数名 | 类型 | 描述 |
|
|
40
|
-
| :--- | :--- | :--- |
|
|
41
|
-
| `map` | `Map` | OpenLayers 地图实例 |
|
|
42
|
-
|
|
43
|
-
## 方法
|
|
44
|
-
|
|
45
|
-
### 监听事件
|
|
46
|
-
|
|
47
|
-
```typescript
|
|
48
|
-
on(type: MapEventType, callback: EventCallback, options?: { once?: boolean; filter?: (event: MapEventData) => boolean; }): string
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
注册事件监听器。
|
|
52
|
-
|
|
53
|
-
| 参数名 | 类型 | 描述 |
|
|
54
|
-
| :--- | :--- | :--- |
|
|
55
|
-
| `type` | `MapEventType` | 事件类型 |
|
|
56
|
-
| `callback` | `EventCallback` | 事件回调函数 |
|
|
57
|
-
| `options` | `object` | 监听选项 (可选) |
|
|
58
|
-
| `options.once` | `boolean` | 是否只触发一次 |
|
|
59
|
-
| `options.filter` | `function` | 事件过滤器,返回 false 则不触发回调 |
|
|
60
|
-
|
|
61
|
-
**返回值**: `string` - 监听器 ID,用于取消监听。
|
|
62
|
-
|
|
63
|
-
### 移除监听
|
|
64
|
-
|
|
65
|
-
```typescript
|
|
66
|
-
off(id: string): boolean
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
根据监听器 ID 移除事件监听。
|
|
70
|
-
|
|
71
|
-
| 参数名 | 类型 | 描述 |
|
|
72
|
-
| :--- | :--- | :--- |
|
|
73
|
-
| `id` | `string` | 监听器 ID |
|
|
74
|
-
|
|
75
|
-
**返回值**: `boolean` - 是否成功移除。
|
|
76
|
-
|
|
77
|
-
### 移除所有监听
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
clear(): void
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
移除所有已注册的事件监听器。
|
|
84
|
-
|
|
85
|
-
## 使用示例
|
|
86
|
-
|
|
87
|
-
### 基础事件监听
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
import { MyOl } from 'my-openlayer';
|
|
91
|
-
|
|
92
|
-
const myOl = new MyOl('map-container');
|
|
93
|
-
const eventManager = myOl.getEventManager();
|
|
94
|
-
|
|
95
|
-
// 监听点击事件
|
|
96
|
-
const clickId = eventManager.on('click', (event) => {
|
|
97
|
-
console.log('Clicked at:', event.coordinate);
|
|
98
|
-
|
|
99
|
-
if (event.feature) {
|
|
100
|
-
console.log('Clicked feature:', event.feature);
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// 监听缩放结束
|
|
105
|
-
eventManager.on('zoomend', (event) => {
|
|
106
|
-
console.log('Current zoom:', event.zoom);
|
|
107
|
-
});
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### 一次性监听
|
|
111
|
-
|
|
112
|
-
```typescript
|
|
113
|
-
// 只监听一次点击
|
|
114
|
-
eventManager.on('click', (event) => {
|
|
115
|
-
console.log('First click only');
|
|
116
|
-
}, { once: true });
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
### 带过滤条件的监听
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
// 只监听包含特定属性要素的点击
|
|
123
|
-
eventManager.on('click', (event) => {
|
|
124
|
-
console.log('Clicked special feature');
|
|
125
|
-
}, {
|
|
126
|
-
filter: (event) => {
|
|
127
|
-
if (!event.feature) return false;
|
|
128
|
-
const props = event.feature.getProperties();
|
|
129
|
-
return props.type === 'special';
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### 移除监听
|
|
135
|
-
|
|
136
|
-
```typescript
|
|
137
|
-
// 停止监听
|
|
138
|
-
eventManager.off(clickId);
|
|
139
|
-
|
|
140
|
-
// 或清除所有
|
|
141
|
-
eventManager.clear();
|
|
142
|
-
```
|
|
1
|
+
# EventManager 事件管理
|
|
2
|
+
|
|
3
|
+
`EventManager` 类用于统一管理地图事件,提供简单的 API 来监听、移除和触发地图事件。它对 OpenLayers 的原生事件系统进行了封装,提供了类型安全的事件处理。
|
|
4
|
+
|
|
5
|
+
## 导入
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { EventManager, MapEventData } from 'my-openlayer';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 接口定义
|
|
12
|
+
|
|
13
|
+
### MapEventData 接口
|
|
14
|
+
|
|
15
|
+
统一的事件数据结构,回调函数会接收此类型的参数。
|
|
16
|
+
|
|
17
|
+
| 属性名 | 类型 | 描述 |
|
|
18
|
+
| :--- | :--- | :--- |
|
|
19
|
+
| `type` | `MapEventType` | 事件类型 |
|
|
20
|
+
| `originalEvent` | `Event` | 原始 DOM 事件对象 (可选) |
|
|
21
|
+
| `coordinate` | `number[]` | 发生事件的地理坐标 (可选) |
|
|
22
|
+
| `pixel` | `Pixel` | 发生事件的屏幕像素坐标 (可选) |
|
|
23
|
+
| `features` | `FeatureLike[]` | 事件位置处的要素列表 (可选) |
|
|
24
|
+
| `feature` | `FeatureLike` | 事件位置处的第一个要素 (可选) |
|
|
25
|
+
| `zoom` | `number` | 当前地图缩放级别 (可选) |
|
|
26
|
+
| `[key: string]` | `any` | 其他自定义属性 |
|
|
27
|
+
|
|
28
|
+
### MapEventType 类型
|
|
29
|
+
|
|
30
|
+
支持的事件类型字符串:
|
|
31
|
+
`'click'` | `'dblclick'` | `'hover'` | `'moveend'` | `'zoomend'` | `'pointermove'` | `'rendercomplete'` | `'error'`
|
|
32
|
+
|
|
33
|
+
## 构造函数
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
constructor(map: Map)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
| 参数名 | 类型 | 描述 |
|
|
40
|
+
| :--- | :--- | :--- |
|
|
41
|
+
| `map` | `Map` | OpenLayers 地图实例 |
|
|
42
|
+
|
|
43
|
+
## 方法
|
|
44
|
+
|
|
45
|
+
### 监听事件
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
on(type: MapEventType, callback: EventCallback, options?: { once?: boolean; filter?: (event: MapEventData) => boolean; }): string
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
注册事件监听器。
|
|
52
|
+
|
|
53
|
+
| 参数名 | 类型 | 描述 |
|
|
54
|
+
| :--- | :--- | :--- |
|
|
55
|
+
| `type` | `MapEventType` | 事件类型 |
|
|
56
|
+
| `callback` | `EventCallback` | 事件回调函数 |
|
|
57
|
+
| `options` | `object` | 监听选项 (可选) |
|
|
58
|
+
| `options.once` | `boolean` | 是否只触发一次 |
|
|
59
|
+
| `options.filter` | `function` | 事件过滤器,返回 false 则不触发回调 |
|
|
60
|
+
|
|
61
|
+
**返回值**: `string` - 监听器 ID,用于取消监听。
|
|
62
|
+
|
|
63
|
+
### 移除监听
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
off(id: string): boolean
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
根据监听器 ID 移除事件监听。
|
|
70
|
+
|
|
71
|
+
| 参数名 | 类型 | 描述 |
|
|
72
|
+
| :--- | :--- | :--- |
|
|
73
|
+
| `id` | `string` | 监听器 ID |
|
|
74
|
+
|
|
75
|
+
**返回值**: `boolean` - 是否成功移除。
|
|
76
|
+
|
|
77
|
+
### 移除所有监听
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
clear(): void
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
移除所有已注册的事件监听器。
|
|
84
|
+
|
|
85
|
+
## 使用示例
|
|
86
|
+
|
|
87
|
+
### 基础事件监听
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { MyOl } from 'my-openlayer';
|
|
91
|
+
|
|
92
|
+
const myOl = new MyOl('map-container');
|
|
93
|
+
const eventManager = myOl.getEventManager();
|
|
94
|
+
|
|
95
|
+
// 监听点击事件
|
|
96
|
+
const clickId = eventManager.on('click', (event) => {
|
|
97
|
+
console.log('Clicked at:', event.coordinate);
|
|
98
|
+
|
|
99
|
+
if (event.feature) {
|
|
100
|
+
console.log('Clicked feature:', event.feature);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// 监听缩放结束
|
|
105
|
+
eventManager.on('zoomend', (event) => {
|
|
106
|
+
console.log('Current zoom:', event.zoom);
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 一次性监听
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
// 只监听一次点击
|
|
114
|
+
eventManager.on('click', (event) => {
|
|
115
|
+
console.log('First click only');
|
|
116
|
+
}, { once: true });
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 带过滤条件的监听
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
// 只监听包含特定属性要素的点击
|
|
123
|
+
eventManager.on('click', (event) => {
|
|
124
|
+
console.log('Clicked special feature');
|
|
125
|
+
}, {
|
|
126
|
+
filter: (event) => {
|
|
127
|
+
if (!event.feature) return false;
|
|
128
|
+
const props = event.feature.getProperties();
|
|
129
|
+
return props.type === 'special';
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 移除监听
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
// 停止监听
|
|
138
|
+
eventManager.off(clickId);
|
|
139
|
+
|
|
140
|
+
// 或清除所有
|
|
141
|
+
eventManager.clear();
|
|
142
|
+
```
|
package/docs/Line.md
CHANGED
|
@@ -1,187 +1,187 @@
|
|
|
1
|
-
# Line 线要素类
|
|
2
|
-
|
|
3
|
-
`Line` 类用于在地图上绘制线要素,支持 GeoJSON 数据加载、自定义样式和图层管理。
|
|
4
|
-
|
|
5
|
-
## 构造函数
|
|
6
|
-
|
|
7
|
-
```typescript
|
|
8
|
-
const line = new Line(map: Map);
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
- **map**: OpenLayers 地图实例。
|
|
12
|
-
|
|
13
|
-
## 接口定义
|
|
14
|
-
|
|
15
|
-
### LineOptions
|
|
16
|
-
|
|
17
|
-
配置线图层的样式、属性和行为。继承自 `BaseOptions`, `StyleOptions`, `TextOptions`。
|
|
18
|
-
|
|
19
|
-
| 属性 | 类型 | 说明 |
|
|
20
|
-
| :--- | :--- | :--- |
|
|
21
|
-
| **基础配置** | | |
|
|
22
|
-
| `layerName` | `string` | 图层名称,用于唯一标识和管理图层 |
|
|
23
|
-
| `zIndex` | `number` | 图层层级,默认自动处理 |
|
|
24
|
-
| `visible` | `boolean` | 图层是否可见,默认 `true` |
|
|
25
|
-
| `opacity` | `number` | 图层透明度 (0-1) |
|
|
26
|
-
| `fitView` | `boolean` | 添加要素后是否自动缩放视图以适应要素范围 |
|
|
27
|
-
| **样式配置** | | |
|
|
28
|
-
| `strokeColor` | `string` \| `number[]` | 线条颜色,如 `'#ff0000'` 或 `[255, 0, 0, 1]` |
|
|
29
|
-
| `strokeWidth` | `number` | 线条宽度(像素) |
|
|
30
|
-
| `lineDash` | `number[]` | 虚线模式,如 `[10, 10]` 表示 10px 实线 10px 间隔 |
|
|
31
|
-
| `lineDashOffset` | `number` | 虚线偏移量 |
|
|
32
|
-
| `style` | `Style` \| `Function` | 自定义 OpenLayers 样式或样式函数 |
|
|
33
|
-
| **文本标注** | | |
|
|
34
|
-
| `textVisible` | `boolean` | 是否显示文本标注 |
|
|
35
|
-
| `textCallBack` | `(feature) => string` | 获取文本内容的回调函数 |
|
|
36
|
-
| `textFont` | `string` | 字体样式,默认 `'12px sans-serif'` |
|
|
37
|
-
| `textFillColor` | `string` | 文本填充颜色 |
|
|
38
|
-
| `textStrokeColor` | `string` | 文本描边颜色 |
|
|
39
|
-
| `textStrokeWidth` | `number` | 文本描边宽度 |
|
|
40
|
-
| `textOffsetY` | `number` | 文本 Y 轴偏移量 |
|
|
41
|
-
| **其他** | | |
|
|
42
|
-
| `type` | `string` | 线条类型标识,会写入 Feature 的属性中 |
|
|
43
|
-
|
|
44
|
-
### MapJSONData (GeoJSON)
|
|
45
|
-
|
|
46
|
-
标准的 GeoJSON 格式数据结构。
|
|
47
|
-
|
|
48
|
-
```typescript
|
|
49
|
-
interface MapJSONData {
|
|
50
|
-
type: string; // 通常为 "FeatureCollection"
|
|
51
|
-
name?: string; // 数据集名称
|
|
52
|
-
features: FeatureData[]; // 要素数组
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
interface FeatureData {
|
|
56
|
-
type: string; // "Feature"
|
|
57
|
-
properties: any; // 属性对象
|
|
58
|
-
geometry: {
|
|
59
|
-
type: "LineString" | "MultiLineString";
|
|
60
|
-
coordinates: number[][] | number[][][];
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## 方法
|
|
66
|
-
|
|
67
|
-
### addLine
|
|
68
|
-
|
|
69
|
-
添加线要素图层。
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
addLine(data: MapJSONData, options?: LineOptions): VectorLayer<VectorSource>
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
| 参数 | 类型 | 说明 |
|
|
76
|
-
| :--- | :--- | :--- |
|
|
77
|
-
| `data` | `MapJSONData` | GeoJSON 格式的线数据 |
|
|
78
|
-
| `options` | `LineOptions` | 图层配置项 |
|
|
79
|
-
|
|
80
|
-
**返回**: 创建的 `VectorLayer` 实例。
|
|
81
|
-
|
|
82
|
-
### addLineByUrl
|
|
83
|
-
|
|
84
|
-
从 URL 加载并添加线要素图层。
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
addLineByUrl(url: string, options?: LineOptions): VectorLayer<VectorSource>
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
| 参数 | 类型 | 说明 |
|
|
91
|
-
| :--- | :--- | :--- |
|
|
92
|
-
| `url` | `string` | GeoJSON 数据的 URL 地址 |
|
|
93
|
-
| `options` | `LineOptions` | 图层配置项 |
|
|
94
|
-
|
|
95
|
-
**返回**: 创建的 `VectorLayer` 实例。
|
|
96
|
-
|
|
97
|
-
### removeLineLayer
|
|
98
|
-
|
|
99
|
-
根据图层名称移除线图层。
|
|
100
|
-
|
|
101
|
-
```typescript
|
|
102
|
-
removeLineLayer(layerName: string): void
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
| 参数 | 类型 | 说明 |
|
|
106
|
-
| :--- | :--- | :--- |
|
|
107
|
-
| `layerName` | `string` | 要移除的图层名称 |
|
|
108
|
-
|
|
109
|
-
## 使用示例
|
|
110
|
-
|
|
111
|
-
### 基础用法
|
|
112
|
-
|
|
113
|
-
```typescript
|
|
114
|
-
const lineModule = map.getLine();
|
|
115
|
-
|
|
116
|
-
// GeoJSON 数据
|
|
117
|
-
const lineData = {
|
|
118
|
-
type: 'FeatureCollection',
|
|
119
|
-
features: [
|
|
120
|
-
{
|
|
121
|
-
type: 'Feature',
|
|
122
|
-
geometry: {
|
|
123
|
-
type: 'LineString',
|
|
124
|
-
coordinates: [[119.8, 29.9], [119.9, 30.0]]
|
|
125
|
-
},
|
|
126
|
-
properties: { name: '线路1', status: 'active' }
|
|
127
|
-
}
|
|
128
|
-
]
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
// 添加红色实线
|
|
132
|
-
lineModule.addLine(lineData, {
|
|
133
|
-
layerName: 'base-line',
|
|
134
|
-
strokeColor: '#ff0000',
|
|
135
|
-
strokeWidth: 3,
|
|
136
|
-
fitView: true
|
|
137
|
-
});
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
### 虚线与标注
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
lineModule.addLine(lineData, {
|
|
144
|
-
layerName: 'dash-line',
|
|
145
|
-
strokeColor: '#0000ff',
|
|
146
|
-
strokeWidth: 2,
|
|
147
|
-
lineDash: [10, 5], // 10px实线,5px间隔
|
|
148
|
-
textVisible: true,
|
|
149
|
-
textFillColor: '#000',
|
|
150
|
-
textStrokeColor: '#fff',
|
|
151
|
-
textStrokeWidth: 2,
|
|
152
|
-
// 动态获取显示的文本
|
|
153
|
-
textCallBack: (feature) => feature.get('name')
|
|
154
|
-
});
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
### 自定义样式函数
|
|
158
|
-
|
|
159
|
-
```typescript
|
|
160
|
-
import { Style, Stroke } from 'ol/style';
|
|
161
|
-
|
|
162
|
-
lineModule.addLine(lineData, {
|
|
163
|
-
layerName: 'custom-style-line',
|
|
164
|
-
// 根据属性动态设置颜色
|
|
165
|
-
style: (feature) => {
|
|
166
|
-
const status = feature.get('properties').status;
|
|
167
|
-
const color = status === 'active' ? 'green' : 'gray';
|
|
168
|
-
|
|
169
|
-
return new Style({
|
|
170
|
-
stroke: new Stroke({
|
|
171
|
-
color: color,
|
|
172
|
-
width: 4
|
|
173
|
-
})
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
### 从接口加载
|
|
180
|
-
|
|
181
|
-
```typescript
|
|
182
|
-
lineModule.addLineByUrl('/api/lines/all.json', {
|
|
183
|
-
layerName: 'api-lines',
|
|
184
|
-
strokeColor: 'orange',
|
|
185
|
-
strokeWidth: 2
|
|
186
|
-
});
|
|
187
|
-
```
|
|
1
|
+
# Line 线要素类
|
|
2
|
+
|
|
3
|
+
`Line` 类用于在地图上绘制线要素,支持 GeoJSON 数据加载、自定义样式和图层管理。
|
|
4
|
+
|
|
5
|
+
## 构造函数
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
const line = new Line(map: Map);
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
- **map**: OpenLayers 地图实例。
|
|
12
|
+
|
|
13
|
+
## 接口定义
|
|
14
|
+
|
|
15
|
+
### LineOptions
|
|
16
|
+
|
|
17
|
+
配置线图层的样式、属性和行为。继承自 `BaseOptions`, `StyleOptions`, `TextOptions`。
|
|
18
|
+
|
|
19
|
+
| 属性 | 类型 | 说明 |
|
|
20
|
+
| :--- | :--- | :--- |
|
|
21
|
+
| **基础配置** | | |
|
|
22
|
+
| `layerName` | `string` | 图层名称,用于唯一标识和管理图层 |
|
|
23
|
+
| `zIndex` | `number` | 图层层级,默认自动处理 |
|
|
24
|
+
| `visible` | `boolean` | 图层是否可见,默认 `true` |
|
|
25
|
+
| `opacity` | `number` | 图层透明度 (0-1) |
|
|
26
|
+
| `fitView` | `boolean` | 添加要素后是否自动缩放视图以适应要素范围 |
|
|
27
|
+
| **样式配置** | | |
|
|
28
|
+
| `strokeColor` | `string` \| `number[]` | 线条颜色,如 `'#ff0000'` 或 `[255, 0, 0, 1]` |
|
|
29
|
+
| `strokeWidth` | `number` | 线条宽度(像素) |
|
|
30
|
+
| `lineDash` | `number[]` | 虚线模式,如 `[10, 10]` 表示 10px 实线 10px 间隔 |
|
|
31
|
+
| `lineDashOffset` | `number` | 虚线偏移量 |
|
|
32
|
+
| `style` | `Style` \| `Function` | 自定义 OpenLayers 样式或样式函数 |
|
|
33
|
+
| **文本标注** | | |
|
|
34
|
+
| `textVisible` | `boolean` | 是否显示文本标注 |
|
|
35
|
+
| `textCallBack` | `(feature) => string` | 获取文本内容的回调函数 |
|
|
36
|
+
| `textFont` | `string` | 字体样式,默认 `'12px sans-serif'` |
|
|
37
|
+
| `textFillColor` | `string` | 文本填充颜色 |
|
|
38
|
+
| `textStrokeColor` | `string` | 文本描边颜色 |
|
|
39
|
+
| `textStrokeWidth` | `number` | 文本描边宽度 |
|
|
40
|
+
| `textOffsetY` | `number` | 文本 Y 轴偏移量 |
|
|
41
|
+
| **其他** | | |
|
|
42
|
+
| `type` | `string` | 线条类型标识,会写入 Feature 的属性中 |
|
|
43
|
+
|
|
44
|
+
### MapJSONData (GeoJSON)
|
|
45
|
+
|
|
46
|
+
标准的 GeoJSON 格式数据结构。
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
interface MapJSONData {
|
|
50
|
+
type: string; // 通常为 "FeatureCollection"
|
|
51
|
+
name?: string; // 数据集名称
|
|
52
|
+
features: FeatureData[]; // 要素数组
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface FeatureData {
|
|
56
|
+
type: string; // "Feature"
|
|
57
|
+
properties: any; // 属性对象
|
|
58
|
+
geometry: {
|
|
59
|
+
type: "LineString" | "MultiLineString";
|
|
60
|
+
coordinates: number[][] | number[][][];
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## 方法
|
|
66
|
+
|
|
67
|
+
### addLine
|
|
68
|
+
|
|
69
|
+
添加线要素图层。
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
addLine(data: MapJSONData, options?: LineOptions): VectorLayer<VectorSource>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
| 参数 | 类型 | 说明 |
|
|
76
|
+
| :--- | :--- | :--- |
|
|
77
|
+
| `data` | `MapJSONData` | GeoJSON 格式的线数据 |
|
|
78
|
+
| `options` | `LineOptions` | 图层配置项 |
|
|
79
|
+
|
|
80
|
+
**返回**: 创建的 `VectorLayer` 实例。
|
|
81
|
+
|
|
82
|
+
### addLineByUrl
|
|
83
|
+
|
|
84
|
+
从 URL 加载并添加线要素图层。
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
addLineByUrl(url: string, options?: LineOptions): VectorLayer<VectorSource>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
| 参数 | 类型 | 说明 |
|
|
91
|
+
| :--- | :--- | :--- |
|
|
92
|
+
| `url` | `string` | GeoJSON 数据的 URL 地址 |
|
|
93
|
+
| `options` | `LineOptions` | 图层配置项 |
|
|
94
|
+
|
|
95
|
+
**返回**: 创建的 `VectorLayer` 实例。
|
|
96
|
+
|
|
97
|
+
### removeLineLayer
|
|
98
|
+
|
|
99
|
+
根据图层名称移除线图层。
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
removeLineLayer(layerName: string): void
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
| 参数 | 类型 | 说明 |
|
|
106
|
+
| :--- | :--- | :--- |
|
|
107
|
+
| `layerName` | `string` | 要移除的图层名称 |
|
|
108
|
+
|
|
109
|
+
## 使用示例
|
|
110
|
+
|
|
111
|
+
### 基础用法
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
const lineModule = map.getLine();
|
|
115
|
+
|
|
116
|
+
// GeoJSON 数据
|
|
117
|
+
const lineData = {
|
|
118
|
+
type: 'FeatureCollection',
|
|
119
|
+
features: [
|
|
120
|
+
{
|
|
121
|
+
type: 'Feature',
|
|
122
|
+
geometry: {
|
|
123
|
+
type: 'LineString',
|
|
124
|
+
coordinates: [[119.8, 29.9], [119.9, 30.0]]
|
|
125
|
+
},
|
|
126
|
+
properties: { name: '线路1', status: 'active' }
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// 添加红色实线
|
|
132
|
+
lineModule.addLine(lineData, {
|
|
133
|
+
layerName: 'base-line',
|
|
134
|
+
strokeColor: '#ff0000',
|
|
135
|
+
strokeWidth: 3,
|
|
136
|
+
fitView: true
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### 虚线与标注
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
lineModule.addLine(lineData, {
|
|
144
|
+
layerName: 'dash-line',
|
|
145
|
+
strokeColor: '#0000ff',
|
|
146
|
+
strokeWidth: 2,
|
|
147
|
+
lineDash: [10, 5], // 10px实线,5px间隔
|
|
148
|
+
textVisible: true,
|
|
149
|
+
textFillColor: '#000',
|
|
150
|
+
textStrokeColor: '#fff',
|
|
151
|
+
textStrokeWidth: 2,
|
|
152
|
+
// 动态获取显示的文本
|
|
153
|
+
textCallBack: (feature) => feature.get('name')
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### 自定义样式函数
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import { Style, Stroke } from 'ol/style';
|
|
161
|
+
|
|
162
|
+
lineModule.addLine(lineData, {
|
|
163
|
+
layerName: 'custom-style-line',
|
|
164
|
+
// 根据属性动态设置颜色
|
|
165
|
+
style: (feature) => {
|
|
166
|
+
const status = feature.get('properties').status;
|
|
167
|
+
const color = status === 'active' ? 'green' : 'gray';
|
|
168
|
+
|
|
169
|
+
return new Style({
|
|
170
|
+
stroke: new Stroke({
|
|
171
|
+
color: color,
|
|
172
|
+
width: 4
|
|
173
|
+
})
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 从接口加载
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
lineModule.addLineByUrl('/api/lines/all.json', {
|
|
183
|
+
layerName: 'api-lines',
|
|
184
|
+
strokeColor: 'orange',
|
|
185
|
+
strokeWidth: 2
|
|
186
|
+
});
|
|
187
|
+
```
|