my-openlayer 2.4.7 → 2.4.9

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.
Files changed (37) hide show
  1. package/CHANGELOG.md +208 -101
  2. package/MyOl.js +1 -1
  3. package/README.md +156 -156
  4. package/core/Point.js +21 -3
  5. package/docs/.vitepress/cache/deps/@theme_index.js +275 -0
  6. package/docs/.vitepress/cache/deps/@theme_index.js.map +7 -0
  7. package/docs/.vitepress/cache/deps/_metadata.json +40 -0
  8. package/docs/.vitepress/cache/deps/chunk-LW4I4DCF.js +12542 -0
  9. package/docs/.vitepress/cache/deps/chunk-LW4I4DCF.js.map +7 -0
  10. package/docs/.vitepress/cache/deps/chunk-Z5QSWKN2.js +9719 -0
  11. package/docs/.vitepress/cache/deps/chunk-Z5QSWKN2.js.map +7 -0
  12. package/docs/.vitepress/cache/deps/package.json +3 -0
  13. package/docs/.vitepress/cache/deps/vitepress___@vue_devtools-api.js +4505 -0
  14. package/docs/.vitepress/cache/deps/vitepress___@vue_devtools-api.js.map +7 -0
  15. package/docs/.vitepress/cache/deps/vitepress___@vueuse_core.js +583 -0
  16. package/docs/.vitepress/cache/deps/vitepress___@vueuse_core.js.map +7 -0
  17. package/docs/.vitepress/cache/deps/vue.js +343 -0
  18. package/docs/.vitepress/cache/deps/vue.js.map +7 -0
  19. package/docs/.vitepress/config.mts +57 -0
  20. package/docs/ConfigManager.md +71 -40
  21. package/docs/ErrorHandler.md +106 -45
  22. package/docs/EventManager.md +142 -52
  23. package/docs/Line.md +187 -52
  24. package/docs/MapBaseLayers.md +198 -48
  25. package/docs/MapTools.md +172 -48
  26. package/docs/MeasureHandler.md +87 -47
  27. package/docs/MyOl.md +247 -62
  28. package/docs/Point.md +165 -58
  29. package/docs/Polygon.md +241 -65
  30. package/docs/RiverLayerManager.md +187 -59
  31. package/docs/SelectHandler.md +147 -126
  32. package/docs/ValidationUtils.md +83 -38
  33. package/docs/VueTemplatePoint.md +214 -85
  34. package/docs/index.md +78 -0
  35. package/package.json +7 -4
  36. package/types.d.ts +2 -0
  37. package/AI_CONTEXT.md +0 -147
@@ -1,59 +1,187 @@
1
- # RiverLayerManager 河流图层管理类
2
-
3
- `RiverLayerManager` 专门用于处理河流数据的分级显示和样式管理。
4
-
5
- ## 方法
6
-
7
- ### 分级显示 (Zoom-based)
8
-
9
- 根据地图缩放级别自动显示不同等级的河流。
10
-
11
- - **addRiverLayersByZoom(fyRiverJson: MapJSONData, options?: RiverLayerOptions)**: 添加分级河流图层。
12
- - `options.levelCount`: 河流等级总数。
13
- - `options.zoomOffset`: 缩放级别偏移量,控制何时显示哪一级。
14
- - **addRiverLayersByZoomByUrl(url: string, options?)**: 从 URL 加载分级河流。
15
- - **showRiverLayer(show: boolean)**: 控制分级河流图层的总开关。
16
-
17
- ### 宽度分级 (Level-based Width)
18
-
19
- 根据河流等级属性渲染不同宽度的线条。
20
-
21
- - **addRiverWidthByLevel(data: MapJSONData, options?)**: 添加按等级设定宽度的河流。
22
- - **addRiverWidthByLevelByUrl(url: string, options?)**: URL 加载。
23
-
24
- ### 管理
25
-
26
- - **clearRiverLayers()**: 清除所有由该管理器创建的河流图层。
27
- - **getRiverLayerVisibility()**: 获取当前可见性状态。
28
-
29
- ## 使用示例
30
-
31
- ```typescript
32
- // 需要单独实例化,或扩展 MyOl
33
- import { RiverLayerManager } from 'my-openlayer';
34
-
35
- const riverManager = new RiverLayerManager(map.map);
36
-
37
- // 1. 添加分级河流(随缩放显示更多细节)
38
- riverManager.addRiverLayersByZoom(riverGeoJSON, {
39
- layerName: 'main-river',
40
- levelCount: 5, // 数据中包含 level 1-5
41
- zoomOffset: 8, // level 1 在 zoom > 9 (1+8) 显示
42
- strokeColor: '#0071FF',
43
- strokeWidth: 2
44
- });
45
-
46
- // 2. 控制显隐
47
- riverManager.showRiverLayer(true);
48
-
49
- // 3. 添加按等级设定宽度的河流(静态显示)
50
- riverManager.addRiverWidthByLevel(riverGeoJSON, {
51
- layerName: 'width-river',
52
- strokeColor: '#0071FF',
53
- levelWidthMap: {
54
- 1: 5, // 1级河流宽5px
55
- 2: 3,
56
- 3: 1
57
- }
58
- });
59
- ```
1
+ # RiverLayerManager 河流图层管理类
2
+
3
+ `RiverLayerManager` 专门用于处理河流数据的分级显示和样式管理。它支持基于地图缩放级别(Zoom-based)的动态分级显示,以及基于河流属性等级(Level-based)的固定线宽渲染。
4
+
5
+ ## 构造函数
6
+
7
+ ```typescript
8
+ constructor(map: Map, eventManager?: EventManager)
9
+ ```
10
+
11
+ - **参数**:
12
+ - `map` (Map): OpenLayers 地图实例。
13
+ - `eventManager` (EventManager): 可选。事件管理器实例,如果未提供则内部创建。
14
+
15
+ ## 接口定义
16
+
17
+ ### RiverLayerOptions
18
+
19
+ 河流图层配置选项,继承自 `LineOptions`。
20
+
21
+ | 属性 | 类型 | 必填 | 默认值 | 描述 |
22
+ | :--- | :--- | :--- | :--- | :--- |
23
+ | levelCount | `number` | 否 | `5` | 河流等级总数。用于分级显示循环加载。 |
24
+ | zoomOffset | `number` | 否 | `8` | 缩放级别偏移量。控制何时显示哪一级河流 (Zoom > Level + Offset)。 |
25
+ | levelWidthMap | `RiverLevelWidthMap` | 否 | 默认映射 | 河流级别与线宽的映射对象。 |
26
+ | removeExisting | `boolean` | 否 | `false` | 添加前是否删除由本管理器创建的旧图层。 |
27
+ | layerName | `string` | 否 | - | 图层名称,用于标识和清理。 |
28
+ | visible | `boolean` | 否 | `true` | 初始可见性。 |
29
+ | strokeColor | `string` | 否 | - | 线条颜色。 |
30
+ | strokeWidth | `number` | 否 | - | 基础线宽(如果未使用 levelWidthMap)。 |
31
+ | zIndex | `number` | 否 | - | 图层 Z-Index。 |
32
+ | projectionOptOptions | `any` | 否 | - | GeoJSON 读取时的投影选项。 |
33
+
34
+ ### RiverLevelWidthMap
35
+
36
+ 河流级别线宽映射接口。
37
+
38
+ ```typescript
39
+ interface RiverLevelWidthMap {
40
+ [level: number]: number;
41
+ }
42
+ ```
43
+
44
+ 默认映射 (`ConfigManager.DEFAULT_RIVER_LEVEL_WIDTH_MAP`) 通常为 `{1: 5, 2: 4, 3: 3, 4: 2, 5: 1}` 等类似结构。
45
+
46
+ ## 方法
47
+
48
+ ### 分级显示 (Zoom-based)
49
+
50
+ 根据地图缩放级别自动显示不同等级的河流。通常用于实现“缩放越大约详细”的效果。
51
+
52
+ #### addRiverLayersByZoom
53
+
54
+ 直接传递 GeoJSON 数据添加分级河流图层。
55
+
56
+ ```typescript
57
+ addRiverLayersByZoom(fyRiverJson: MapJSONData, options?: RiverLayerOptions): void
58
+ ```
59
+
60
+ - **参数**:
61
+ - `fyRiverJson`: 河流 GeoJSON 数据对象。
62
+ - `options`: 配置选项。
63
+
64
+ #### addRiverLayersByZoomByUrl
65
+
66
+ 通过 URL 加载 GeoJSON 数据并添加分级河流图层。
67
+
68
+ ```typescript
69
+ addRiverLayersByZoomByUrl(url: string, options?: RiverLayerOptions): void
70
+ ```
71
+
72
+ #### showRiverLayer
73
+
74
+ 控制分级河流图层的总开关。
75
+
76
+ ```typescript
77
+ showRiverLayer(show: boolean): void
78
+ ```
79
+
80
+ #### showRiverLayerByZoom
81
+
82
+ 手动触发根据当前缩放级别更新图层可见性(通常不需要手动调用,已绑定 `moveend` 事件)。
83
+
84
+ ```typescript
85
+ showRiverLayerByZoom(): void
86
+ ```
87
+
88
+ ### 宽度分级 (Level-based Width)
89
+
90
+ 根据河流数据中的 `level` 属性渲染不同宽度的线条,通常用于静态展示不同等级河流的粗细差异。
91
+
92
+ #### addRiverWidthByLevel
93
+
94
+ 直接传递 GeoJSON 数据添加按等级设定宽度的河流。
95
+
96
+ ```typescript
97
+ addRiverWidthByLevel(data: MapJSONData, options?: RiverLayerOptions): VectorLayer<VectorSource>
98
+ ```
99
+
100
+ - **返回值**: 创建的矢量图层。
101
+
102
+ #### addRiverWidthByLevelByUrl
103
+
104
+ 通过 URL 加载 GeoJSON 数据并添加按等级设定宽度的河流。
105
+
106
+ ```typescript
107
+ addRiverWidthByLevelByUrl(url: string, options?: RiverLayerOptions): VectorLayer<VectorSource>
108
+ ```
109
+
110
+ ### 管理与清理
111
+
112
+ #### clearRiverLayers
113
+
114
+ 清除所有由该管理器创建的分级河流图层。
115
+
116
+ ```typescript
117
+ clearRiverLayers(): void
118
+ ```
119
+
120
+ #### getRiverLayerVisibility
121
+
122
+ 获取当前分级河流图层的总显示状态。
123
+
124
+ ```typescript
125
+ getRiverLayerVisibility(): boolean
126
+ ```
127
+
128
+ #### getRiverLayers
129
+
130
+ 获取当前管理的所有分级河流图层列表。
131
+
132
+ ```typescript
133
+ getRiverLayers(): VectorLayer<VectorSource>[]
134
+ ```
135
+
136
+ #### destroy
137
+
138
+ 销毁管理器,清理所有图层和事件监听。
139
+
140
+ ```typescript
141
+ destroy(): void
142
+ ```
143
+
144
+ ## 使用示例
145
+
146
+ ```typescript
147
+ import { MyOl, RiverLayerManager } from 'my-openlayer';
148
+
149
+ const map = new MyOl('map-container');
150
+ const riverManager = new RiverLayerManager(map.map);
151
+
152
+ // 假设 riverGeoJSON 数据中每个 feature 都有 properties.level (1-5)
153
+
154
+ // 示例 1: 添加分级河流(随地图缩放自动显隐不同级别的河流)
155
+ // Level 1 在 Zoom > 9 (1+8) 显示
156
+ // Level 2 在 Zoom > 10 (2+8) 显示
157
+ // ...
158
+ riverManager.addRiverLayersByZoom(riverGeoJSON, {
159
+ layerName: 'dynamic-river',
160
+ levelCount: 5,
161
+ zoomOffset: 8,
162
+ strokeColor: '#0071FF',
163
+ strokeWidth: 2,
164
+ removeExisting: true
165
+ });
166
+
167
+ // 控制分级河流的总开关
168
+ riverManager.showRiverLayer(false); // 全部隐藏
169
+ riverManager.showRiverLayer(true); // 恢复并根据当前 Zoom 显示对应级别
170
+
171
+ // 示例 2: 添加静态河流,根据等级显示不同粗细
172
+ riverManager.addRiverWidthByLevel(riverGeoJSON, {
173
+ layerName: 'static-river',
174
+ strokeColor: '#0071FF',
175
+ levelWidthMap: {
176
+ 1: 6, // 1级河流宽 6px
177
+ 2: 5,
178
+ 3: 4,
179
+ 4: 3,
180
+ 5: 1
181
+ },
182
+ zIndex: 10
183
+ });
184
+
185
+ // 清理资源
186
+ // riverManager.destroy();
187
+ ```
@@ -1,126 +1,147 @@
1
- # SelectHandler 要素选择类
2
-
3
- `SelectHandler` 负责地图要素的交互选择,支持点击、悬停等多种模式,以及编程式选择。
4
-
5
- ## 核心特性
6
-
7
- - **独立渲染机制**:采用独立的 Select 实例渲染选中要素,确保样式完全隔离,互不干扰。
8
- - **静态样式固化**:在选中瞬间计算并固化样式,支持基于原始样式的复杂计算(如 `feature.getStyle()`),彻底避免递归调用风险。
9
- - **多选隔离**:支持不同要素同时应用完全不同的高亮样式。
10
- - **自动清理**:自动管理渲染实例的生命周期,防止内存泄漏。
11
-
12
- ## 方法
13
-
14
- ### 交互选择
15
-
16
- - **enableSelect(mode: SelectMode, options?: SelectOptions)**: 启用选择交互。
17
- - `mode`: `'click'` (点击), `'hover'` (悬停), `'ctrl'` (Ctrl+点击)。
18
- - `options`:
19
- - `multi`: 是否允许多选。
20
- - `layerFilter`: 图层过滤器(图层名称数组)。
21
- - `onSelect`: 选中回调。
22
- - `onDeselect`: 取消选中回调。
23
- - `selectStyle`: 选中时的样式。支持 `Style` 对象、数组或函数 `(feature, resolution) => Style`。**推荐使用函数**以实现基于原始样式的动态高亮。
24
-
25
- - **disableSelect()**: 禁用选择交互(停止响应用户操作,并清理交互式高亮)。
26
- - **isSelectEnabled()**: 检查是否已启用。
27
-
28
- ### 编程式选择
29
-
30
- - **selectByIds(featureIds: string[], options?)**: 根据 ID 选中要素。
31
- - **selectByProperty(propertyName: string, propertyValue: any, options?)**: 根据属性值选中要素。
32
- - **clearSelection()**: 清除当前所有选中状态(包括交互式和编程式)。
33
- - **getSelectedFeatures()**: 获取当前选中的要素列表(仅包含交互式选择的要素)。
34
-
35
- ## 使用示例
36
-
37
- ### 1. 基础用法
38
-
39
- ```typescript
40
- const selectHandler = map.getSelectHandler();
41
-
42
- // 启用点击选择
43
- selectHandler.enableSelect('click', {
44
- multi: false, // 单选
45
- layerFilter: ['target-layer'], // 仅选择特定图层的要素
46
- onSelect: (event) => {
47
- const feature = event.selected[0];
48
- console.log('选中要素:', feature.getProperties());
49
- },
50
- onDeselect: (event) => {
51
- console.log('取消选中:', event.deselected);
52
- },
53
- // 自定义选中样式(简单对象)
54
- selectStyle: new Style({
55
- image: new Circle({
56
- radius: 8,
57
- fill: new Fill({ color: 'red' }),
58
- stroke: new Stroke({ color: 'white', width: 2 })
59
- })
60
- })
61
- });
62
- ```
63
-
64
- ### 2. 高级用法:基于原始样式的动态高亮
65
-
66
- 利用函数式 `selectStyle`,可以在选中时动态获取要素的原始样式,并在此基础上修改(例如只改边框颜色,保留填充)。由于采用了**样式固化**机制,这种写法是安全的。
67
-
68
- ```typescript
69
- selectHandler.enableSelect('click', {
70
- selectStyle: (feature, resolution) => {
71
- // 获取要素的原始样式
72
- // 注意:feature.getStyle() 可能返回 Style 对象、数组或函数,需要自行处理归一化
73
- const originStyleLike = feature.getStyle();
74
- let originStyle = originStyleLike;
75
-
76
- // 如果原始样式是函数,执行它获取 Style 对象
77
- if (typeof originStyleLike === 'function') {
78
- originStyle = originStyleLike(feature, resolution);
79
- }
80
-
81
- // 归一化为数组
82
- const styles = Array.isArray(originStyle) ? originStyle : [originStyle];
83
-
84
- // 克隆并修改样式(例如:将边框改为青色)
85
- return styles.map(s => {
86
- const clone = s.clone();
87
- const stroke = clone.getStroke();
88
- if (stroke) {
89
- stroke.setColor('#00FFFF');
90
- stroke.setWidth(3);
91
- } else {
92
- // 如果没有边框,添加一个
93
- clone.setStroke(new Stroke({ color: '#00FFFF', width: 3 }));
94
- }
95
- return clone;
96
- });
97
- }
98
- });
99
- ```
100
-
101
- ### 3. 编程式选择(带独立样式)
102
-
103
- 编程式选择支持传入特定的样式,该样式将独立于主交互样式生效,且支持多要素使用不同样式。
104
-
105
- ```typescript
106
- // 选中 ID 为 'feature-1' 的要素,并用红色高亮
107
- selectHandler.selectByIds(['feature-1'], {
108
- selectStyle: new Style({ stroke: new Stroke({ color: 'red', width: 4 }) }),
109
- fitView: true // 自动定位
110
- });
111
-
112
- // 同时选中 ID 为 'feature-2' 的要素,但用蓝色高亮(互不冲突)
113
- selectHandler.selectByIds(['feature-2'], {
114
- selectStyle: new Style({ stroke: new Stroke({ color: 'blue', width: 4 }) })
115
- });
116
- ```
117
-
118
- ### 4. 禁用与清理
119
-
120
- ```typescript
121
- // 禁用用户交互(停止响应点击,但保留编程式高亮)
122
- selectHandler.disableSelect();
123
-
124
- // 清除所有选择(包括交互式和编程式的高亮)
125
- selectHandler.clearSelection();
126
- ```
1
+ # SelectHandler
2
+
3
+ `SelectHandler` 类用于处理地图上的要素选择交互,支持点击、悬停、Ctrl+点击等多种模式,并支持编程式选择。
4
+
5
+ ## 构造函数
6
+
7
+ ```typescript
8
+ constructor(map: Map)
9
+ ```
10
+
11
+ - **map**: OpenLayers 地图实例。
12
+
13
+ ## 接口定义
14
+
15
+ ### SelectOptions
16
+
17
+ | 属性名 | 类型 | 说明 |
18
+ | :--- | :--- | :--- |
19
+ | multi | `boolean` | 是否支持多选,默认 `false` |
20
+ | layerFilter | `string[]` | 图层过滤器,指定可选择的图层名称列表 |
21
+ | featureFilter | `(feature: FeatureLike) => boolean` | 要素过滤器函数 |
22
+ | hitTolerance | `number` | 点击容差(像素),默认为 0 |
23
+ | selectStyle | `Style \| Style[] \| ((feature: FeatureLike) => Style \| Style[])` | 选中要素的样式 |
24
+ | onSelect | `(event: SelectCallbackEvent) => void` | 选中要素时的回调函数 |
25
+ | onDeselect | `(event: SelectCallbackEvent) => void` | 取消选中要素时的回调函数 |
26
+
27
+ ### ProgrammaticSelectOptions
28
+
29
+ | 属性名 | 类型 | 说明 |
30
+ | :--- | :--- | :--- |
31
+ | layerName | `string` | 图层名称,指定在哪个图层中选择要素 |
32
+ | selectStyle | `Style \| Style[] \| ((feature: FeatureLike) => Style \| Style[])` | 自定义选中样式(仅作用于此次选择) |
33
+ | fitView | `boolean` | 是否定位至选中要素,默认 `false` |
34
+ | fitDuration | `number` | 定位动画持续时间(毫秒),默认 500 |
35
+ | fitPadding | `number` | 定位时的边距(像素),默认 100 |
36
+
37
+ ### SelectMode
38
+
39
+ ```typescript
40
+ type SelectMode = 'click' | 'hover' | 'ctrl';
41
+ ```
42
+
43
+ ## 方法
44
+
45
+ ### enableSelect
46
+
47
+ 启用要素选择。
48
+
49
+ ```typescript
50
+ enableSelect(mode: SelectMode = 'click', options?: SelectOptions): this
51
+ ```
52
+
53
+ - **mode**: 选择模式。
54
+ - **options**: 配置选项。
55
+ - **返回值**: `SelectHandler` 实例,支持链式调用。
56
+
57
+ ### disableSelect
58
+
59
+ 禁用要素选择。
60
+
61
+ ```typescript
62
+ disableSelect(): this
63
+ ```
64
+
65
+ - **返回值**: `SelectHandler` 实例。
66
+
67
+ ### clearSelection
68
+
69
+ 清除所有选择(包括交互式和编程式)。
70
+
71
+ ```typescript
72
+ clearSelection(): this
73
+ ```
74
+
75
+ - **返回值**: `SelectHandler` 实例。
76
+
77
+ ### selectByIds
78
+
79
+ 通过要素 ID 编程式选择要素。
80
+
81
+ ```typescript
82
+ selectByIds(featureIds: string[], options?: ProgrammaticSelectOptions): this
83
+ ```
84
+
85
+ - **featureIds**: 要素 ID 数组。
86
+ - **options**: 编程式选择选项。
87
+ - **返回值**: `SelectHandler` 实例。
88
+
89
+ ### selectByProperty
90
+
91
+ 通过属性编程式选择要素。
92
+
93
+ ```typescript
94
+ selectByProperty(propertyName: string, propertyValue: any, options?: ProgrammaticSelectOptions): this
95
+ ```
96
+
97
+ - **propertyName**: 属性名。
98
+ - **propertyValue**: 属性值。
99
+ - **options**: 编程式选择选项。
100
+ - **返回值**: `SelectHandler` 实例。
101
+
102
+ ## 使用示例
103
+
104
+ ### 启用点击选择
105
+
106
+ ```typescript
107
+ import { SelectHandler } from 'my-openlayers';
108
+
109
+ const selectHandler = new SelectHandler(map);
110
+
111
+ // 启用点击选择,仅针对 'cities' 图层
112
+ selectHandler.enableSelect('click', {
113
+ layerFilter: ['cities'],
114
+ onSelect: (event) => {
115
+ console.log('Selected:', event.selected);
116
+ },
117
+ onDeselect: (event) => {
118
+ console.log('Deselected:', event.deselected);
119
+ }
120
+ });
121
+ ```
122
+
123
+ ### 启用悬停高亮
124
+
125
+ ```typescript
126
+ selectHandler.enableSelect('hover', {
127
+ selectStyle: new Style({
128
+ stroke: new Stroke({ color: 'yellow', width: 4 })
129
+ })
130
+ });
131
+ ```
132
+
133
+ ### 编程式选择并定位
134
+
135
+ ```typescript
136
+ // 选择 ID 为 'beijing' 的要素并自动定位
137
+ selectHandler.selectByIds(['beijing'], {
138
+ fitView: true,
139
+ fitPadding: 50
140
+ });
141
+ ```
142
+
143
+ ### 清除选择
144
+
145
+ ```typescript
146
+ selectHandler.clearSelection();
147
+ ```
@@ -1,38 +1,83 @@
1
- # ValidationUtils 验证工具类
2
-
3
- `ValidationUtils` 提供了一系列静态方法用于参数校验。
4
-
5
- ## 方法
6
-
7
- - **isValidCoordinate(lng, lat)**: 检查坐标是否有效(数字且在范围内)。
8
- - **validateLngLat(lng, lat)**: 简化的经纬度检查。
9
- - **validateLayerName(name)**: 验证图层名称非空。
10
- - **validateGeoJSONData(data)**: 验证 GeoJSON 数据格式。
11
- - **isValidColor(color)**: 验证颜色字符串格式。
12
- - **validateVueParams(...)**: 验证 Vue 组件点位所需的参数。
13
-
14
- ## 使用示例
15
-
16
- ```typescript
17
- import { ValidationUtils } from 'my-openlayer';
18
-
19
- // 1. 验证坐标
20
- const lng = 120.1;
21
- const lat = 30.2;
22
-
23
- if (ValidationUtils.isValidCoordinate(lng, lat)) {
24
- console.log('坐标有效');
25
- } else {
26
- console.error('坐标无效');
27
- }
28
-
29
- // 2. 验证颜色
30
- if (ValidationUtils.isValidColor('#ff0000')) {
31
- // ...
32
- }
33
-
34
- // 3. 验证 GeoJSON
35
- if (ValidationUtils.validateGeoJSONData(jsonData)) {
36
- // 安全地加载数据
37
- }
38
- ```
1
+ # ValidationUtils 验证工具
2
+
3
+ `ValidationUtils` 提供了一系列静态方法,用于验证地图操作中的各种参数和数据,确保数据的完整性和类型正确性。
4
+
5
+ ## 导入
6
+
7
+ ```typescript
8
+ import { ValidationUtils } from 'my-openlayer';
9
+ ```
10
+
11
+ ## 静态方法
12
+
13
+ ### 坐标验证
14
+
15
+ | 方法名 | 参数 | 返回值 | 描述 |
16
+ | :--- | :--- | :--- | :--- |
17
+ | `isValidCoordinate(longitude, latitude)` | `longitude: number`<br>`latitude: number` | `boolean` | 验证经纬度坐标是否有效(数字且在合法范围内) |
18
+ | `validateCoordinate(longitude, latitude)` | `longitude: number`<br>`latitude: number` | `void` | 验证经纬度,无效时抛出异常 |
19
+
20
+ ### 数据验证
21
+
22
+ | 方法名 | 参数 | 返回值 | 描述 |
23
+ | :--- | :--- | :--- | :--- |
24
+ | `validatePointData(pointData)` | `pointData: any[]` | `boolean` | 验证点位数据数组是否有效 |
25
+ | `validateOptions(options)` | `options: any` | `boolean` | 验证配置选项是否为对象 |
26
+ | `validateMaskData(data)` | `data: any` | `void` | 验证遮罩数据,无效时抛出异常 |
27
+
28
+ ### 对象/实例验证
29
+
30
+ | 方法名 | 参数 | 返回值 | 描述 |
31
+ | :--- | :--- | :--- | :--- |
32
+ | `validateMapInstance(map)` | `map: any` | `void` | 验证 OpenLayers 地图实例是否存在 |
33
+ | `validateElementId(id)` | `id: string` | `boolean` | 验证 DOM 元素 ID 是否存在 |
34
+ | `validateVueParams(pointInfoList, template, Vue)` | `pointInfoList: any[]`<br>`template: any`<br>`Vue: any` | `boolean` | 验证 Vue 组件相关参数 |
35
+
36
+ ### 通用验证
37
+
38
+ | 方法名 | 参数 | 返回值 | 描述 |
39
+ | :--- | :--- | :--- | :--- |
40
+ | `validateRequired(value, message)` | `value: any`<br>`message: string` | `void` | 验证值是否存在,否则抛出带消息的异常 |
41
+ | `validateType(value, expectedType, message)` | `value: any`<br>`expectedType: string`<br>`message: string` | `void` | 验证值类型,否则抛出带消息的异常 |
42
+
43
+ ## 使用示例
44
+
45
+ ### 验证坐标
46
+
47
+ ```typescript
48
+ import { ValidationUtils } from 'my-openlayer';
49
+
50
+ const lng = 120.5;
51
+ const lat = 30.5;
52
+
53
+ if (ValidationUtils.isValidCoordinate(lng, lat)) {
54
+ // 坐标有效,执行操作
55
+ map.getView().setCenter([lng, lat]);
56
+ } else {
57
+ console.error('Invalid coordinates');
58
+ }
59
+ ```
60
+
61
+ ### 验证必填参数
62
+
63
+ ```typescript
64
+ function initLayer(map, options) {
65
+ try {
66
+ ValidationUtils.validateMapInstance(map);
67
+ ValidationUtils.validateRequired(options, 'Options are required');
68
+
69
+ // 初始化图层
70
+ } catch (error) {
71
+ console.error('Initialization failed:', error.message);
72
+ }
73
+ }
74
+ ```
75
+
76
+ ### 验证 Vue 组件参数
77
+
78
+ ```typescript
79
+ if (ValidationUtils.validateVueParams(points, MyComponent, Vue)) {
80
+ // 创建 Vue 覆盖物
81
+ new VueTemplatePoint(map).addVueTemplatePoint(points, MyComponent);
82
+ }
83
+ ```