gdmap-utils 1.2.0 → 1.2.2

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 (34) hide show
  1. package/.husky/pre-commit +1 -1
  2. package/.prettierrc.json +17 -17
  3. package/README.md +2 -50
  4. package/dist/index.js +1 -83
  5. package/dist/index.js.map +1 -1
  6. package/examples/1_init.html +23 -23
  7. package/examples/2_mapInit.html +48 -48
  8. package/examples/3_MarkerLayer.html +565 -565
  9. package/examples/4_LabelMarkerLayer.html +574 -574
  10. package/index.html +134 -134
  11. package/package.json +48 -48
  12. package/src/LayerManager.ts +49 -49
  13. package/src/MapSourceImport.ts +23 -23
  14. package/src/MapUtils.ts +154 -154
  15. package/src/gdMap/gdHelper.js +194 -194
  16. package/src/gdMap/gdHelper.ts +85 -85
  17. package/src/gdMapUtils.js +377 -377
  18. package/src/index.ts +1 -1
  19. package/src/layers/baseMarkerLayer/LabelMarkerLayer.ts +238 -238
  20. package/src/layers/baseMarkerLayer/MarkerLayer.ts +206 -206
  21. package/src/layers/baseMarkerLayer/index.ts +354 -354
  22. package/src/{layers copy/MarkerClusterLayer.js → layers/clusterMarkerLayer/MarkerClusterLayer.js} +155 -155
  23. package/src/layers/{MarkerClusterLayer.ts → clusterMarkerLayer/MarkerClusterLayer.ts} +30 -30
  24. package/src/layers/index.ts +5 -5
  25. package/src/types/BaseMarkerLayer.d.ts +87 -87
  26. package/src/types/MapUtils.d.ts +53 -53
  27. package/src/types/amap.d.ts +11 -11
  28. package/src/types/index.d.ts +12 -12
  29. package/tsconfig.json +26 -26
  30. package/webpack.config.js +126 -126
  31. package/src/layers copy/LabelMarkerLayer.js +0 -122
  32. package/src/layers copy/MarkerLayer.js +0 -267
  33. package/src/layers copy/OverlayGroupManager.js +0 -254
  34. package/src/layers copy/index.ts +0 -0
package/src/gdMapUtils.js CHANGED
@@ -1,377 +1,377 @@
1
- import AMapLoader from '@amap/amap-jsapi-loader';
2
- import gdHelperMixin from './gdHelper.js'; //抽取的高德mixin工具函数
3
- import OverlayGroupManager from './layers/OverlayGroupManager.js';
4
- import eventMixin from '../eventMixin.js';
5
- import LayerManager from './LayerManager.js';
6
-
7
- /**
8
- * 针对高德的二次封装
9
- * 目前高德地图使用的是 GCJ-02 坐标,如果你采集的是 WGS84 坐标或者其他,请先进行坐标转换
10
- */
11
- class GdMapUtils {
12
- // 地图实例对象
13
- map = null;
14
- //高德AMap对象
15
- AMap = null;
16
- // 地图Ui对象
17
- AMapUI = null;
18
- // loadOpts加载的配置信息 地图配置和加载地图配置分开
19
- loadOpts = {};
20
- // 地图容器id
21
- id = '';
22
- // 地图的配置对象
23
- mapOpts = {};
24
-
25
- mapTitleLayers = {}; //图层map对象 第三方图层
26
-
27
- // 缓存实例集合
28
- static mapInstance = new Map();
29
-
30
- overlayGroupManagerMap = new Map(); //HACK 是否移入到OverlayGroupManager中。
31
-
32
- // 图层实例映射
33
- layerInstances = new Map();
34
-
35
- // 图层管理器
36
- layerManager = LayerManager;
37
-
38
- /**
39
- * 加载地图和初始化地图分开
40
- * @param {Object} options 加载高德初始化地图配置
41
- */
42
- //TODO 构造函数能够接收this.map对象
43
- constructor(options) {
44
- if (!options) {
45
- this.error('请传入配置对象');
46
- }
47
- // 某些API加载前必须设置秘钥
48
- window._AMapSecurityConfig = {
49
- securityJsCode: process.env.VUE_APP_GD_MAP_CODE, // 安全密钥
50
- };
51
- options.key = process.env.VUE_APP_GD_MAP_KEY;
52
- this.loadOpts = options;
53
- this.clickMapRestMarkers = options.clickMapRestMarkers ?? true;
54
- }
55
-
56
- error(msg) {
57
- console.error(`[AmapUtils Error]:${msg}`);
58
- }
59
-
60
- /**
61
- * 异步加载地图插件
62
- * @param {String} plugins AMap.ToolBar
63
- * @returns {Promise}
64
- * @memberof GdMapUtils
65
- */
66
- loadPlugins(plugins) {
67
- return new Promise((resolve, reject) => {
68
- this.AMap.plugin(plugins, function (result) {
69
- resolve(result);
70
- });
71
- });
72
- }
73
-
74
- /**
75
- * 异步加载UI插件
76
- * @param {String} plugins overlay/AwesomeMarker
77
- * @return {Promise}
78
- * @memberof GdMapUtils
79
- */
80
- loadUIPlugins(plugins) {
81
- return new Promise((resolve, reject) => {
82
- if (!this.AMapUI) {
83
- reject(new Error('AMapUI is not initialized.')); // 提供错误信息
84
- return;
85
- }
86
- this.AMapUI.loadUI(plugins, function (result) {
87
- if (result) {
88
- resolve(result);
89
- } else {
90
- reject(new Error('Failed to load UI plugin.')); // 处理加载失败的情况
91
- }
92
- });
93
- });
94
- }
95
-
96
- /**
97
- * 初始化地图
98
- * @param {String} id DOM 的id
99
- * @param {Object} options Map地图配置项
100
- * @return {Promise}
101
- * @memberof GdMapUtils
102
- */
103
- initMap(id, options) {
104
- this.id = id;
105
- this.mapOpts = options;
106
- return new Promise((resolve, reject) => {
107
- // 确保每次的AMap都是合法的所以不分开创建
108
- AMapLoader.load(this.loadOpts)
109
- .then(AMap => {
110
- // 将 AMap 全局对象挂载到 window 上
111
- window.AMap = AMap;
112
-
113
- this.AMapUI = window.AMapUI;
114
-
115
- this.AMap = AMap;
116
-
117
- this.map = new AMap.Map(this.id, this.mapOpts); //"container"为 <div> 容器的 id
118
-
119
- resolve(this.map);
120
- // 将当前实例存储到 mapInstance 中
121
- GdMapUtils.mapInstance.set(id, this);
122
-
123
- this.bindMapClickEvent(); //初始化绑定事件
124
- })
125
- .catch(e => {
126
- reject(e);
127
- console.error(e);
128
- throw new Error(e);
129
- });
130
- });
131
- }
132
- /**
133
- * 连接到已有地图
134
- * @param {String} id DOM 的id
135
- * @param {Object} map 已经存在的地图实例
136
- * @return {Promise}
137
- * @memberof GdMapUtils
138
- */
139
- linkToExistMap(id, gdMapIns) {
140
- if (!id || !('options' in gdMapIns)) {
141
- //HACK map上挂属性标识他为地图实例
142
- return this.error('请传入正确的地图id和地图实例');
143
- }
144
-
145
- this.id = id;
146
- // 关联地图配置
147
- this.mapOpts = gdMapIns.options;
148
-
149
- this.AMap = AMap; //HACK 异步获取AMap对象, 根据map获取当前地图AMap
150
-
151
- this.AMapUI = window.AMapUI; //HACK AMapUI 是个啥
152
-
153
- this.map = gdMapIns; // 关联地图实例
154
-
155
- this.bindMapClickEvent(); //初始化绑定事件
156
- // 将当前实例存储到 mapInstance 中
157
- GdMapUtils.mapInstance.set(id, this);
158
-
159
- return this.map;
160
- }
161
-
162
- // 初始化绑定地图事件
163
- bindMapClickEvent() {
164
- this.map.on('click', () => {
165
- if (this.clickMapRestMarkers) {
166
- this.overlayGroupManagerMap.forEach(overlayGroup => {
167
- overlayGroup.resetActiveMarker(); // 清除图层上的所有覆盖物
168
- });
169
- }
170
- });
171
-
172
- // 绑定缩放时间获取当前层级
173
- this.map.on('zoomchange', () => {
174
- const zoom = this.map.getZoom(); // 获取当前缩放级别
175
- });
176
- }
177
-
178
- //给所有的marker绑定事件
179
- bindEventMarker(type, clickType, callback) {
180
- if (!this.getOverlayGroupManager(type)) {
181
- return this.error('图层不存在,请检查输入!');
182
- }
183
- this.getOverlayGroupManager(type).bindEventMarker(clickType, callback); // 绑定事件到图层管理器
184
- }
185
-
186
- //创建点位
187
- createMarker(type, Opts) {
188
- const overlayGroupManager = this.createOverlayGroupManager(Opts, type); // 关联图层管理器
189
- // 创建图标
190
- const marker = new AMap.Marker(Opts);
191
-
192
- // marker上地图
193
- overlayGroupManager.addOverlay(marker);
194
-
195
- return marker;
196
- }
197
-
198
- // 关联图层管理器
199
- createOverlayGroupManager(overlays, overlayType) {
200
- const overlayManager = this.getOverlayGroupManager(overlayType); //获取图层管理器
201
-
202
- if (overlayManager) return overlayManager; //图层已经关联了
203
-
204
- const overlayGroupManager = new OverlayGroupManager({
205
- overlays,
206
- overlayType,
207
- map: this.map,
208
- });
209
-
210
- this.overlayGroupManagerMap.set(overlayType, overlayGroupManager); //保存图层管理器
211
-
212
- return overlayGroupManager;
213
- }
214
-
215
- // 获取图层管理器
216
- getOverlayGroupManager(overlayType) {
217
- if (typeof overlayType !== 'string' && overlayType.length === 0) {
218
- return this.error('请传入图层类型');
219
- }
220
-
221
- return this.overlayGroupManagerMap.get(overlayType);
222
- }
223
-
224
- updateMarker(marker, Opts) {
225
- if (!marker) {
226
- return this.error('参数错误');
227
- }
228
- // 获取旧marker的类型
229
- const { type } = marker.getExtData();
230
- // 移除点位数据
231
- this.removeMarker(type, marker);
232
- // 更新Marker
233
- this.createMarker(type, Opts);
234
- }
235
- //移除某一个marker或者多个marker
236
- removeMarker(overlayType, overlay) {
237
- if (!this.overlayGroupManagerMap.has(overlayType)) {
238
- return this.error('图层不存在,请检查输入!');
239
- }
240
-
241
- const overlayGroupManager = this.getOverlayGroupManager(overlayType);
242
-
243
- overlayGroupManager.removeOverlay(overlay); // 关联图层管理器
244
- }
245
-
246
- // 清楚所有覆盖物
247
- removeAllOverlay() {
248
- //BUG 这里只能移除用overlayGroupManagerManager管理的图层
249
- this.overlayGroupManagerMap.forEach(overlayGroup => {
250
- overlayGroup.OverlayGroup.clearOverlays(); // 清除图层上的所有覆盖物
251
- });
252
- }
253
-
254
- // 高德地图添加覆盖物
255
- mapToAdd(overlay, autoFit = true) {
256
- this.map.add(overlay);
257
- // 调整到合适的视角
258
- // autoFit && this.map.setFitView();
259
- }
260
-
261
- // 创建海量点图层
262
- createMarkerCluster(data, options) {
263
- return new AMap.MarkerCluster(this.map, data, options);
264
- }
265
-
266
- // 创建标签图层
267
- createLabelLayer(options) {
268
- return new AMap.LabelLayer(options);
269
- }
270
-
271
- /**
272
- * 通过图层类型创建图层
273
- * @param {string} layerType - 图层类型
274
- * @param {Object} options - 图层配置选项
275
- * @returns {Object} 图层控制器实例
276
- */
277
- createLayer(layerType, options) {
278
- // 使用图层管理器创建图层控制器实例
279
- const layerController = this.layerManager.createLayer(layerType, options);
280
-
281
- if (layerController) {
282
- // 存储图层实例
283
- this.layerInstances.set(
284
- options?.config?.name || layerType,
285
- layerController
286
- );
287
-
288
- // 初始化创建图层
289
- layerController.createLayer(this).catch(err => {
290
- console.error(
291
- `[GdMapUtils Error]: Failed to create layer '${layerType}':`,
292
- err
293
- );
294
- });
295
- }
296
-
297
- return layerController;
298
- }
299
-
300
- /**
301
- * 获取指定名称的图层实例
302
- * @param {string} layerName - 图层名称
303
- * @returns {Object|null} 图层控制器实例
304
- */
305
- getLayer(layerName) {
306
- return this.layerInstances.get(layerName);
307
- }
308
-
309
- /**
310
- * 移除指定名称的图层
311
- * @param {string} layerName - 图层名称
312
- * @returns {boolean} 是否移除成功
313
- */
314
- removeLayer(layerName) {
315
- const layerController = this.layerInstances.get(layerName);
316
-
317
- if (layerController) {
318
- // 隐藏图层
319
- if (typeof layerController.hideLayer === 'function') {
320
- layerController.hideLayer();
321
- }
322
-
323
- // 从映射中移除
324
- this.layerInstances.delete(layerName);
325
- return true;
326
- }
327
-
328
- return false;
329
- }
330
-
331
- /**
332
- * 获取所有图层实例
333
- * @returns {Array} 图层控制器实例数组
334
- */
335
- getAllLayers() {
336
- return Array.from(this.layerInstances.values());
337
- }
338
-
339
- /**
340
- * 显示指定名称的图层
341
- * @param {string} layerName - 图层名称
342
- * @returns {boolean} 是否显示成功
343
- */
344
- showLayer(layerName) {
345
- const layerController = this.layerInstances.get(layerName);
346
-
347
- if (layerController && typeof layerController.showLayer === 'function') {
348
- layerController.showLayer();
349
- return true;
350
- }
351
-
352
- return false;
353
- }
354
-
355
- /**
356
- * 隐藏指定名称的图层
357
- * @param {string} layerName - 图层名称
358
- * @returns {boolean} 是否隐藏成功
359
- */
360
- hideLayer(layerName) {
361
- const layerController = this.layerInstances.get(layerName);
362
-
363
- if (layerController && typeof layerController.hideLayer === 'function') {
364
- layerController.hideLayer();
365
- return true;
366
- }
367
-
368
- return false;
369
- }
370
- }
371
- // 加载工具类方法到gdMapUtils中
372
- Object.assign(GdMapUtils.prototype, {
373
- ...eventMixin,
374
- ...gdHelperMixin,
375
- });
376
-
377
- export default GdMapUtils;
1
+ import AMapLoader from '@amap/amap-jsapi-loader';
2
+ import gdHelperMixin from './gdHelper.js'; //抽取的高德mixin工具函数
3
+ import OverlayGroupManager from './layers/OverlayGroupManager.js';
4
+ import eventMixin from '../eventMixin.js';
5
+ import LayerManager from './LayerManager.js';
6
+
7
+ /**
8
+ * 针对高德的二次封装
9
+ * 目前高德地图使用的是 GCJ-02 坐标,如果你采集的是 WGS84 坐标或者其他,请先进行坐标转换
10
+ */
11
+ class GdMapUtils {
12
+ // 地图实例对象
13
+ map = null;
14
+ //高德AMap对象
15
+ AMap = null;
16
+ // 地图Ui对象
17
+ AMapUI = null;
18
+ // loadOpts加载的配置信息 地图配置和加载地图配置分开
19
+ loadOpts = {};
20
+ // 地图容器id
21
+ id = '';
22
+ // 地图的配置对象
23
+ mapOpts = {};
24
+
25
+ mapTitleLayers = {}; //图层map对象 第三方图层
26
+
27
+ // 缓存实例集合
28
+ static mapInstance = new Map();
29
+
30
+ overlayGroupManagerMap = new Map(); //HACK 是否移入到OverlayGroupManager中。
31
+
32
+ // 图层实例映射
33
+ layerInstances = new Map();
34
+
35
+ // 图层管理器
36
+ layerManager = LayerManager;
37
+
38
+ /**
39
+ * 加载地图和初始化地图分开
40
+ * @param {Object} options 加载高德初始化地图配置
41
+ */
42
+ //TODO 构造函数能够接收this.map对象
43
+ constructor(options) {
44
+ if (!options) {
45
+ this.error('请传入配置对象');
46
+ }
47
+ // 某些API加载前必须设置秘钥
48
+ window._AMapSecurityConfig = {
49
+ securityJsCode: process.env.VUE_APP_GD_MAP_CODE, // 安全密钥
50
+ };
51
+ options.key = process.env.VUE_APP_GD_MAP_KEY;
52
+ this.loadOpts = options;
53
+ this.clickMapRestMarkers = options.clickMapRestMarkers ?? true;
54
+ }
55
+
56
+ error(msg) {
57
+ console.error(`[AmapUtils Error]:${msg}`);
58
+ }
59
+
60
+ /**
61
+ * 异步加载地图插件
62
+ * @param {String} plugins AMap.ToolBar
63
+ * @returns {Promise}
64
+ * @memberof GdMapUtils
65
+ */
66
+ loadPlugins(plugins) {
67
+ return new Promise((resolve, reject) => {
68
+ this.AMap.plugin(plugins, function (result) {
69
+ resolve(result);
70
+ });
71
+ });
72
+ }
73
+
74
+ /**
75
+ * 异步加载UI插件
76
+ * @param {String} plugins overlay/AwesomeMarker
77
+ * @return {Promise}
78
+ * @memberof GdMapUtils
79
+ */
80
+ loadUIPlugins(plugins) {
81
+ return new Promise((resolve, reject) => {
82
+ if (!this.AMapUI) {
83
+ reject(new Error('AMapUI is not initialized.')); // 提供错误信息
84
+ return;
85
+ }
86
+ this.AMapUI.loadUI(plugins, function (result) {
87
+ if (result) {
88
+ resolve(result);
89
+ } else {
90
+ reject(new Error('Failed to load UI plugin.')); // 处理加载失败的情况
91
+ }
92
+ });
93
+ });
94
+ }
95
+
96
+ /**
97
+ * 初始化地图
98
+ * @param {String} id DOM 的id
99
+ * @param {Object} options Map地图配置项
100
+ * @return {Promise}
101
+ * @memberof GdMapUtils
102
+ */
103
+ initMap(id, options) {
104
+ this.id = id;
105
+ this.mapOpts = options;
106
+ return new Promise((resolve, reject) => {
107
+ // 确保每次的AMap都是合法的所以不分开创建
108
+ AMapLoader.load(this.loadOpts)
109
+ .then(AMap => {
110
+ // 将 AMap 全局对象挂载到 window 上
111
+ window.AMap = AMap;
112
+
113
+ this.AMapUI = window.AMapUI;
114
+
115
+ this.AMap = AMap;
116
+
117
+ this.map = new AMap.Map(this.id, this.mapOpts); //"container"为 <div> 容器的 id
118
+
119
+ resolve(this.map);
120
+ // 将当前实例存储到 mapInstance 中
121
+ GdMapUtils.mapInstance.set(id, this);
122
+
123
+ this.bindMapClickEvent(); //初始化绑定事件
124
+ })
125
+ .catch(e => {
126
+ reject(e);
127
+ console.error(e);
128
+ throw new Error(e);
129
+ });
130
+ });
131
+ }
132
+ /**
133
+ * 连接到已有地图
134
+ * @param {String} id DOM 的id
135
+ * @param {Object} map 已经存在的地图实例
136
+ * @return {Promise}
137
+ * @memberof GdMapUtils
138
+ */
139
+ linkToExistMap(id, gdMapIns) {
140
+ if (!id || !('options' in gdMapIns)) {
141
+ //HACK map上挂属性标识他为地图实例
142
+ return this.error('请传入正确的地图id和地图实例');
143
+ }
144
+
145
+ this.id = id;
146
+ // 关联地图配置
147
+ this.mapOpts = gdMapIns.options;
148
+
149
+ this.AMap = AMap; //HACK 异步获取AMap对象, 根据map获取当前地图AMap
150
+
151
+ this.AMapUI = window.AMapUI; //HACK AMapUI 是个啥
152
+
153
+ this.map = gdMapIns; // 关联地图实例
154
+
155
+ this.bindMapClickEvent(); //初始化绑定事件
156
+ // 将当前实例存储到 mapInstance 中
157
+ GdMapUtils.mapInstance.set(id, this);
158
+
159
+ return this.map;
160
+ }
161
+
162
+ // 初始化绑定地图事件
163
+ bindMapClickEvent() {
164
+ this.map.on('click', () => {
165
+ if (this.clickMapRestMarkers) {
166
+ this.overlayGroupManagerMap.forEach(overlayGroup => {
167
+ overlayGroup.resetActiveMarker(); // 清除图层上的所有覆盖物
168
+ });
169
+ }
170
+ });
171
+
172
+ // 绑定缩放时间获取当前层级
173
+ this.map.on('zoomchange', () => {
174
+ const zoom = this.map.getZoom(); // 获取当前缩放级别
175
+ });
176
+ }
177
+
178
+ //给所有的marker绑定事件
179
+ bindEventMarker(type, clickType, callback) {
180
+ if (!this.getOverlayGroupManager(type)) {
181
+ return this.error('图层不存在,请检查输入!');
182
+ }
183
+ this.getOverlayGroupManager(type).bindEventMarker(clickType, callback); // 绑定事件到图层管理器
184
+ }
185
+
186
+ //创建点位
187
+ createMarker(type, Opts) {
188
+ const overlayGroupManager = this.createOverlayGroupManager(Opts, type); // 关联图层管理器
189
+ // 创建图标
190
+ const marker = new AMap.Marker(Opts);
191
+
192
+ // marker上地图
193
+ overlayGroupManager.addOverlay(marker);
194
+
195
+ return marker;
196
+ }
197
+
198
+ // 关联图层管理器
199
+ createOverlayGroupManager(overlays, overlayType) {
200
+ const overlayManager = this.getOverlayGroupManager(overlayType); //获取图层管理器
201
+
202
+ if (overlayManager) return overlayManager; //图层已经关联了
203
+
204
+ const overlayGroupManager = new OverlayGroupManager({
205
+ overlays,
206
+ overlayType,
207
+ map: this.map,
208
+ });
209
+
210
+ this.overlayGroupManagerMap.set(overlayType, overlayGroupManager); //保存图层管理器
211
+
212
+ return overlayGroupManager;
213
+ }
214
+
215
+ // 获取图层管理器
216
+ getOverlayGroupManager(overlayType) {
217
+ if (typeof overlayType !== 'string' && overlayType.length === 0) {
218
+ return this.error('请传入图层类型');
219
+ }
220
+
221
+ return this.overlayGroupManagerMap.get(overlayType);
222
+ }
223
+
224
+ updateMarker(marker, Opts) {
225
+ if (!marker) {
226
+ return this.error('参数错误');
227
+ }
228
+ // 获取旧marker的类型
229
+ const { type } = marker.getExtData();
230
+ // 移除点位数据
231
+ this.removeMarker(type, marker);
232
+ // 更新Marker
233
+ this.createMarker(type, Opts);
234
+ }
235
+ //移除某一个marker或者多个marker
236
+ removeMarker(overlayType, overlay) {
237
+ if (!this.overlayGroupManagerMap.has(overlayType)) {
238
+ return this.error('图层不存在,请检查输入!');
239
+ }
240
+
241
+ const overlayGroupManager = this.getOverlayGroupManager(overlayType);
242
+
243
+ overlayGroupManager.removeOverlay(overlay); // 关联图层管理器
244
+ }
245
+
246
+ // 清楚所有覆盖物
247
+ removeAllOverlay() {
248
+ //BUG 这里只能移除用overlayGroupManagerManager管理的图层
249
+ this.overlayGroupManagerMap.forEach(overlayGroup => {
250
+ overlayGroup.OverlayGroup.clearOverlays(); // 清除图层上的所有覆盖物
251
+ });
252
+ }
253
+
254
+ // 高德地图添加覆盖物
255
+ mapToAdd(overlay, autoFit = true) {
256
+ this.map.add(overlay);
257
+ // 调整到合适的视角
258
+ // autoFit && this.map.setFitView();
259
+ }
260
+
261
+ // 创建海量点图层
262
+ createMarkerCluster(data, options) {
263
+ return new AMap.MarkerCluster(this.map, data, options);
264
+ }
265
+
266
+ // 创建标签图层
267
+ createLabelLayer(options) {
268
+ return new AMap.LabelLayer(options);
269
+ }
270
+
271
+ /**
272
+ * 通过图层类型创建图层
273
+ * @param {string} layerType - 图层类型
274
+ * @param {Object} options - 图层配置选项
275
+ * @returns {Object} 图层控制器实例
276
+ */
277
+ createLayer(layerType, options) {
278
+ // 使用图层管理器创建图层控制器实例
279
+ const layerController = this.layerManager.createLayer(layerType, options);
280
+
281
+ if (layerController) {
282
+ // 存储图层实例
283
+ this.layerInstances.set(
284
+ options?.config?.name || layerType,
285
+ layerController
286
+ );
287
+
288
+ // 初始化创建图层
289
+ layerController.createLayer(this).catch(err => {
290
+ console.error(
291
+ `[GdMapUtils Error]: Failed to create layer '${layerType}':`,
292
+ err
293
+ );
294
+ });
295
+ }
296
+
297
+ return layerController;
298
+ }
299
+
300
+ /**
301
+ * 获取指定名称的图层实例
302
+ * @param {string} layerName - 图层名称
303
+ * @returns {Object|null} 图层控制器实例
304
+ */
305
+ getLayer(layerName) {
306
+ return this.layerInstances.get(layerName);
307
+ }
308
+
309
+ /**
310
+ * 移除指定名称的图层
311
+ * @param {string} layerName - 图层名称
312
+ * @returns {boolean} 是否移除成功
313
+ */
314
+ removeLayer(layerName) {
315
+ const layerController = this.layerInstances.get(layerName);
316
+
317
+ if (layerController) {
318
+ // 隐藏图层
319
+ if (typeof layerController.hideLayer === 'function') {
320
+ layerController.hideLayer();
321
+ }
322
+
323
+ // 从映射中移除
324
+ this.layerInstances.delete(layerName);
325
+ return true;
326
+ }
327
+
328
+ return false;
329
+ }
330
+
331
+ /**
332
+ * 获取所有图层实例
333
+ * @returns {Array} 图层控制器实例数组
334
+ */
335
+ getAllLayers() {
336
+ return Array.from(this.layerInstances.values());
337
+ }
338
+
339
+ /**
340
+ * 显示指定名称的图层
341
+ * @param {string} layerName - 图层名称
342
+ * @returns {boolean} 是否显示成功
343
+ */
344
+ showLayer(layerName) {
345
+ const layerController = this.layerInstances.get(layerName);
346
+
347
+ if (layerController && typeof layerController.showLayer === 'function') {
348
+ layerController.showLayer();
349
+ return true;
350
+ }
351
+
352
+ return false;
353
+ }
354
+
355
+ /**
356
+ * 隐藏指定名称的图层
357
+ * @param {string} layerName - 图层名称
358
+ * @returns {boolean} 是否隐藏成功
359
+ */
360
+ hideLayer(layerName) {
361
+ const layerController = this.layerInstances.get(layerName);
362
+
363
+ if (layerController && typeof layerController.hideLayer === 'function') {
364
+ layerController.hideLayer();
365
+ return true;
366
+ }
367
+
368
+ return false;
369
+ }
370
+ }
371
+ // 加载工具类方法到gdMapUtils中
372
+ Object.assign(GdMapUtils.prototype, {
373
+ ...eventMixin,
374
+ ...gdHelperMixin,
375
+ });
376
+
377
+ export default GdMapUtils;