my-openlayer 0.1.18 → 1.0.0
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 +113 -0
- package/dist/core/ConfigManager.js +164 -0
- package/dist/core/DomPoint.d.ts +163 -14
- package/dist/core/DomPoint.js +401 -26
- package/dist/core/EventManager.d.ts +131 -0
- package/dist/core/EventManager.js +257 -0
- package/dist/core/Line.d.ts +20 -4
- package/dist/core/Line.js +36 -1
- package/dist/core/MapBaseLayers.d.ts +187 -19
- package/dist/core/MapBaseLayers.js +460 -122
- package/dist/core/MapTools.d.ts +77 -7
- package/dist/core/MapTools.js +267 -65
- package/dist/core/MeasureHandler.d.ts +13 -6
- package/dist/core/MeasureHandler.js +46 -25
- package/dist/core/Point.d.ts +13 -5
- package/dist/core/Point.js +94 -39
- package/dist/core/Polygon.d.ts +74 -22
- package/dist/core/Polygon.js +306 -125
- package/dist/index.d.ts +17 -10
- package/dist/index.js +15 -10
- package/dist/types.d.ts +280 -96
- package/dist/types.js +11 -1
- package/dist/utils/ErrorHandler.d.ts +102 -0
- package/dist/utils/ErrorHandler.js +191 -0
- package/package.json +5 -6
package/dist/MyOl.d.ts
CHANGED
|
@@ -5,63 +5,131 @@ import Point from "./core/Point";
|
|
|
5
5
|
import Line from "./core/Line";
|
|
6
6
|
import MapBaseLayers from "./core/MapBaseLayers";
|
|
7
7
|
import MapTools from "./core/MapTools";
|
|
8
|
+
import { ErrorHandler } from './utils/ErrorHandler';
|
|
9
|
+
import { EventManager } from './core/EventManager';
|
|
10
|
+
import { ConfigManager } from './core/ConfigManager';
|
|
8
11
|
import { MapInitType, EventType } from './types';
|
|
12
|
+
/**
|
|
13
|
+
* MyOl 地图核心类
|
|
14
|
+
* 提供完整的地图操作功能,包括点、线、面要素管理,底图切换,工具操作等
|
|
15
|
+
*/
|
|
9
16
|
export default class MyOl {
|
|
10
|
-
map: Map;
|
|
11
|
-
private
|
|
12
|
-
private
|
|
13
|
-
private
|
|
14
|
-
private
|
|
15
|
-
private
|
|
17
|
+
readonly map: Map;
|
|
18
|
+
private _baseLayers?;
|
|
19
|
+
private _polygon?;
|
|
20
|
+
private _mapTools?;
|
|
21
|
+
private _point?;
|
|
22
|
+
private _line?;
|
|
23
|
+
private readonly errorHandler;
|
|
24
|
+
private readonly eventManager;
|
|
25
|
+
private readonly configManager;
|
|
16
26
|
private readonly options;
|
|
17
|
-
static DefaultOptions: MapInitType;
|
|
18
|
-
|
|
27
|
+
static readonly DefaultOptions: MapInitType;
|
|
28
|
+
private static readonly PROJECTIONS;
|
|
19
29
|
/**
|
|
20
|
-
*
|
|
30
|
+
* 构造函数
|
|
31
|
+
* @param id 地图容器 DOM 元素 ID
|
|
32
|
+
* @param options 地图初始化配置
|
|
33
|
+
*/
|
|
34
|
+
constructor(id: string, options?: Partial<MapInitType>);
|
|
35
|
+
/**
|
|
36
|
+
* 验证构造函数参数
|
|
37
|
+
* @private
|
|
38
|
+
*/
|
|
39
|
+
private validateConstructorParams;
|
|
40
|
+
/**
|
|
41
|
+
* 初始化坐标系
|
|
42
|
+
* @private
|
|
43
|
+
*/
|
|
44
|
+
private static initializeProjections;
|
|
45
|
+
/**
|
|
46
|
+
* 创建地图控件
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
private createControls;
|
|
50
|
+
/**
|
|
51
|
+
* 初始化事件监听
|
|
52
|
+
* @private
|
|
53
|
+
*/
|
|
54
|
+
private initializeEventListeners;
|
|
55
|
+
/**
|
|
56
|
+
* 创建地图视图
|
|
21
57
|
* @param options 视图配置
|
|
22
|
-
* @
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
58
|
+
* @returns View 地图视图实例
|
|
59
|
+
*/
|
|
60
|
+
static createView(options?: MapInitType): View;
|
|
61
|
+
/**
|
|
62
|
+
* 获取视图(向后兼容)
|
|
63
|
+
* @deprecated 请使用 createView 方法
|
|
28
64
|
*/
|
|
29
65
|
static getView(options?: MapInitType): View;
|
|
30
66
|
/**
|
|
31
|
-
*
|
|
32
|
-
* @returns Polygon
|
|
67
|
+
* 获取面要素操作模块
|
|
68
|
+
* @returns Polygon 面要素操作实例
|
|
33
69
|
*/
|
|
34
70
|
getPolygon(): Polygon;
|
|
71
|
+
/**
|
|
72
|
+
* 获取底图图层管理模块
|
|
73
|
+
* @returns MapBaseLayers 底图管理实例
|
|
74
|
+
*/
|
|
35
75
|
getMapBaseLayers(): MapBaseLayers;
|
|
36
76
|
/**
|
|
37
|
-
*
|
|
38
|
-
* @returns Point
|
|
77
|
+
* 获取点要素操作模块
|
|
78
|
+
* @returns Point 点要素操作实例
|
|
39
79
|
*/
|
|
40
80
|
getPoint(): Point;
|
|
41
81
|
/**
|
|
42
|
-
*
|
|
43
|
-
* @returns Line
|
|
82
|
+
* 获取线要素操作模块
|
|
83
|
+
* @returns Line 线要素操作实例
|
|
44
84
|
*/
|
|
45
85
|
getLine(): Line;
|
|
46
86
|
/**
|
|
47
|
-
*
|
|
48
|
-
* @returns MapTools
|
|
87
|
+
* 获取地图工具模块
|
|
88
|
+
* @returns MapTools 地图工具实例
|
|
49
89
|
*/
|
|
50
90
|
getTools(): MapTools;
|
|
91
|
+
/**
|
|
92
|
+
* 重置地图位置到初始中心点
|
|
93
|
+
* @param duration 动画持续时间(毫秒)
|
|
94
|
+
*/
|
|
51
95
|
resetPosition(duration?: number): void;
|
|
52
96
|
/**
|
|
53
|
-
*
|
|
54
|
-
* @param
|
|
55
|
-
* @param
|
|
97
|
+
* 地图定位到指定坐标
|
|
98
|
+
* @param longitude 经度
|
|
99
|
+
* @param latitude 纬度
|
|
56
100
|
* @param zoom 缩放级别
|
|
57
|
-
* @param duration
|
|
101
|
+
* @param duration 动画持续时间(毫秒)
|
|
58
102
|
*/
|
|
59
|
-
locationAction(
|
|
103
|
+
locationAction(longitude: number, latitude: number, zoom?: number, duration?: number): void;
|
|
60
104
|
/**
|
|
61
|
-
*
|
|
105
|
+
* 监听地图事件
|
|
62
106
|
* @param eventType 事件类型
|
|
63
|
-
* @param clickType 点击类型
|
|
64
107
|
* @param callback 回调函数
|
|
108
|
+
* @param clickType 点击类型(可选)
|
|
109
|
+
*/
|
|
110
|
+
mapOnEvent(eventType: EventType, callback: (feature?: any, e?: any) => void, clickType?: 'point' | 'line' | 'polygon'): void;
|
|
111
|
+
/**
|
|
112
|
+
* 获取错误处理器实例
|
|
113
|
+
* @returns ErrorHandler 错误处理器
|
|
114
|
+
*/
|
|
115
|
+
getErrorHandler(): ErrorHandler;
|
|
116
|
+
/**
|
|
117
|
+
* 获取事件管理器实例
|
|
118
|
+
* @returns EventManager 事件管理器
|
|
119
|
+
*/
|
|
120
|
+
getEventManager(): EventManager;
|
|
121
|
+
/**
|
|
122
|
+
* 获取配置管理器实例
|
|
123
|
+
* @returns ConfigManager 配置管理器
|
|
124
|
+
*/
|
|
125
|
+
getConfigManager(): ConfigManager;
|
|
126
|
+
/**
|
|
127
|
+
* 获取当前地图配置
|
|
128
|
+
* @returns MapInitType 地图配置
|
|
129
|
+
*/
|
|
130
|
+
getMapOptions(): Readonly<MapInitType>;
|
|
131
|
+
/**
|
|
132
|
+
* 销毁地图实例和相关资源
|
|
65
133
|
*/
|
|
66
|
-
|
|
134
|
+
destroy(): void;
|
|
67
135
|
}
|
package/dist/MyOl.js
CHANGED
|
@@ -1,159 +1,386 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// OpenLayers 核心导入
|
|
2
3
|
import { register as olProj4Register } from 'ol/proj/proj4';
|
|
3
4
|
import { Projection as olProjProjection, addProjection as olProjAddProjection, fromLonLat as olProjFromLonLat } from 'ol/proj';
|
|
4
5
|
import View from "ol/View";
|
|
5
6
|
import Map from "ol/Map";
|
|
7
|
+
import { defaults as defaultControls } from 'ol/control';
|
|
8
|
+
import proj4 from "proj4";
|
|
9
|
+
// 内部模块导入
|
|
6
10
|
import Polygon from "./core/Polygon";
|
|
7
11
|
import Point from "./core/Point";
|
|
8
12
|
import Line from "./core/Line";
|
|
9
13
|
import MapBaseLayers from "./core/MapBaseLayers";
|
|
10
|
-
import proj4 from "proj4";
|
|
11
14
|
import MapTools from "./core/MapTools";
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
import { ErrorHandler, MyOpenLayersError, ErrorType } from './utils/ErrorHandler';
|
|
16
|
+
import { EventManager } from './core/EventManager';
|
|
17
|
+
import { ConfigManager } from './core/ConfigManager';
|
|
18
|
+
/**
|
|
19
|
+
* MyOl 地图核心类
|
|
20
|
+
* 提供完整的地图操作功能,包括点、线、面要素管理,底图切换,工具操作等
|
|
21
|
+
*/
|
|
16
22
|
class MyOl {
|
|
23
|
+
/**
|
|
24
|
+
* 构造函数
|
|
25
|
+
* @param id 地图容器 DOM 元素 ID
|
|
26
|
+
* @param options 地图初始化配置
|
|
27
|
+
*/
|
|
17
28
|
constructor(id, options) {
|
|
18
|
-
|
|
19
|
-
this.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
// 初始化错误处理器(必须最先初始化)
|
|
30
|
+
this.errorHandler = ErrorHandler.getInstance();
|
|
31
|
+
try {
|
|
32
|
+
// 初始化配置管理器
|
|
33
|
+
this.configManager = new ConfigManager();
|
|
34
|
+
// 合并配置(处理 undefined 情况)
|
|
35
|
+
this.options = ConfigManager.mergeOptions(MyOl.DefaultOptions, options || {});
|
|
36
|
+
// 参数验证
|
|
37
|
+
this.validateConstructorParams(id, this.options);
|
|
38
|
+
// 初始化坐标系
|
|
39
|
+
MyOl.initializeProjections();
|
|
40
|
+
// 准备图层
|
|
41
|
+
const layers = Array.isArray(this.options.layers) ? this.options.layers : [];
|
|
42
|
+
// 创建地图实例
|
|
43
|
+
this.map = new Map({
|
|
44
|
+
target: id,
|
|
45
|
+
view: MyOl.createView(this.options),
|
|
46
|
+
layers: layers,
|
|
47
|
+
controls: this.createControls()
|
|
48
|
+
});
|
|
49
|
+
// 初始化事件管理器(需要地图实例)
|
|
50
|
+
this.eventManager = new EventManager(this.map);
|
|
51
|
+
// 初始化事件监听
|
|
52
|
+
this.initializeEventListeners();
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
this.errorHandler.handleError(new MyOpenLayersError(`地图初始化失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.MAP_ERROR, { id, options }));
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
34
58
|
}
|
|
35
59
|
/**
|
|
36
|
-
*
|
|
37
|
-
* @
|
|
38
|
-
* @param options.center 中心点
|
|
39
|
-
* @param options.zoom 缩放级别
|
|
40
|
-
* @param options.minZoom 最小缩放级别
|
|
41
|
-
* @param options.maxZoom 最大缩放级别
|
|
42
|
-
* @param options.extent 视图范围
|
|
43
|
-
* @returns View
|
|
60
|
+
* 验证构造函数参数
|
|
61
|
+
* @private
|
|
44
62
|
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
63
|
+
validateConstructorParams(id, options) {
|
|
64
|
+
if (!id || typeof id !== 'string') {
|
|
65
|
+
throw new Error('地图容器 ID 必须是非空字符串');
|
|
66
|
+
}
|
|
67
|
+
if (!options || typeof options !== 'object') {
|
|
68
|
+
throw new Error('地图配置选项不能为空');
|
|
69
|
+
}
|
|
70
|
+
// 检查 DOM 元素是否存在
|
|
71
|
+
const element = document.getElementById(id);
|
|
72
|
+
if (!element) {
|
|
73
|
+
throw new Error(`找不到 ID 为 '${id}' 的 DOM 元素`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 初始化坐标系
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
static initializeProjections() {
|
|
81
|
+
// 定义 CGCS2000 坐标系
|
|
82
|
+
proj4.defs(MyOl.PROJECTIONS.CGCS2000, "+proj=longlat +ellps=GRS80 +no_defs");
|
|
83
|
+
proj4.defs(MyOl.PROJECTIONS.CGCS2000_3_DEGREE, "+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs");
|
|
84
|
+
// 注册到 OpenLayers
|
|
48
85
|
olProj4Register(proj4);
|
|
86
|
+
// 添加 CGCS2000 投影
|
|
49
87
|
const cgsc2000 = new olProjProjection({
|
|
50
|
-
code:
|
|
88
|
+
code: MyOl.PROJECTIONS.CGCS2000,
|
|
51
89
|
extent: [-180, -90, 180, 90],
|
|
52
90
|
worldExtent: [-180, -90, 180, 90],
|
|
53
91
|
units: "degrees"
|
|
54
92
|
});
|
|
55
93
|
olProjAddProjection(cgsc2000);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 创建地图控件
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
createControls() {
|
|
100
|
+
return defaultControls({
|
|
101
|
+
zoom: false,
|
|
102
|
+
rotate: false,
|
|
103
|
+
attribution: false
|
|
104
|
+
}).extend([]);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 初始化事件监听
|
|
108
|
+
* @private
|
|
109
|
+
*/
|
|
110
|
+
initializeEventListeners() {
|
|
111
|
+
// 地图加载完成事件
|
|
112
|
+
this.map.once('rendercomplete', () => {
|
|
113
|
+
console.debug('地图初始化完成', { map: this.map });
|
|
114
|
+
});
|
|
115
|
+
// 地图错误事件
|
|
116
|
+
this.map.on('error', (error) => {
|
|
117
|
+
this.errorHandler.handleError(new MyOpenLayersError('地图渲染错误', ErrorType.MAP_ERROR, { error }));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 创建地图视图
|
|
122
|
+
* @param options 视图配置
|
|
123
|
+
* @returns View 地图视图实例
|
|
124
|
+
*/
|
|
125
|
+
static createView(options = MyOl.DefaultOptions) {
|
|
126
|
+
try {
|
|
127
|
+
const projection = new olProjProjection({
|
|
128
|
+
code: MyOl.PROJECTIONS.CGCS2000,
|
|
129
|
+
extent: [-180, -90, 180, 90],
|
|
130
|
+
worldExtent: [-180, -90, 180, 90],
|
|
131
|
+
units: "degrees"
|
|
132
|
+
});
|
|
133
|
+
const viewOptions = {
|
|
134
|
+
projection,
|
|
135
|
+
center: olProjFromLonLat(options.center, projection),
|
|
136
|
+
zoom: options.zoom ?? MyOl.DefaultOptions.zoom,
|
|
137
|
+
minZoom: options.minZoom ?? MyOl.DefaultOptions.minZoom,
|
|
138
|
+
maxZoom: options.maxZoom ?? MyOl.DefaultOptions.maxZoom,
|
|
139
|
+
...(options.extent && { extent: options.extent })
|
|
140
|
+
};
|
|
141
|
+
return new View(viewOptions);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
throw new MyOpenLayersError(`视图创建失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.MAP_ERROR, { options });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 获取视图(向后兼容)
|
|
149
|
+
* @deprecated 请使用 createView 方法
|
|
150
|
+
*/
|
|
151
|
+
static getView(options = MyOl.DefaultOptions) {
|
|
152
|
+
console.warn('getView 方法已废弃,请使用 createView 方法');
|
|
153
|
+
return MyOl.createView(options);
|
|
154
|
+
}
|
|
155
|
+
// ==========================================
|
|
156
|
+
// 功能模块获取方法(懒加载模式)
|
|
157
|
+
// ==========================================
|
|
158
|
+
/**
|
|
159
|
+
* 获取面要素操作模块
|
|
160
|
+
* @returns Polygon 面要素操作实例
|
|
74
161
|
*/
|
|
75
162
|
getPolygon() {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
163
|
+
try {
|
|
164
|
+
if (!this._polygon) {
|
|
165
|
+
this._polygon = new Polygon(this.map);
|
|
166
|
+
console.debug('面要素模块已加载');
|
|
167
|
+
}
|
|
168
|
+
return this._polygon;
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
this.errorHandler.handleError(new MyOpenLayersError('面要素模块初始化失败', ErrorType.COMPONENT_ERROR, { error }));
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
79
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* 获取底图图层管理模块
|
|
177
|
+
* @returns MapBaseLayers 底图管理实例
|
|
178
|
+
*/
|
|
80
179
|
getMapBaseLayers() {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
180
|
+
try {
|
|
181
|
+
if (!this._baseLayers) {
|
|
182
|
+
// 检查是否设置了自定义底图
|
|
183
|
+
if (Array.isArray(this.options.layers)) {
|
|
184
|
+
console.warn('已设置默认底图,MapBaseLayers 中的 switchBaseLayer 方法将失效');
|
|
185
|
+
}
|
|
186
|
+
const layerOptions = {
|
|
187
|
+
layers: this.options.layers,
|
|
188
|
+
annotation: this.options.annotation,
|
|
189
|
+
zIndex: 1,
|
|
190
|
+
mapClip: !!this.options.mapClipData,
|
|
191
|
+
mapClipData: this.options.mapClipData,
|
|
192
|
+
token: this.options.token || ''
|
|
193
|
+
};
|
|
194
|
+
this._baseLayers = new MapBaseLayers(this.map, layerOptions);
|
|
195
|
+
console.debug('基础图层模块已加载');
|
|
196
|
+
}
|
|
197
|
+
return this._baseLayers;
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
this.errorHandler.handleError(new MyOpenLayersError('基础图层模块初始化失败', ErrorType.COMPONENT_ERROR, { error }));
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* 获取点要素操作模块
|
|
206
|
+
* @returns Point 点要素操作实例
|
|
102
207
|
*/
|
|
103
208
|
getPoint() {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
209
|
+
try {
|
|
210
|
+
if (!this._point) {
|
|
211
|
+
this._point = new Point(this.map);
|
|
212
|
+
console.debug('点要素模块已加载');
|
|
213
|
+
}
|
|
214
|
+
return this._point;
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
this.errorHandler.handleError(new MyOpenLayersError('点要素模块初始化失败', ErrorType.COMPONENT_ERROR, { error }));
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
107
220
|
}
|
|
108
|
-
// ╔══════════╗
|
|
109
|
-
// ║ 地图 线 ║
|
|
110
|
-
// ╚══════════╝
|
|
111
221
|
/**
|
|
112
|
-
*
|
|
113
|
-
* @returns Line
|
|
222
|
+
* 获取线要素操作模块
|
|
223
|
+
* @returns Line 线要素操作实例
|
|
114
224
|
*/
|
|
115
225
|
getLine() {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
226
|
+
try {
|
|
227
|
+
if (!this._line) {
|
|
228
|
+
this._line = new Line(this.map);
|
|
229
|
+
console.debug('线要素模块已加载');
|
|
230
|
+
}
|
|
231
|
+
return this._line;
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
this.errorHandler.handleError(new MyOpenLayersError('线要素模块初始化失败', ErrorType.COMPONENT_ERROR, { error }));
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
119
237
|
}
|
|
120
|
-
// ╔════════════╗
|
|
121
|
-
// ║ 地图 工具 ║
|
|
122
|
-
// ╚════════════╝
|
|
123
238
|
/**
|
|
124
|
-
*
|
|
125
|
-
* @returns MapTools
|
|
239
|
+
* 获取地图工具模块
|
|
240
|
+
* @returns MapTools 地图工具实例
|
|
126
241
|
*/
|
|
127
242
|
getTools() {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
243
|
+
try {
|
|
244
|
+
if (!this._mapTools) {
|
|
245
|
+
this._mapTools = new MapTools(this.map);
|
|
246
|
+
console.debug('工具模块已加载');
|
|
247
|
+
}
|
|
248
|
+
return this._mapTools;
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
this.errorHandler.handleError(new MyOpenLayersError('工具模块初始化失败', ErrorType.COMPONENT_ERROR, { error }));
|
|
252
|
+
throw error;
|
|
253
|
+
}
|
|
131
254
|
}
|
|
255
|
+
// ==========================================
|
|
256
|
+
// 地图操作方法
|
|
257
|
+
// ==========================================
|
|
258
|
+
/**
|
|
259
|
+
* 重置地图位置到初始中心点
|
|
260
|
+
* @param duration 动画持续时间(毫秒)
|
|
261
|
+
*/
|
|
132
262
|
resetPosition(duration = 3000) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
263
|
+
try {
|
|
264
|
+
if (!this.options.center) {
|
|
265
|
+
throw new Error('未设置中心点,无法重置位置');
|
|
266
|
+
}
|
|
267
|
+
const [longitude, latitude] = this.options.center;
|
|
268
|
+
this.locationAction(longitude, latitude, this.options.zoom, duration);
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
this.errorHandler.handleError(new MyOpenLayersError(`重置地图位置失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.MAP_ERROR, { center: this.options.center, duration }));
|
|
272
|
+
}
|
|
136
273
|
}
|
|
137
274
|
/**
|
|
138
|
-
*
|
|
139
|
-
* @param
|
|
140
|
-
* @param
|
|
275
|
+
* 地图定位到指定坐标
|
|
276
|
+
* @param longitude 经度
|
|
277
|
+
* @param latitude 纬度
|
|
141
278
|
* @param zoom 缩放级别
|
|
142
|
-
* @param duration
|
|
279
|
+
* @param duration 动画持续时间(毫秒)
|
|
143
280
|
*/
|
|
144
|
-
locationAction(
|
|
145
|
-
|
|
281
|
+
locationAction(longitude, latitude, zoom = 20, duration = 3000) {
|
|
282
|
+
try {
|
|
283
|
+
// 参数验证
|
|
284
|
+
if (typeof longitude !== 'number' || typeof latitude !== 'number') {
|
|
285
|
+
throw new Error('经纬度必须是数字类型');
|
|
286
|
+
}
|
|
287
|
+
if (longitude < -180 || longitude > 180) {
|
|
288
|
+
throw new Error('经度值必须在 -180 到 180 之间');
|
|
289
|
+
}
|
|
290
|
+
if (latitude < -90 || latitude > 90) {
|
|
291
|
+
throw new Error('纬度值必须在 -90 到 90 之间');
|
|
292
|
+
}
|
|
293
|
+
this.getPoint().locationAction(longitude, latitude, zoom, duration);
|
|
294
|
+
// 记录定位操作
|
|
295
|
+
console.debug('地图定位完成', {
|
|
296
|
+
longitude,
|
|
297
|
+
latitude,
|
|
298
|
+
zoom,
|
|
299
|
+
duration
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
this.errorHandler.handleError(new MyOpenLayersError(`地图定位失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.MAP_ERROR, { longitude, latitude, zoom, duration }));
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
146
306
|
}
|
|
147
307
|
/**
|
|
148
|
-
*
|
|
308
|
+
* 监听地图事件
|
|
149
309
|
* @param eventType 事件类型
|
|
150
|
-
* @param clickType 点击类型
|
|
151
310
|
* @param callback 回调函数
|
|
311
|
+
* @param clickType 点击类型(可选)
|
|
152
312
|
*/
|
|
153
313
|
mapOnEvent(eventType, callback, clickType) {
|
|
154
|
-
|
|
314
|
+
try {
|
|
315
|
+
if (typeof callback !== 'function') {
|
|
316
|
+
throw new Error('回调函数必须是函数类型');
|
|
317
|
+
}
|
|
318
|
+
MapTools.mapOnEvent(this.map, eventType, callback, clickType);
|
|
319
|
+
// 记录事件监听
|
|
320
|
+
console.debug('地图事件监听已添加', {
|
|
321
|
+
eventType,
|
|
322
|
+
clickType
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
this.errorHandler.handleError(new MyOpenLayersError(`添加地图事件监听失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.MAP_ERROR, { eventType, clickType }));
|
|
327
|
+
throw error;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
// ==========================================
|
|
331
|
+
// 管理器访问方法
|
|
332
|
+
// ==========================================
|
|
333
|
+
/**
|
|
334
|
+
* 获取错误处理器实例
|
|
335
|
+
* @returns ErrorHandler 错误处理器
|
|
336
|
+
*/
|
|
337
|
+
getErrorHandler() {
|
|
338
|
+
return this.errorHandler;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* 获取事件管理器实例
|
|
342
|
+
* @returns EventManager 事件管理器
|
|
343
|
+
*/
|
|
344
|
+
getEventManager() {
|
|
345
|
+
return this.eventManager;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* 获取配置管理器实例
|
|
349
|
+
* @returns ConfigManager 配置管理器
|
|
350
|
+
*/
|
|
351
|
+
getConfigManager() {
|
|
352
|
+
return this.configManager;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* 获取当前地图配置
|
|
356
|
+
* @returns MapInitType 地图配置
|
|
357
|
+
*/
|
|
358
|
+
getMapOptions() {
|
|
359
|
+
return Object.freeze({ ...this.options });
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* 销毁地图实例和相关资源
|
|
363
|
+
*/
|
|
364
|
+
destroy() {
|
|
365
|
+
try {
|
|
366
|
+
// 清理事件监听
|
|
367
|
+
this.eventManager.clear();
|
|
368
|
+
// 销毁功能模块
|
|
369
|
+
this._point = undefined;
|
|
370
|
+
this._line = undefined;
|
|
371
|
+
this._polygon = undefined;
|
|
372
|
+
this._mapTools = undefined;
|
|
373
|
+
this._baseLayers = undefined;
|
|
374
|
+
// 销毁地图
|
|
375
|
+
this.map.setTarget(undefined);
|
|
376
|
+
console.debug('地图实例已销毁', { map: this.map });
|
|
377
|
+
}
|
|
378
|
+
catch (error) {
|
|
379
|
+
this.errorHandler.handleError(new MyOpenLayersError(`销毁地图失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.MAP_ERROR));
|
|
380
|
+
}
|
|
155
381
|
}
|
|
156
382
|
}
|
|
383
|
+
// 默认配置
|
|
157
384
|
MyOl.DefaultOptions = {
|
|
158
385
|
layers: undefined,
|
|
159
386
|
zoom: 10,
|
|
@@ -162,4 +389,9 @@ MyOl.DefaultOptions = {
|
|
|
162
389
|
maxZoom: 20,
|
|
163
390
|
extent: undefined
|
|
164
391
|
};
|
|
392
|
+
// 坐标系配置
|
|
393
|
+
MyOl.PROJECTIONS = {
|
|
394
|
+
CGCS2000: "EPSG:4490",
|
|
395
|
+
CGCS2000_3_DEGREE: "EPSG:4549"
|
|
396
|
+
};
|
|
165
397
|
export default MyOl;
|