my-openlayer 0.1.18 → 1.0.1
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/README.md +1114 -213
- package/dist/MyOl.d.ts +99 -31
- package/dist/MyOl.js +338 -106
- package/dist/core/ConfigManager.d.ts +88 -0
- package/dist/core/ConfigManager.js +112 -0
- package/dist/core/DomPoint.d.ts +163 -14
- package/dist/core/DomPoint.js +378 -26
- package/dist/core/EventManager.d.ts +131 -0
- package/dist/core/EventManager.js +257 -0
- package/dist/core/Line.d.ts +99 -12
- package/dist/core/Line.js +216 -57
- package/dist/core/MapBaseLayers.d.ts +187 -19
- package/dist/core/MapBaseLayers.js +453 -122
- package/dist/core/MapTools.d.ts +77 -7
- package/dist/core/MapTools.js +264 -65
- package/dist/core/MeasureHandler.d.ts +13 -6
- package/dist/core/MeasureHandler.js +43 -27
- package/dist/core/Point.d.ts +51 -5
- package/dist/core/Point.js +181 -97
- package/dist/core/Polygon.d.ts +74 -22
- package/dist/core/Polygon.js +294 -125
- package/dist/index.d.ts +18 -10
- package/dist/index.js +17 -10
- package/dist/types.d.ts +200 -14
- package/dist/types.js +11 -1
- package/dist/utils/ErrorHandler.d.ts +102 -0
- package/dist/utils/ErrorHandler.js +191 -0
- package/dist/utils/ValidationUtils.d.ts +162 -0
- package/dist/utils/ValidationUtils.js +308 -0
- package/package.json +5 -6
package/dist/core/MapTools.d.ts
CHANGED
|
@@ -5,15 +5,31 @@ import VectorSource from "ol/source/Vector";
|
|
|
5
5
|
import BaseLayer from "ol/layer/Base";
|
|
6
6
|
import ImageLayer from "ol/layer/Image";
|
|
7
7
|
import ImageSource from "ol/source/Image";
|
|
8
|
+
import { EventManager, MapEventData } from "./EventManager";
|
|
9
|
+
/**
|
|
10
|
+
* 地图工具类
|
|
11
|
+
* 提供地图的基础操作功能
|
|
12
|
+
*/
|
|
8
13
|
export default class MapTools {
|
|
9
14
|
private readonly map;
|
|
15
|
+
private eventManager;
|
|
16
|
+
private errorHandler;
|
|
10
17
|
constructor(map: Map);
|
|
11
18
|
/**
|
|
12
19
|
* 根据名称获取图层
|
|
13
20
|
* @param layerName 图层名称
|
|
21
|
+
* @returns 图层数组
|
|
22
|
+
* @throws 当参数无效时抛出错误
|
|
14
23
|
*/
|
|
15
|
-
getLayerByLayerName(layerName: string | string[]): (
|
|
16
|
-
|
|
24
|
+
getLayerByLayerName(layerName: string | string[]): (VectorLayer<VectorSource> | BaseLayer | ImageLayer<ImageSource>)[];
|
|
25
|
+
/**
|
|
26
|
+
* 根据图层名称获取图层
|
|
27
|
+
* @param map 地图实例
|
|
28
|
+
* @param layerName 图层名称
|
|
29
|
+
* @returns 图层数组
|
|
30
|
+
* @throws 当参数无效时抛出错误
|
|
31
|
+
*/
|
|
32
|
+
static getLayerByLayerName(map: Map, layerName: string | string[]): (VectorLayer<VectorSource> | BaseLayer | ImageLayer<ImageSource>)[];
|
|
17
33
|
/**
|
|
18
34
|
* 设置地图裁剪
|
|
19
35
|
*/
|
|
@@ -21,29 +37,83 @@ export default class MapTools {
|
|
|
21
37
|
/**
|
|
22
38
|
* 移除图层
|
|
23
39
|
* @param layerName 图层名称
|
|
40
|
+
* @throws 当参数无效时抛出错误
|
|
24
41
|
*/
|
|
25
42
|
removeLayer(layerName: string | string[]): void;
|
|
26
43
|
/**
|
|
27
|
-
*
|
|
44
|
+
* 移除图层(静态方法,兼容性保留)
|
|
28
45
|
* @param map 地图对象
|
|
29
46
|
* @param layerName 图层名称
|
|
47
|
+
* @throws 当参数无效时抛出错误
|
|
30
48
|
*/
|
|
31
49
|
static removeLayer(map: Map, layerName: string | string[]): void;
|
|
50
|
+
/**
|
|
51
|
+
* 设置图层可见性
|
|
52
|
+
* @param layerName 图层名称
|
|
53
|
+
* @param visible 是否可见
|
|
54
|
+
* @throws 当参数无效时抛出错误
|
|
55
|
+
*/
|
|
32
56
|
setLayerVisible(layerName: string, visible: boolean): void;
|
|
33
57
|
/**
|
|
34
58
|
* 设置图层可见性
|
|
35
|
-
* @param map
|
|
59
|
+
* @param map 地图实例
|
|
36
60
|
* @param layerName 图层名称
|
|
37
61
|
* @param visible 是否可见
|
|
62
|
+
* @throws 当参数无效时抛出错误
|
|
38
63
|
*/
|
|
39
64
|
static setLayerVisible: (map: Map, layerName: string, visible: boolean) => void;
|
|
40
|
-
mapOnEvent(eventType: EventType, callback: (feature?: any, e?: any) => void, clickType?: 'point' | 'line' | 'polygon' | undefined): void;
|
|
41
65
|
/**
|
|
42
66
|
* 地图监听事件
|
|
43
|
-
* @param map
|
|
44
67
|
* @param eventType 事件类型
|
|
45
|
-
* @param clickType 点击类型
|
|
46
68
|
* @param callback 回调函数
|
|
69
|
+
* @param options 事件选项
|
|
70
|
+
* @returns 事件监听器ID
|
|
71
|
+
* @throws 当参数无效时抛出错误
|
|
72
|
+
*/
|
|
73
|
+
mapOnEvent(eventType: EventType, callback: (feature?: any, e?: any) => void, options?: {
|
|
74
|
+
clickType?: 'point' | 'line' | 'polygon';
|
|
75
|
+
once?: boolean;
|
|
76
|
+
filter?: (event: MapEventData) => boolean;
|
|
77
|
+
}): string;
|
|
78
|
+
/**
|
|
79
|
+
* 地图监听事件(静态方法,兼容性保留)
|
|
80
|
+
* @param map 地图实例
|
|
81
|
+
* @param eventType 事件类型
|
|
82
|
+
* @param callback 回调函数
|
|
83
|
+
* @param clickType 点击类型
|
|
84
|
+
* @throws 当参数无效时抛出错误
|
|
85
|
+
* @deprecated 推荐使用实例方法 mapOnEvent
|
|
47
86
|
*/
|
|
48
87
|
static mapOnEvent(map: Map, eventType: EventType, callback: (feature?: any, e?: any) => void, clickType?: 'point' | 'line' | 'polygon'): void;
|
|
88
|
+
/**
|
|
89
|
+
* 兼容性方法:使用传统方式注册事件
|
|
90
|
+
* @private
|
|
91
|
+
*/
|
|
92
|
+
private registerEventWithManager;
|
|
93
|
+
/**
|
|
94
|
+
* 转换事件类型
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
private convertToMapEventType;
|
|
98
|
+
/**
|
|
99
|
+
* 移除事件监听器
|
|
100
|
+
* @param listenerId 监听器ID
|
|
101
|
+
* @returns 是否成功移除
|
|
102
|
+
*/
|
|
103
|
+
removeEventListener(listenerId: string): boolean;
|
|
104
|
+
/**
|
|
105
|
+
* 移除指定类型的所有事件监听器
|
|
106
|
+
* @param eventType 事件类型
|
|
107
|
+
*/
|
|
108
|
+
removeAllEventListeners(eventType?: EventType): void;
|
|
109
|
+
/**
|
|
110
|
+
* 获取 EventManager 实例
|
|
111
|
+
* @returns EventManager 实例
|
|
112
|
+
*/
|
|
113
|
+
getEventManager(): EventManager;
|
|
114
|
+
/**
|
|
115
|
+
* 获取地图实例
|
|
116
|
+
* @returns 地图实例
|
|
117
|
+
*/
|
|
118
|
+
getMap(): Map;
|
|
49
119
|
}
|
package/dist/core/MapTools.js
CHANGED
|
@@ -4,35 +4,69 @@ import VectorSource from "ol/source/Vector";
|
|
|
4
4
|
import GeoJSON from "ol/format/GeoJSON";
|
|
5
5
|
import { Fill, Style } from "ol/style";
|
|
6
6
|
import { getVectorContext } from "ol/render";
|
|
7
|
+
import { EventManager } from "./EventManager";
|
|
8
|
+
import { ErrorHandler, ErrorType } from "../utils/ErrorHandler";
|
|
9
|
+
import { ValidationUtils } from "../utils/ValidationUtils";
|
|
10
|
+
/**
|
|
11
|
+
* 地图工具类
|
|
12
|
+
* 提供地图的基础操作功能
|
|
13
|
+
*/
|
|
7
14
|
class MapTools {
|
|
8
15
|
constructor(map) {
|
|
9
|
-
this.
|
|
16
|
+
this.errorHandler = ErrorHandler.getInstance();
|
|
17
|
+
try {
|
|
18
|
+
ValidationUtils.validateMap(map);
|
|
19
|
+
this.map = map;
|
|
20
|
+
this.eventManager = new EventManager(map);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
this.errorHandler.createAndHandleError(`Failed to initialize MapTools: ${error}`, ErrorType.MAP_ERROR, { map, error });
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
10
26
|
}
|
|
11
27
|
/**
|
|
12
28
|
* 根据名称获取图层
|
|
13
29
|
* @param layerName 图层名称
|
|
30
|
+
* @returns 图层数组
|
|
31
|
+
* @throws 当参数无效时抛出错误
|
|
14
32
|
*/
|
|
15
33
|
getLayerByLayerName(layerName) {
|
|
16
|
-
if (!this.map)
|
|
17
|
-
|
|
34
|
+
if (!this.map) {
|
|
35
|
+
throw new Error('Map instance is not available');
|
|
36
|
+
}
|
|
18
37
|
return MapTools.getLayerByLayerName(this.map, layerName);
|
|
19
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* 根据图层名称获取图层
|
|
41
|
+
* @param map 地图实例
|
|
42
|
+
* @param layerName 图层名称
|
|
43
|
+
* @returns 图层数组
|
|
44
|
+
* @throws 当参数无效时抛出错误
|
|
45
|
+
*/
|
|
20
46
|
static getLayerByLayerName(map, layerName) {
|
|
47
|
+
ValidationUtils.validateMap(map);
|
|
48
|
+
ValidationUtils.validateLayerNameParam(layerName);
|
|
21
49
|
const targetLayer = [];
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
|
|
50
|
+
try {
|
|
51
|
+
const layers = map.getLayers().getArray();
|
|
52
|
+
layers.forEach((layer) => {
|
|
53
|
+
const _layerName = layer.get('layerName');
|
|
54
|
+
if (typeof layerName === "string") {
|
|
55
|
+
if (_layerName && _layerName === layerName) {
|
|
56
|
+
targetLayer.push(layer);
|
|
57
|
+
}
|
|
28
58
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
59
|
+
else {
|
|
60
|
+
if (_layerName && layerName.includes(_layerName)) {
|
|
61
|
+
targetLayer.push(layer);
|
|
62
|
+
}
|
|
33
63
|
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
console.error('Error getting layers:', error);
|
|
68
|
+
throw new Error('Failed to retrieve layers from map');
|
|
69
|
+
}
|
|
36
70
|
return targetLayer;
|
|
37
71
|
}
|
|
38
72
|
/**
|
|
@@ -72,84 +106,249 @@ class MapTools {
|
|
|
72
106
|
/**
|
|
73
107
|
* 移除图层
|
|
74
108
|
* @param layerName 图层名称
|
|
109
|
+
* @throws 当参数无效时抛出错误
|
|
75
110
|
*/
|
|
76
111
|
removeLayer(layerName) {
|
|
77
|
-
if (!this.map)
|
|
78
|
-
|
|
79
|
-
|
|
112
|
+
if (!this.map) {
|
|
113
|
+
throw new Error('Map instance is not available');
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
const layers = this.getLayerByLayerName(layerName);
|
|
117
|
+
layers.forEach(layer => {
|
|
118
|
+
this.map.removeLayer(layer);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
console.error('Error removing layers:', error);
|
|
123
|
+
throw new Error('Failed to remove layers from map');
|
|
124
|
+
}
|
|
80
125
|
}
|
|
81
126
|
/**
|
|
82
|
-
*
|
|
127
|
+
* 移除图层(静态方法,兼容性保留)
|
|
83
128
|
* @param map 地图对象
|
|
84
129
|
* @param layerName 图层名称
|
|
130
|
+
* @throws 当参数无效时抛出错误
|
|
85
131
|
*/
|
|
86
132
|
static removeLayer(map, layerName) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
133
|
+
if (!map) {
|
|
134
|
+
throw new Error('Map instance is required');
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const layers = MapTools.getLayerByLayerName(map, layerName);
|
|
138
|
+
layers.forEach(layer => {
|
|
139
|
+
map.removeLayer(layer);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
console.error('Error removing layers:', error);
|
|
144
|
+
throw new Error('Failed to remove layers from map');
|
|
145
|
+
}
|
|
91
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* 设置图层可见性
|
|
149
|
+
* @param layerName 图层名称
|
|
150
|
+
* @param visible 是否可见
|
|
151
|
+
* @throws 当参数无效时抛出错误
|
|
152
|
+
*/
|
|
92
153
|
setLayerVisible(layerName, visible) {
|
|
93
|
-
if (!this.map)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
154
|
+
if (!this.map) {
|
|
155
|
+
throw new Error('Map instance is not available');
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
const layers = this.getLayerByLayerName(layerName);
|
|
159
|
+
layers.forEach(layer => {
|
|
160
|
+
layer.setVisible(visible);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
console.error('Error setting layer visibility:', error);
|
|
165
|
+
throw new Error('Failed to set layer visibility');
|
|
166
|
+
}
|
|
101
167
|
}
|
|
102
168
|
/**
|
|
103
169
|
* 地图监听事件
|
|
104
|
-
* @param map
|
|
105
170
|
* @param eventType 事件类型
|
|
106
|
-
* @param clickType 点击类型
|
|
107
171
|
* @param callback 回调函数
|
|
172
|
+
* @param options 事件选项
|
|
173
|
+
* @returns 事件监听器ID
|
|
174
|
+
* @throws 当参数无效时抛出错误
|
|
175
|
+
*/
|
|
176
|
+
mapOnEvent(eventType, callback, options) {
|
|
177
|
+
try {
|
|
178
|
+
ErrorHandler.validateMap(this.map);
|
|
179
|
+
if (!eventType) {
|
|
180
|
+
throw new Error('Event type is required');
|
|
181
|
+
}
|
|
182
|
+
if (typeof callback !== 'function') {
|
|
183
|
+
throw new Error('Callback must be a function');
|
|
184
|
+
}
|
|
185
|
+
return this.registerEventWithManager(eventType, callback, options);
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
this.errorHandler.createAndHandleError(`Failed to register map event: ${error}`, ErrorType.COMPONENT_ERROR, { eventType, callback, options, error });
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* 地图监听事件(静态方法,兼容性保留)
|
|
194
|
+
* @param map 地图实例
|
|
195
|
+
* @param eventType 事件类型
|
|
196
|
+
* @param callback 回调函数
|
|
197
|
+
* @param clickType 点击类型
|
|
198
|
+
* @throws 当参数无效时抛出错误
|
|
199
|
+
* @deprecated 推荐使用实例方法 mapOnEvent
|
|
108
200
|
*/
|
|
109
201
|
static mapOnEvent(map, eventType, callback, clickType) {
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
202
|
+
const errorHandler = ErrorHandler.getInstance();
|
|
203
|
+
try {
|
|
204
|
+
ErrorHandler.validateMap(map);
|
|
205
|
+
if (!eventType) {
|
|
206
|
+
throw new Error('Event type is required');
|
|
207
|
+
}
|
|
208
|
+
if (typeof callback !== 'function') {
|
|
209
|
+
throw new Error('Callback must be a function');
|
|
210
|
+
}
|
|
211
|
+
const eventManager = new EventManager(map);
|
|
212
|
+
const mapTools = new MapTools(map);
|
|
213
|
+
mapTools.registerEventWithManager(eventType, callback, { clickType });
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
errorHandler.createAndHandleError(`Failed to register static map event: ${error}`, ErrorType.COMPONENT_ERROR, { map, eventType, callback, clickType, error });
|
|
217
|
+
throw error;
|
|
125
218
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* 兼容性方法:使用传统方式注册事件
|
|
222
|
+
* @private
|
|
223
|
+
*/
|
|
224
|
+
registerEventWithManager(eventType, callback, options) {
|
|
225
|
+
const mapEventType = this.convertToMapEventType(eventType);
|
|
226
|
+
const eventCallback = (event) => {
|
|
227
|
+
try {
|
|
228
|
+
if (eventType === 'click') {
|
|
229
|
+
const feature = event.feature;
|
|
230
|
+
const extraData = {
|
|
231
|
+
features: event.features || [],
|
|
232
|
+
pixel: event.pixel
|
|
233
|
+
};
|
|
234
|
+
// 应用点击类型过滤
|
|
235
|
+
if (options?.clickType && feature) {
|
|
236
|
+
const geometryType = feature.getGeometry()?.getType();
|
|
237
|
+
const clickTypeMap = {
|
|
238
|
+
point: ['Point', 'MultiPoint'],
|
|
239
|
+
line: ['LineString', 'MultiLineString'],
|
|
240
|
+
polygon: ['Polygon', 'MultiPolygon']
|
|
241
|
+
};
|
|
242
|
+
if (geometryType && !clickTypeMap[options.clickType].includes(geometryType)) {
|
|
243
|
+
return; // 不符合点击类型过滤条件
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
callback(feature, extraData);
|
|
131
247
|
}
|
|
132
|
-
|
|
248
|
+
else if (eventType === 'moveend') {
|
|
249
|
+
callback(event.zoom);
|
|
250
|
+
}
|
|
251
|
+
else if (eventType === 'hover') {
|
|
252
|
+
callback({
|
|
253
|
+
features: event.features || [],
|
|
254
|
+
pixel: event.pixel
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
this.errorHandler.createAndHandleError(`Error in event callback: ${error}`, ErrorType.COMPONENT_ERROR, { eventType, event, error });
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
return this.eventManager.on(mapEventType, eventCallback, {
|
|
263
|
+
once: options?.once,
|
|
264
|
+
filter: options?.filter
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* 转换事件类型
|
|
269
|
+
* @private
|
|
270
|
+
*/
|
|
271
|
+
convertToMapEventType(eventType) {
|
|
272
|
+
const typeMap = {
|
|
273
|
+
'click': 'click',
|
|
274
|
+
'hover': 'hover',
|
|
275
|
+
'moveend': 'moveend'
|
|
276
|
+
};
|
|
277
|
+
return typeMap[eventType] || 'click';
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* 移除事件监听器
|
|
281
|
+
* @param listenerId 监听器ID
|
|
282
|
+
* @returns 是否成功移除
|
|
283
|
+
*/
|
|
284
|
+
removeEventListener(listenerId) {
|
|
285
|
+
try {
|
|
286
|
+
return this.eventManager.off(listenerId);
|
|
133
287
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const features = map.getFeaturesAtPixel(pixel);
|
|
138
|
-
callback({ features, pixel });
|
|
139
|
-
});
|
|
288
|
+
catch (error) {
|
|
289
|
+
this.errorHandler.createAndHandleError(`Failed to remove event listener: ${error}`, ErrorType.COMPONENT_ERROR, { listenerId, error });
|
|
290
|
+
return false;
|
|
140
291
|
}
|
|
141
292
|
}
|
|
293
|
+
/**
|
|
294
|
+
* 移除指定类型的所有事件监听器
|
|
295
|
+
* @param eventType 事件类型
|
|
296
|
+
*/
|
|
297
|
+
removeAllEventListeners(eventType) {
|
|
298
|
+
try {
|
|
299
|
+
if (eventType) {
|
|
300
|
+
const mapEventType = this.convertToMapEventType(eventType);
|
|
301
|
+
this.eventManager.offAll(mapEventType);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
this.eventManager.clear();
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
this.errorHandler.createAndHandleError(`Failed to remove event listeners: ${error}`, ErrorType.COMPONENT_ERROR, { eventType, error });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* 获取 EventManager 实例
|
|
313
|
+
* @returns EventManager 实例
|
|
314
|
+
*/
|
|
315
|
+
getEventManager() {
|
|
316
|
+
return this.eventManager;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* 获取地图实例
|
|
320
|
+
* @returns 地图实例
|
|
321
|
+
*/
|
|
322
|
+
getMap() {
|
|
323
|
+
return this.map;
|
|
324
|
+
}
|
|
142
325
|
}
|
|
143
326
|
/**
|
|
144
327
|
* 设置图层可见性
|
|
145
|
-
* @param map
|
|
328
|
+
* @param map 地图实例
|
|
146
329
|
* @param layerName 图层名称
|
|
147
330
|
* @param visible 是否可见
|
|
331
|
+
* @throws 当参数无效时抛出错误
|
|
148
332
|
*/
|
|
149
333
|
MapTools.setLayerVisible = (map, layerName, visible) => {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
334
|
+
if (!map) {
|
|
335
|
+
throw new Error('Map instance is required');
|
|
336
|
+
}
|
|
337
|
+
if (typeof layerName !== 'string') {
|
|
338
|
+
throw new Error('Layer name must be a string');
|
|
339
|
+
}
|
|
340
|
+
if (typeof visible !== 'boolean') {
|
|
341
|
+
throw new Error('Visible parameter must be a boolean');
|
|
342
|
+
}
|
|
343
|
+
try {
|
|
344
|
+
const layers = MapTools.getLayerByLayerName(map, layerName);
|
|
345
|
+
layers.forEach(layer => {
|
|
346
|
+
layer.setVisible(visible);
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
catch (error) {
|
|
350
|
+
console.error('Error setting layer visibility:', error);
|
|
351
|
+
throw new Error('Failed to set layer visibility');
|
|
352
|
+
}
|
|
154
353
|
};
|
|
155
354
|
export default MapTools;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import Map from "ol/Map";
|
|
2
2
|
import { MeasureHandlerType } from "../types";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 测量工具处理类
|
|
5
|
+
* 提供距离和面积测量功能
|
|
5
6
|
*/
|
|
6
7
|
export default class MeasureHandler {
|
|
7
8
|
private readonly source;
|
|
@@ -9,14 +10,19 @@ export default class MeasureHandler {
|
|
|
9
10
|
private sketch;
|
|
10
11
|
private helpTooltipElement;
|
|
11
12
|
private helpTooltip;
|
|
12
|
-
private _map;
|
|
13
|
+
private readonly _map;
|
|
13
14
|
private measureTooltipElement;
|
|
14
15
|
private measureTooltip;
|
|
15
|
-
private continuePolygonMsg;
|
|
16
|
-
private continueLineMsg;
|
|
16
|
+
private readonly continuePolygonMsg;
|
|
17
|
+
private readonly continueLineMsg;
|
|
17
18
|
private _tipsCollection;
|
|
18
19
|
private _mouseListener;
|
|
19
20
|
private _draw;
|
|
21
|
+
/**
|
|
22
|
+
* 构造函数
|
|
23
|
+
* @param map OpenLayers地图实例
|
|
24
|
+
* @throws 当地图实例无效时抛出错误
|
|
25
|
+
*/
|
|
20
26
|
constructor(map: Map);
|
|
21
27
|
/**
|
|
22
28
|
* destory the object
|
|
@@ -35,8 +41,9 @@ export default class MeasureHandler {
|
|
|
35
41
|
*/
|
|
36
42
|
formatArea(polygon: any): string;
|
|
37
43
|
/**
|
|
38
|
-
*
|
|
39
|
-
* @param
|
|
44
|
+
* 开始测量
|
|
45
|
+
* @param type 测量类型
|
|
46
|
+
* @throws 当测量类型无效时抛出错误
|
|
40
47
|
*/
|
|
41
48
|
start(type: MeasureHandlerType): void;
|
|
42
49
|
/**
|
|
@@ -6,13 +6,21 @@ import { Vector as VectorSource } from 'ol/source.js';
|
|
|
6
6
|
import { Vector as VectorLayer } from 'ol/layer.js';
|
|
7
7
|
import { getArea, getLength } from 'ol/sphere.js';
|
|
8
8
|
import { unByKey } from 'ol/Observable.js';
|
|
9
|
+
import { ValidationUtils } from '../utils/ValidationUtils';
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
+
* 测量工具处理类
|
|
12
|
+
* 提供距离和面积测量功能
|
|
11
13
|
*/
|
|
12
14
|
export default class MeasureHandler {
|
|
15
|
+
/**
|
|
16
|
+
* 构造函数
|
|
17
|
+
* @param map OpenLayers地图实例
|
|
18
|
+
* @throws 当地图实例无效时抛出错误
|
|
19
|
+
*/
|
|
13
20
|
constructor(map) {
|
|
14
|
-
this._map = null;
|
|
15
21
|
this._draw = null;
|
|
22
|
+
ValidationUtils.validateMap(map);
|
|
23
|
+
this._map = map;
|
|
16
24
|
this.source = new VectorSource();
|
|
17
25
|
this.vector = new VectorLayer({
|
|
18
26
|
source: this.source,
|
|
@@ -37,7 +45,6 @@ export default class MeasureHandler {
|
|
|
37
45
|
}),
|
|
38
46
|
zIndex: 999,
|
|
39
47
|
});
|
|
40
|
-
this._map = map;
|
|
41
48
|
/**
|
|
42
49
|
* Currently drawn feature.
|
|
43
50
|
* @type {import("ol/Feature.js").default}
|
|
@@ -149,17 +156,23 @@ export default class MeasureHandler {
|
|
|
149
156
|
return output;
|
|
150
157
|
}
|
|
151
158
|
/**
|
|
152
|
-
*
|
|
153
|
-
* @param
|
|
159
|
+
* 开始测量
|
|
160
|
+
* @param type 测量类型
|
|
161
|
+
* @throws 当测量类型无效时抛出错误
|
|
154
162
|
*/
|
|
155
163
|
start(type) {
|
|
156
|
-
|
|
157
|
-
|
|
164
|
+
ValidationUtils.validateMeasureType(type);
|
|
165
|
+
ValidationUtils.validateMap(this._map);
|
|
166
|
+
try {
|
|
167
|
+
this.createMeasureTooltip();
|
|
168
|
+
this.createHelpTooltip();
|
|
169
|
+
if (this._draw) {
|
|
170
|
+
this._map.removeInteraction(this._draw);
|
|
171
|
+
}
|
|
158
172
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
this._map.removeInteraction(this._draw);
|
|
173
|
+
catch (error) {
|
|
174
|
+
console.error('Error starting measurement:', error);
|
|
175
|
+
throw new Error('Failed to start measurement');
|
|
163
176
|
}
|
|
164
177
|
this._draw = new Draw({
|
|
165
178
|
source: this.source,
|
|
@@ -196,22 +209,25 @@ export default class MeasureHandler {
|
|
|
196
209
|
this.sketch = evt.feature;
|
|
197
210
|
/** @type {import("ol/coordinate.js").Coordinate|undefined} */
|
|
198
211
|
let tooltipCoord = evt?.coordinate;
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
output
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
212
|
+
const geometry = this.sketch?.getGeometry();
|
|
213
|
+
if (geometry) {
|
|
214
|
+
listener = geometry.on('change', (evt) => {
|
|
215
|
+
const geom = evt.target;
|
|
216
|
+
let output;
|
|
217
|
+
if (geom instanceof Polygon) {
|
|
218
|
+
output = this.formatArea(geom);
|
|
219
|
+
tooltipCoord = geom.getInteriorPoint().getCoordinates();
|
|
220
|
+
}
|
|
221
|
+
else if (geom instanceof LineString) {
|
|
222
|
+
output = this.formatLength(geom);
|
|
223
|
+
tooltipCoord = geom.getLastCoordinate();
|
|
224
|
+
}
|
|
225
|
+
if (this.measureTooltipElement) {
|
|
226
|
+
this.measureTooltipElement.innerHTML = output;
|
|
227
|
+
}
|
|
228
|
+
this.measureTooltip?.setPosition(tooltipCoord);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
215
231
|
});
|
|
216
232
|
this._draw.on('drawend', () => {
|
|
217
233
|
if (this.measureTooltipElement) {
|