gdmap-utils 1.2.3 → 1.2.5

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 (38) hide show
  1. package/.husky/pre-commit +1 -1
  2. package/.prettierrc.json +17 -17
  3. package/README.md +467 -2
  4. package/dist/index.js +127 -7
  5. package/dist/index.js.map +1 -1
  6. package/docs/classes/MapUtils.md +153 -0
  7. package/docs/functions/createMapUtils.md +18 -0
  8. package/docs/functions/initMapSource.md +14 -0
  9. package/docs/globals.md +11 -0
  10. package/examples/1_init.html +23 -23
  11. package/examples/2_mapInit.html +48 -48
  12. package/examples/3_MarkerLayer.html +565 -565
  13. package/examples/4_LabelMarkerLayer.html +574 -574
  14. package/examples/5_markerCluster.html +528 -0
  15. package/index.html +134 -134
  16. package/package.json +54 -51
  17. package/scripts/cleanDocs.js +220 -0
  18. package/scripts/mergeDocs.js +129 -0
  19. package/src/LayerManager.ts +17 -1
  20. package/src/MapSourceImport.ts +23 -23
  21. package/src/MapUtils.ts +198 -22
  22. package/src/gdMap/gdHelper.ts +113 -85
  23. package/src/index.ts +3 -1
  24. package/src/layers/baseMarkerLayer/LabelMarkerLayer.ts +240 -240
  25. package/src/layers/baseMarkerLayer/MarkerLayer.ts +208 -208
  26. package/src/layers/baseMarkerLayer/index.ts +369 -354
  27. package/src/layers/clusterMarkerLayer/MarkerClusterLayer.ts +53 -53
  28. package/src/layers/clusterMarkerLayer/index.ts +204 -177
  29. package/src/layers/index.ts +9 -9
  30. package/src/types/MapUtils.d.ts +53 -53
  31. package/src/types/amap.d.ts +11 -11
  32. package/src/types/{BaseMarkerLayer.d.ts → baseMarkerLayer.d.ts} +86 -87
  33. package/src/types/clusterMarkerLayer.d.ts +89 -88
  34. package/src/types/index.d.ts +14 -14
  35. package/tsconfig.json +26 -26
  36. package/typedoc.json +22 -0
  37. package/webpack.config.js +125 -126
  38. package/src/gdMap/gdHelper.js +0 -194
@@ -0,0 +1,129 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const docsDir = path.join(__dirname, '../docs');
5
+ const srcDir = path.join(__dirname, '../src');
6
+ const outputFile = path.join(docsDir, 'COMPLETE_API.md');
7
+
8
+ // 读取README.md作为文档头部
9
+ const readmePath = path.join(docsDir, 'README.md');
10
+ let readmeContent = '';
11
+ if (fs.existsSync(readmePath)) {
12
+ readmeContent = fs.readFileSync(readmePath, 'utf8');
13
+ }
14
+
15
+ // 要处理的源文件
16
+ const sourceFiles = [
17
+ {
18
+ path: path.join(srcDir, 'layers', 'baseMarkerLayer', 'index.ts'),
19
+ name: 'BaseMarkerLayer'
20
+ },
21
+ {
22
+ path: path.join(srcDir, 'layers', 'clusterMarkerLayer', 'index.ts'),
23
+ name: 'ClusterMarkerLayer'
24
+ }
25
+ ];
26
+
27
+ // 从TypeScript源文件中提取文档注释
28
+ function extractDocumentationFromSource(filePath, className) {
29
+ const content = fs.readFileSync(filePath, 'utf8');
30
+ let documentation = '';
31
+
32
+ // 提取类文档注释
33
+ const classCommentMatch = content.match(/\/\*\*[\s\S]*?\*\/\s*class\s+\w+/);
34
+ if (classCommentMatch) {
35
+ documentation += classCommentMatch[0].replace(/class\s+\w+/, '') + '\n\n';
36
+ }
37
+
38
+ // 提取构造函数
39
+ const constructorMatch = content.match(/constructor\s*\([^)]*\)\s*\{/);
40
+ if (constructorMatch) {
41
+ let constructorStr = constructorMatch[0].replace(/\{/, '');
42
+ // 移除类型注解
43
+ constructorStr = constructorStr.replace(/:\s*[^,]+/g, '');
44
+ documentation += '## Constructors\n\n';
45
+ documentation += '### Constructor\n';
46
+ documentation += `> **new ${className}**${constructorStr}\n\n`;
47
+ }
48
+
49
+ // 提取属性(去重)
50
+ const propertiesMatch = content.match(/\w+:\s*[^;]+;/g);
51
+ if (propertiesMatch) {
52
+ const uniqueProps = [...new Set(propertiesMatch.map(prop => prop.match(/\w+/)[0]))];
53
+ documentation += '## Properties\n\n';
54
+ uniqueProps.forEach(propName => {
55
+ documentation += `### ${propName}\n`;
56
+ documentation += `> **${propName}**\n\n`;
57
+ });
58
+ }
59
+
60
+ // 提取方法
61
+ const methodMatch = content.match(/\w+\s*\([^)]*\)\s*[:{]/g);
62
+ if (methodMatch) {
63
+ documentation += '## Methods\n\n';
64
+ methodMatch.forEach(method => {
65
+ const methodName = method.match(/\w+/)[0];
66
+ if (methodName !== 'constructor') {
67
+ let methodStr = method.replace(/:\s*$/, '');
68
+ // 移除类型注解
69
+ methodStr = methodStr.replace(/:\s*[^,]+/g, '');
70
+ documentation += `### ${methodName}\n`;
71
+ documentation += `> **${methodName}**${methodStr.replace(methodName, '')}\n\n`;
72
+ }
73
+ });
74
+ }
75
+
76
+ return documentation;
77
+ }
78
+
79
+ // 合并所有文档
80
+ function mergeDocs() {
81
+ console.log('开始合并文档...');
82
+
83
+ // 构建文档内容
84
+ let mergedContent = '';
85
+
86
+ // 添加README内容
87
+ if (readmeContent) {
88
+ mergedContent += readmeContent + '\n\n';
89
+ mergedContent += '---\n\n';
90
+ mergedContent += '# API 文档\n\n';
91
+ }
92
+
93
+ // 添加目录
94
+ mergedContent += '## 目录\n\n';
95
+ sourceFiles.forEach(file => {
96
+ mergedContent += `- [${file.name}](#${file.name.toLowerCase()})\n`;
97
+ });
98
+ mergedContent += '\n---\n\n';
99
+
100
+ // 添加各个文档内容
101
+ sourceFiles.forEach(file => {
102
+ const documentation = extractDocumentationFromSource(file.path, file.name);
103
+
104
+ mergedContent += `## ${file.name}\n\n`;
105
+ mergedContent += `<a id="${file.name.toLowerCase()}"></a>\n\n`;
106
+ mergedContent += documentation + '\n\n';
107
+ mergedContent += '---\n\n';
108
+ });
109
+
110
+ // 移除末尾多余的分隔符
111
+ mergedContent = mergedContent.replace(/\n*\*\*\n*$/, '');
112
+
113
+ // 添加生成信息
114
+ mergedContent += '\n\n---\n\n';
115
+ mergedContent += `*最后更新时间: ${new Date().toLocaleString('zh-CN')}*`;
116
+
117
+ // 写入合并后的文档
118
+ fs.writeFileSync(outputFile, mergedContent, 'utf8');
119
+ console.log(`文档合并完成: ${outputFile}`);
120
+ console.log(`总字数: ${mergedContent.length}`);
121
+ }
122
+
123
+ // 执行合并
124
+ try {
125
+ mergeDocs();
126
+ } catch (error) {
127
+ console.error('文档合并失败:', error);
128
+ process.exit(1);
129
+ }
@@ -2,6 +2,7 @@ import type {
2
2
  BaseMarkerLayerClass,
3
3
  ClusterMarkerLayerClass,
4
4
  } from '@/layers/index.ts';
5
+ import { MapUtils } from '@/MapUtils';
5
6
 
6
7
  type LayerIns =
7
8
  | InstanceType<BaseMarkerLayerClass>
@@ -11,7 +12,8 @@ export class LayerManager {
11
12
 
12
13
  addLayer(layer: LayerIns) {
13
14
  if (this.hasLayer(layer)) {
14
- throw new Error(`Layer with name "${layer.layerName}" already exists`);
15
+ const errorMsg = `Layer with name "${layer.layerName}" already exists`;
16
+ return MapUtils.error(errorMsg);
15
17
  }
16
18
  this.layers.set(layer.layerName, layer);
17
19
  }
@@ -24,6 +26,9 @@ export class LayerManager {
24
26
  let layer = this.layers.get(LayerName);
25
27
  if (layer) {
26
28
  layer.show();
29
+ } else {
30
+ const errorMsg = `Layer with name "${LayerName}" not found`;
31
+ return MapUtils.error(errorMsg);
27
32
  }
28
33
  }
29
34
 
@@ -31,6 +36,9 @@ export class LayerManager {
31
36
  let layer = this.layers.get(LayerName);
32
37
  if (layer) {
33
38
  layer.hide();
39
+ } else {
40
+ const errorMsg = `Layer with name "${LayerName}" not found`;
41
+ return MapUtils.error(errorMsg);
34
42
  }
35
43
  }
36
44
 
@@ -53,5 +61,13 @@ export class LayerManager {
53
61
  hasLayer(layer: LayerIns): boolean {
54
62
  return this.layers.has(layer.layerName);
55
63
  }
64
+
65
+ clear() {
66
+ this.layers.forEach(layer => {
67
+ if (typeof (layer as any).destroy === 'function') {
68
+ (layer as any).destroy();
69
+ }
70
+ });
71
+ }
56
72
  }
57
73
  export default LayerManager;
@@ -1,23 +1,23 @@
1
- import AMapLoader from '@amap/amap-jsapi-loader';
2
- import type { AMap, loaderOpts } from './types/amap.d.ts'; //TODO 项目配置ts自动引入,移除导入信息
3
-
4
- class MapSourceImport {
5
- static async loadScript(opts: loaderOpts) {
6
- // 某些API加载前必须设置秘钥
7
- // window._AMapSecurityConfig = {
8
- // securityJsCode: process.env.VUE_APP_GD_MAP_CODE, // 安全密钥
9
- // };
10
- // 确保每次的AMap都是合法的所以不分开创建
11
- return AMapLoader.load(opts)
12
- .then((rawAMap: typeof AMap) => {
13
- // 将 AMap 全局对象挂载到 window 上
14
- return rawAMap;
15
- })
16
- .catch(e => {
17
- console.error(e);
18
- throw new Error(e);
19
- });
20
- }
21
- }
22
-
23
- export default MapSourceImport;
1
+ import AMapLoader from '@amap/amap-jsapi-loader';
2
+ import type { AMap, loaderOpts } from './types/amap.d.ts'; //TODO 项目配置ts自动引入,移除导入信息
3
+
4
+ class MapSourceImport {
5
+ static async loadScript(opts: loaderOpts) {
6
+ // 某些API加载前必须设置秘钥
7
+ // window._AMapSecurityConfig = {
8
+ // securityJsCode: process.env.VUE_APP_GD_MAP_CODE, // 安全密钥
9
+ // };
10
+ // 确保每次的AMap都是合法的所以不分开创建
11
+ return AMapLoader.load(opts)
12
+ .then((rawAMap: typeof AMap) => {
13
+ // 将 AMap 全局对象挂载到 window 上
14
+ return rawAMap;
15
+ })
16
+ .catch(e => {
17
+ console.error(e);
18
+ throw new Error(e);
19
+ });
20
+ }
21
+ }
22
+
23
+ export default MapSourceImport;
package/src/MapUtils.ts CHANGED
@@ -9,8 +9,12 @@ import type {
9
9
  LayerOpts,
10
10
  ClusterMarkerLayerOpts,
11
11
  } from './types/index.d';
12
- import { BaseMarkerLayer, ClusterMarkerLayer } from './layers/index';
13
- import type { BaseMarkerLayerIns, ClusterMarkerLayerIns } from './layers';
12
+ import {
13
+ BaseMarkerLayerIns,
14
+ BaseMarkerLayer,
15
+ ClusterMarkerLayerIns,
16
+ ClusterMarkerLayer,
17
+ } from './layers/index';
14
18
  import type { SetOptional, Simplify } from 'type-fest';
15
19
  import LayerManager from './LayerManager';
16
20
 
@@ -26,18 +30,42 @@ export class MapUtils {
26
30
 
27
31
  LayerManager: LayerManager = new LayerManager();
28
32
 
33
+ /**
34
+ *@category 高德地图工具
35
+ */
29
36
  static createAMapMarker = MapMixin.createAMapMarker;
30
-
37
+ /**
38
+ *@category 高德地图工具
39
+ */
31
40
  static createIcon = MapMixin.createIcon;
32
-
41
+ /**
42
+ *@category 高德地图工具
43
+ */
33
44
  static Size = MapMixin.Size;
34
-
45
+ /**
46
+ *@category 高德地图工具
47
+ */
35
48
  static Pixel = MapMixin.Pixel;
36
-
49
+ /**
50
+ *@category 高德地图工具
51
+ */
37
52
  static LngLat = MapMixin.LngLat;
38
-
53
+ /**
54
+ *@category 高德地图工具
55
+ */
39
56
  static createAMapInfoWindow = MapMixin.createAMapInfoWindow;
57
+ /**
58
+ *@category 高德地图工具
59
+ */
60
+ static createAMapPolyline = MapMixin.createAMapPolyline;
61
+ /**
62
+ *@category 高德地图工具
63
+ */
64
+ static loadPlugins = MapMixin.loadPlugins;
40
65
 
66
+ /**
67
+ * @ignore
68
+ */
41
69
  constructor(
42
70
  opts: MapUtilsOpts[keyof MapUtilsOpts],
43
71
  AMap: Simplify<typeof gdAMap>
@@ -61,26 +89,36 @@ export class MapUtils {
61
89
  this.map = this.initMap(mountSelector, rest); //地图初始化
62
90
  }
63
91
  }
64
-
65
- this.bindMapClickEvent();
66
92
  }
67
93
 
68
- // 初始化绑定地图事件
69
- bindMapClickEvent() {
70
- this.map.on('click', () => {
71
- // if (this.clickMapRestMarkers) {
72
- // this.overlayGroupManagerMap.forEach((overlayGroup) => {
73
- // overlayGroup.resetActiveMarker(); // 清除图层上的所有覆盖物
74
- // });
75
- // }
76
- });
94
+ /**
95
+ * @param {AMap.EventType} type 地图事件类型
96
+ * @param {(e: any) => void} fn 回调函数
97
+ * @param {*} [context] 事件上下文,缺省为当前实例
98
+ * @param {boolean} [once] 事件是否执行一次
99
+ * @memberof MapUtils
100
+ */
101
+ bindMapClickEvent(
102
+ type: AMap.EventType,
103
+ fn: (e: any) => void,
104
+ context?: any,
105
+ once?: boolean
106
+ ) {
107
+ this.map.on(type, fn, context, once);
77
108
  }
78
109
 
110
+ /**
111
+ * @ignore
112
+ */
79
113
  initMap(id: string, opts: MapOptions): mapIns {
80
114
  //参数要作检验吗
81
115
  return new window.AMap.Map(id, opts);
82
116
  }
83
117
 
118
+ /**
119
+ * @ignore
120
+ * @return {*}
121
+ */
84
122
  createBaseMarkerLayer<
85
123
  U extends {},
86
124
  T extends MarkerLayerBaseType = 'markerLayer',
@@ -91,16 +129,25 @@ export class MapUtils {
91
129
  return layer;
92
130
  }
93
131
 
132
+ /**
133
+ * 高德地图中 `MarkerCluster, MassMarks`用于渲染海量点,它们调用方式也比较接近,将其聚合为一个统一的图层管理
134
+ * 目前, 聚合图层只包装`MarkerCluster`
135
+ * @param {ClusterMarkerLayerOpts<U, T>} opts - 图层配置选项
136
+ */
94
137
  createClusterMarkerLayer<
95
138
  U extends {},
96
139
  T extends MarkerClusterLayerType = 'markerClusterLayer',
97
140
  >(opts: ClusterMarkerLayerOpts<U, T>) {
98
141
  const layer = new ClusterMarkerLayer<U, T>(opts, this);
99
-
142
+ // @ts-ignore
100
143
  this.LayerManager.addLayer(layer);
101
144
  return layer;
102
145
  }
103
146
 
147
+ /**
148
+ * `removeLayer`接收图层实例将其从mapUtils的关联中移除
149
+ * @param {(BaseMarkerLayerIns | ClusterMarkerLayerIns)} layer
150
+ */
104
151
  removeLayer(layer: BaseMarkerLayerIns | ClusterMarkerLayerIns) {
105
152
  const isLayerExist = this.LayerManager.hasLayer(layer);
106
153
 
@@ -122,13 +169,43 @@ export class MapUtils {
122
169
  this.map.setFitView(...opts);
123
170
  }
124
171
 
172
+ /**
173
+ * 地图弹窗关闭
174
+ * @memberof MapUtils
175
+ */
125
176
  clearInfoWindow() {
126
177
  this.map.clearInfoWindow();
127
178
  }
128
179
 
180
+ /**
181
+ * 销毁地图实例,释放资源
182
+ * - 清空所有图层
183
+ * - 销毁地图实例
184
+ * - 清理 LayerManager
185
+ * @memberof MapUtils
186
+ */
187
+ destroyMap() {
188
+ // 清空所有图层
189
+ this.LayerManager.clear();
190
+ // 销毁地图实例
191
+ this.map.destroy();
192
+ }
193
+
194
+ /**
195
+ * 地图跳转参数
196
+ * @typedef {Object} GotoOpts
197
+ * @property {number} zoom - 目标缩放级别
198
+ * @property {AMap.LngLat|[number,number]} center - 目标中心点(高德坐标实例或 [lng,lat] 数组)
199
+ * @property {boolean} [immediately=false] - 是否立即跳转,默认平滑过渡
200
+ * @property {number} [duration] - 过渡动画时长(毫秒)
201
+ */
202
+ /**
203
+ * 将地图视图切换到指定状态
204
+ * @param {GotoOpts} opts 跳转配置
205
+ */
129
206
  seZoomAndCenter(opts: {
130
- zoom: number;
131
- center: AMap.LngLat | [number, number];
207
+ zoom?: number;
208
+ center?: AMap.LngLat | [number, number];
132
209
  immediately?: boolean;
133
210
  duration?: number;
134
211
  }): void {
@@ -140,6 +217,47 @@ export class MapUtils {
140
217
  this.map.setZoomAndCenter(zoom, center, immediately, duration);
141
218
  }
142
219
 
220
+ /**
221
+ * 图层`layerName`显示, `layerName`为创建图层所传递的参数,用于标识图层名称。
222
+ * @param {string} layerName - 图层名称
223
+ */
224
+ showLayer(layerName: string): void {
225
+ this.LayerManager.show(layerName);
226
+ }
227
+
228
+ /**
229
+ * 图层`layerName`隐藏, `layerName`为创建图层所传递的参数,用于标识图层名称。
230
+ * @param {string} layerName - 图层名称
231
+ */
232
+ hideLayer(layerName: string): void {
233
+ this.LayerManager.hide(layerName);
234
+ }
235
+
236
+ /**
237
+ * 地图显示所有图层
238
+ */
239
+ showAllLayers(): void {
240
+ this.LayerManager.showAll();
241
+ }
242
+
243
+ /**
244
+ * 地图隐藏所有图层
245
+ */
246
+ hideAllLayers(): void {
247
+ this.LayerManager.hideAll();
248
+ }
249
+
250
+ /**
251
+ * 地图重新渲染所有图层
252
+ */
253
+ reloadLayers(): void {
254
+ this.LayerManager.reload();
255
+ }
256
+
257
+ /**
258
+ * @ignore
259
+ * @param msg
260
+ */
143
261
  static error(msg: string) {
144
262
  console.error(`[MapUtils Error]:${msg}`);
145
263
  }
@@ -147,6 +265,45 @@ export class MapUtils {
147
265
 
148
266
  export type { mapUtilsIns, MapUtilsConstructor };
149
267
 
268
+ /**
269
+ *
270
+ * `createMapUtils`功能如下:
271
+ * 1. 支持将高德地图实例包装成工具函数 (为其扩展功能),
272
+ * 2. 地图创建并为其扩展功能,
273
+ * 3. 高德地图依赖加载 ( 高德AMap函数加载 )
274
+ * @param {MapUtilsOpts[keyof MapUtilsOpts]} opts
275
+ * - 工具函数配置选项,支持两种模式:
276
+ * 1. 使用现有地图实例(MapUtilsUseExistingOpts)
277
+ * 2. 创建新地图实例(MapUtilsCreateOpts)
278
+ *
279
+ * [MapOptions文档地址](https://lbs.amap.com/api/javascript-api-v2/documentation#map)
280
+ * ```
281
+ * interface MapUtilsCreateOpts extends MapOptions {
282
+ mountSelector: string; //选择器
283
+ }
284
+
285
+ interface MapUtilsUseExistingOpts extends MapOptions {
286
+ mapIns: mapIns; //高德地图实例
287
+ }
288
+ * ```
289
+ * @param {loaderOpts} [loaderOPts]
290
+ * - 高德地图环境加载所传递的配置对象
291
+ * ```
292
+ * type loaderOpts = {
293
+ key: string;
294
+ version: string;
295
+ plugins?: string[] | undefined;
296
+ AMapUI?: {
297
+ version?: string | undefined;
298
+ plugins?: string[] | undefined;
299
+ } | undefined;
300
+ Loca?: {
301
+ version?: string | undefined;
302
+ } | undefined;
303
+ }
304
+ * ```
305
+ * @return {*}
306
+ */
150
307
  export async function createMapUtils(
151
308
  opts: MapUtilsOpts[keyof MapUtilsOpts],
152
309
  loaderOPts?: loaderOpts
@@ -161,7 +318,26 @@ export async function createMapUtils(
161
318
  return new MapUtils(opts, AMap);
162
319
  }
163
320
 
164
- // 地图拆分
321
+ /**
322
+ *
323
+ * @param {loaderOpts} Opts
324
+ * - 高德地图环境加载所传递的配置对象
325
+ * ```
326
+ * type loaderOpts = {
327
+ key: string;
328
+ version: string;
329
+ plugins?: string[] | undefined;
330
+ AMapUI?: {
331
+ version?: string | undefined;
332
+ plugins?: string[] | undefined;
333
+ } | undefined;
334
+ Loca?: {
335
+ version?: string | undefined;
336
+ } | undefined;
337
+ }
338
+ * ```
339
+ * @return {*}
340
+ */
165
341
  export async function initMapSource(Opts: loaderOpts) {
166
342
  return await MapSourceImport.loadScript(Opts);
167
343
  }