hn-map 1.1.14 → 1.1.16

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.
@@ -4,9 +4,9 @@ import {
4
4
  wgs84ToGcj02Format,
5
5
  convertPosition,
6
6
  } from "../util";
7
- import mars3d_entity from "../base/mars3d_entity";
8
- import gaode_entity from "../base/gaode_entity";
9
- import siji_entity from "../base/siji_entity";
7
+ import Mars3dEntity from "../base/mars3d_entity";
8
+ import GaodeEntity from "../base/gaode_entity";
9
+ import SijiEntity from "../base/siji_entity";
10
10
 
11
11
  export default (hnMap: any) => {
12
12
  const defaultOption = {
@@ -37,7 +37,7 @@ export default (hnMap: any) => {
37
37
  data: null,
38
38
  };
39
39
 
40
- class mars3d_class extends mars3d_entity {
40
+ class mars3d_class extends Mars3dEntity {
41
41
  type: any = "point";
42
42
  id: any = null;
43
43
  option: any = JSON.parse(JSON.stringify(defaultOption));
@@ -105,7 +105,7 @@ export default (hnMap: any) => {
105
105
  }
106
106
  }
107
107
 
108
- class gaode_class extends gaode_entity {
108
+ class gaode_class extends GaodeEntity {
109
109
  id: any = null;
110
110
  option: any = JSON.parse(JSON.stringify(defaultOption));
111
111
  config: any = null;
@@ -144,7 +144,7 @@ export default (hnMap: any) => {
144
144
  }
145
145
  }
146
146
 
147
- class siji_class extends siji_entity {
147
+ class siji_class extends SijiEntity {
148
148
  type: any = "point";
149
149
  id: any = null;
150
150
  option: any = JSON.parse(JSON.stringify(defaultOption));
@@ -180,7 +180,11 @@ export default (hnMap: any) => {
180
180
  ],
181
181
  },
182
182
  },
183
-
183
+ layout: {
184
+ "text-field": "{name}", // 文本
185
+ "text-size": Number(option.fontSize),
186
+ "text-offset": convertPosition(option.fontOffset),
187
+ },
184
188
  paint: {
185
189
  "circle-opacity": Number(option.opacity),
186
190
  "circle-radius": Number(option.size),
@@ -194,6 +198,7 @@ export default (hnMap: any) => {
194
198
  "circle-stroke-opacity": option.outline
195
199
  ? Number(option.outlineOpacity)
196
200
  : 1, // 边框样式
201
+
197
202
  }, // 填充样式
198
203
  };
199
204
 
@@ -4,9 +4,9 @@ import {
4
4
  wgs84ToGcj02Format,
5
5
  convertPosition,
6
6
  } from "../util";
7
- import mars3d_entity from "../base/mars3d_entity";
8
- import gaode_entity from "../base/gaode_entity";
9
- import siji_entity from "../base/siji_entity";
7
+ import Mars3dEntity from "../base/mars3d_entity";
8
+ import GaodeEntity from "../base/gaode_entity";
9
+ import SijiEntity from "../base/siji_entity";
10
10
  export default (hnMap: any) => {
11
11
  const defaultOption = {
12
12
  id: "",
@@ -24,7 +24,7 @@ export default (hnMap: any) => {
24
24
  data: null,
25
25
  };
26
26
 
27
- class mars3d_class extends mars3d_entity {
27
+ class mars3d_class extends Mars3dEntity {
28
28
  type: any = "polygon";
29
29
  id: any = null;
30
30
  option: any = JSON.parse(JSON.stringify(defaultOption));
@@ -73,7 +73,7 @@ export default (hnMap: any) => {
73
73
  }
74
74
  }
75
75
 
76
- class gaode_class extends gaode_entity {
76
+ class gaode_class extends GaodeEntity {
77
77
  id: any = null;
78
78
  option: any = JSON.parse(JSON.stringify(defaultOption));
79
79
  config: any = null;
@@ -111,7 +111,7 @@ export default (hnMap: any) => {
111
111
  }
112
112
  }
113
113
 
114
- class siji_class extends siji_entity {
114
+ class siji_class extends SijiEntity {
115
115
  type: any = "polygon";
116
116
  id: any = null;
117
117
  option: any = JSON.parse(JSON.stringify(defaultOption));
@@ -166,7 +166,7 @@ export default (hnMap: any) => {
166
166
  type: "Feature",
167
167
  geometry: {
168
168
  type: "Polygon",
169
- coordinates: convertPosition(option.position), //三层数组[[[0,0],[0,0]]]
169
+ coordinates: convertPosition(this.option.position), //三层数组[[[0,0],[0,0]]]
170
170
  },
171
171
  },
172
172
  ],
@@ -4,9 +4,9 @@ import {
4
4
  createRectangleCoordinates,
5
5
  getLevelMiddleHeight,
6
6
  } from "../util";
7
- import mars3d_entity from "../base/mars3d_entity";
8
- import gaode_entity from "../base/gaode_entity";
9
- import siji_entity from "../base/siji_entity";
7
+ import Mars3dEntity from "../base/mars3d_entity";
8
+ import GaodeEntity from "../base/gaode_entity";
9
+ import SijiEntity from "../base/siji_entity";
10
10
  export default (hnMap: any) => {
11
11
  const defaultOption = {
12
12
  id: "",
@@ -24,7 +24,7 @@ export default (hnMap: any) => {
24
24
  data: null,
25
25
  };
26
26
 
27
- class mars3d_class extends mars3d_entity {
27
+ class mars3d_class extends Mars3dEntity {
28
28
  type: any = "retangle";
29
29
  id: any = null;
30
30
  option: any = JSON.parse(JSON.stringify(defaultOption));
@@ -73,7 +73,7 @@ export default (hnMap: any) => {
73
73
  }
74
74
  }
75
75
 
76
- class gaode_class extends gaode_entity {
76
+ class gaode_class extends GaodeEntity {
77
77
  id: any = null;
78
78
  option: any = JSON.parse(JSON.stringify(defaultOption));
79
79
  config: any = null;
@@ -113,7 +113,7 @@ export default (hnMap: any) => {
113
113
  this.graphic.setBounds(this.config.bounds);
114
114
  }
115
115
  }
116
- class siji_class extends siji_entity {
116
+ class siji_class extends SijiEntity {
117
117
  type: any = "rectangle";
118
118
  id: any = null;
119
119
  option: any = JSON.parse(JSON.stringify(defaultOption));
package/src/index.ts CHANGED
@@ -14,91 +14,127 @@ import heatMap from "./layer/heatMap";
14
14
  import cluster from "./layer/cluster";
15
15
  import pointCloud from "./layer/pointCloud";
16
16
  import geoJson from "./layer/geoJson";
17
- import {mars3dTileErrorHandler} from "./util";
17
+ import { mars3dTileErrorHandler } from "./util";
18
18
 
19
19
  /**
20
20
  * HnMap类是一个地图工具类,用于创建和管理地图对象及其相关元素
21
+ * 支持mars3d、高德地图和思极地图三种地图引擎
21
22
  */
22
- interface option {
23
- lng: number;
24
- lat: number;
25
- level: number;
26
- pitch: number;
27
- gaode_key: string;
28
- sj_app_key: string;
29
- sj_app_secret: string;
30
- sj_js_url: string;
31
- sj_style: string;
32
- sj_route_net: boolean;
23
+
24
+ /**
25
+ * 地图初始化配置选项
26
+ */
27
+ interface MapOption {
28
+ lng: number; // 地图中心点经度
29
+ lat: number; // 地图中心点纬度
30
+ level: number; // 地图初始缩放级别
31
+ pitch: number; // 地图初始俯仰角度
32
+ gaode_key: string; // 高德地图API密钥
33
+ sj_app_key: string; // 思极地图应用密钥
34
+ sj_app_secret: string; // 思极地图应用密钥
35
+ sj_js_url: string; // 思极地图JS API加载地址
36
+ sj_style: string; // 思极地图样式
37
+ sj_route_net: boolean; // 是否显示思极地图路网
38
+ // 添加Cesium相关配置
39
+ cesium_accessToken: string; // Cesium Ion访问令牌
40
+ cesium_baseUrl: string; // Cesium静态资源基础路径
41
+ cesium_terrainProvider: any; // 地形提供者配置
42
+ cesium_imageryProvider: any; // 影像提供者配置
43
+ cesium_options: {};
33
44
  }
34
45
 
46
+ /**
47
+ * HnMap主类,提供统一的地图操作API
48
+ */
35
49
  export default class HnMap {
36
- id: any = null;
37
- option: any = null;
38
- mapType: any = null;
39
- map: any = null;
40
- Layer: any = null;
41
- Point: any = null;
42
- NumPoint: any = null;
43
- ImagePoint: any = null;
44
- DivPoint: any = null;
45
- Label: any = null;
46
- Line: any = null;
47
- Circle: any = null;
48
- Rectangle: any = null;
49
- Polygon: any = null;
50
- Route: any = null;
51
- HeatMap: any = null;
52
- Cluster: any = null;
53
- PointCloud: any = null;
54
-
55
- GeoJson: any = null;
56
-
57
- // 允许构造函数
50
+ id: string | null = null; // 地图容器ID
51
+ option: MapOption | null = null; // 地图配置选项
52
+ mapType: string | null = null; // 地图类型:mars3d、gaode、siji
53
+ map: any = null; // 底层地图实例
54
+ Layer: any = null; // 图层管理实例
55
+ Point: any = null; // 点要素类
56
+ NumPoint: any = null; // 数字点要素类
57
+ ImagePoint: any = null; // 图片点要素类
58
+ DivPoint: any = null; // DOM点要素类
59
+ Label: any = null; // 标签要素类
60
+ Line: any = null; // 线要素类
61
+ Circle: any = null; // 圆要素类
62
+ Rectangle: any = null; // 矩形要素类
63
+ Polygon: any = null; // 多边形要素类
64
+ Route: any = null; // 路线规划类
65
+ HeatMap: any = null; // 热力图类
66
+ Cluster: any = null; // 聚类图层类
67
+ PointCloud: any = null; // 点云图层类
68
+ GeoJson: any = null; // GeoJSON图层类
69
+
70
+ // 构造函数访问控制标志
58
71
  private static allowConstruction = false;
59
72
 
60
73
  /**
61
74
  * HnMap类的构造函数
75
+ * @private 私有构造函数,只能通过create静态方法创建实例
62
76
  * @param id 地图容器的ID
63
77
  * @param option 地图的配置选项
64
78
  * @param mapType 地图的类型
65
- * @param map
79
+ * @param map 底层地图实例
66
80
  */
67
- private constructor(id: string, option: option, mapType: string, map: any) {
81
+ private constructor(
82
+ id: string,
83
+ option: MapOption,
84
+ mapType: string,
85
+ map: any
86
+ ) {
68
87
  if (!HnMap.allowConstruction) {
69
88
  throw new Error("请使用 HnMap.create() 创建实例");
70
89
  }
71
90
  this.id = id;
72
91
  this.option = option;
73
92
  this.mapType = mapType;
74
- this.map = map; // 此时 map 已创建
93
+ this.map = map; // 此时map已创建完成
75
94
  }
76
95
 
77
96
  /**
78
- * 创建HnMap实例的方法
79
- * @param id 地图容器的ID
97
+ * 创建HnMap实例的静态方法
98
+ * @param id 地图容器的DOM元素ID
80
99
  * @param option 地图的配置选项
81
- * @param mapType 用户期望的地图类型(如 'gaode', 'siji')
100
+ * @param mapType 用户期望的地图类型(如 'mars3d', 'gaode', 'siji')
101
+ * @returns Promise<HnMap> 返回HnMap实例的Promise
102
+ * @example
103
+ * ```javascript
104
+ * HnMap.create('mapContainer', {
105
+ * lng: 112.55074,
106
+ * lat: 37.7979042,
107
+ * level: 11,
108
+ * gaode_key: 'your-gaode-key',
109
+ * sj_app_key: 'your-siji-key',
110
+ * sj_app_secret: 'your-siji-secret'
111
+ * }, 'mars3d').then(map => {
112
+ * // 使用地图实例
113
+ * });
114
+ * ```
82
115
  */
83
- static async create(id: string, option: option, mapType: string) {
116
+ static async create(id: string, option: MapOption, mapType: string) {
117
+ // 获取资源加载的基础路径
84
118
  const basePath = window.location.pathname.endsWith("/")
85
- ? window.location.pathname
86
- : window.location.pathname.substring(
87
- 0,
88
- window.location.pathname.lastIndexOf("/") + 1
119
+ ? window.location.pathname
120
+ : window.location.pathname.substring(
121
+ 0,
122
+ window.location.pathname.lastIndexOf("/") + 1
89
123
  );
90
124
 
91
- // 预加载 turf(所有类型都需要)
125
+ // 预加载turf地理空间分析库(所有地图类型都需要)
92
126
  await loadResource(basePath + "lib/turf/turf.min.js", "js");
93
127
 
94
- // 尝试创建地图,支持 fallback
128
+ // 尝试创建地图,支持失败回退机制
95
129
  let innerMap: any = null;
96
130
  let finalMapType = mapType;
97
131
 
98
132
  try {
133
+ // 尝试创建指定类型的地图
99
134
  innerMap = await HnMap.tryCreateMap(id, option, mapType, basePath);
100
135
  } catch (error) {
101
136
  console.warn(`[${mapType}] 地图初始化失败,尝试回退到 mars3d`, error);
137
+ // 回退到mars3d地图
102
138
  finalMapType = "mars3d";
103
139
  try {
104
140
  innerMap = await HnMap.tryCreateMap(id, option, "mars3d", basePath);
@@ -108,65 +144,83 @@ export default class HnMap {
108
144
  }
109
145
  }
110
146
 
111
- // 创建 HnMap 实例
147
+ // 创建HnMap实例
112
148
  HnMap.allowConstruction = true;
113
149
  try {
114
150
  const instance = new HnMap(id, option, finalMapType, innerMap);
115
- instance.initModules();
151
+ instance.initModules(); // 初始化所有地图模块
116
152
  return instance;
117
153
  } finally {
154
+ // 恢复构造函数访问控制
118
155
  HnMap.allowConstruction = false;
119
156
  }
120
157
  }
121
158
 
122
159
  /**
123
- * 尝试根据 mapType 加载资源并创建地图实例
160
+ * 尝试根据地图类型加载资源并创建地图实例
161
+ * @private
162
+ * @param id 地图容器ID
163
+ * @param option 地图配置选项
164
+ * @param mapType 地图类型
165
+ * @param basePath 资源加载基础路径
166
+ * @returns Promise<any> 返回地图实例的Promise
124
167
  */
125
168
  private static async tryCreateMap(
126
- id: string,
127
- option: option,
128
- mapType: string,
129
- basePath: string
169
+ id: string,
170
+ option: MapOption,
171
+ mapType: string,
172
+ basePath: string
130
173
  ): Promise<any> {
131
- // 1. 加载对应地图的资源
174
+ // 1. 加载对应地图平台的资源
132
175
  switch (mapType) {
133
176
  case "mars3d":
177
+ // 加载Cesium资源
134
178
  await loadResource(basePath + "lib/Cesium/Widgets/widgets.css", "css");
135
179
  await loadResource(basePath + "lib/Cesium/Cesium.js", "js");
180
+ // 加载mars3d资源
136
181
  await loadResource(basePath + "lib/mars3d/mars3d.css", "css");
137
182
  await loadResource(basePath + "lib/mars3d/mars3d.js", "js");
183
+ // 加载mars3d热力图插件
138
184
  await loadResource(
139
- basePath + "lib/mars3d/plugins/heatmap/heatmap.js",
140
- "js"
185
+ basePath + "lib/mars3d/plugins/heatmap/heatmap.js",
186
+ "js"
141
187
  );
142
188
  await loadResource(
143
- basePath + "lib/mars3d/plugins/heatmap/mars3d-heatmap.js",
144
- "js"
189
+ basePath + "lib/mars3d/plugins/heatmap/mars3d-heatmap.js",
190
+ "js"
145
191
  );
146
192
 
147
- mars3dTileErrorHandler()
193
+ // 初始化mars3d瓦片错误处理
194
+ mars3dTileErrorHandler();
148
195
  break;
196
+
149
197
  case "gaode":
198
+ // 加载高德地图API
150
199
  await loadResource(
151
- `https://webapi.amap.com/maps?v=2.0&key=${option.gaode_key}&plugin=AMap.HeatMap,AMap.MarkerCluster,AMap.MoveAnimation`,
152
- "js"
200
+ `https://webapi.amap.com/maps?v=2.0&key=${option.gaode_key}&plugin=AMap.HeatMap,AMap.MarkerCluster,AMap.MoveAnimation`,
201
+ "js"
153
202
  );
203
+ // 加载3D模型所需的Three.js资源
154
204
  await loadResource(
155
- "https://a.amap.com/jsapi_demos/static/data3d/lib/three.117.js",
156
- "js"
205
+ "https://a.amap.com/jsapi_demos/static/data3d/lib/three.117.js",
206
+ "js"
157
207
  );
158
208
  await loadResource(
159
- "https://a.amap.com/jsapi_demos/static/data3d/lib/GLTFLoader.117.min.js",
160
- "js"
209
+ "https://a.amap.com/jsapi_demos/static/data3d/lib/GLTFLoader.117.min.js",
210
+ "js"
161
211
  );
162
212
  break;
213
+
163
214
  case "siji":
215
+ // 加载思极地图JS API
164
216
  await loadResource(option.sj_js_url, "js");
165
- // 注意:SGMap 可能是全局变量,需确保已挂载
217
+ // 检查SGMap全局变量是否已定义
166
218
  if (typeof SGMap === "undefined") {
167
219
  throw new Error("siji 地图 JS 加载成功但 SGMap 未定义");
168
220
  }
221
+ // 登录思极地图
169
222
  await SGMap.tokenTask.login(option.sj_app_key, option.sj_app_secret);
223
+ // 加载思极地图插件
170
224
  await SGMap.plugin([
171
225
  "SGMap.DrawPolygonHandler",
172
226
  "SGMap.DrawCircleHandler",
@@ -175,34 +229,78 @@ export default class HnMap {
175
229
  "SGMap.RoadNetLayer",
176
230
  ]);
177
231
  break;
232
+ case "cesium":
233
+ // 加载Cesium资源
234
+ await HnMap.loadCesiumResources(option, basePath);
235
+ break;
178
236
  default:
179
237
  throw new Error(`不支持的地图类型: ${mapType}`);
180
238
  }
181
239
 
182
- // 2. 创建地图对象
240
+ // 2. 创建地图对象实例
183
241
  const MapClass = map({ id, option, mapType });
184
- const innerMap = await MapClass.create(id, option); // 这里可能也会失败(如容器不存在、key无效等)
242
+ const innerMap = await MapClass.create(id, option); // 这里可能会失败(如容器不存在、key无效等)
185
243
  return innerMap;
186
244
  }
187
245
 
188
- // ✅ 添加一个初始化方法,用于注册所有图形类
246
+ /**
247
+ * 加载Cesium相关资源
248
+ */
249
+ private static async loadCesiumResources(
250
+ option: MapOption,
251
+ basePath: string
252
+ ): Promise<void> {
253
+ // 加载Cesium CSS
254
+ // await loadResource(basePath + "lib/Cesium/Widgets/widgets.css", "css");
255
+ await loadResource(
256
+ "https://cesium.com/downloads/cesiumjs/releases/1.107/Build/Cesium/Widgets/widgets.css",
257
+ "css"
258
+ );
259
+
260
+ // 加载Cesium JS
261
+ await loadResource(
262
+ "https://cesium.com/downloads/cesiumjs/releases/1.107/Build/Cesium/Cesium.js",
263
+ "js"
264
+ );
265
+
266
+ // 设置Cesium访问令牌(如果有)
267
+ if (option.cesium_accessToken) {
268
+ Cesium.Ion.defaultAccessToken = option.cesium_accessToken;
269
+ }
270
+
271
+ // 等待Cesium加载完成
272
+ await new Promise<void>((resolve) => {
273
+ const checkCesium = () => {
274
+ if (typeof Cesium !== "undefined" && Cesium.Viewer) {
275
+ resolve();
276
+ } else {
277
+ setTimeout(checkCesium, 100);
278
+ }
279
+ };
280
+ checkCesium();
281
+ });
282
+ }
283
+
284
+ /**
285
+ * 初始化地图模块,注册所有图形要素类
286
+ * @private
287
+ */
189
288
  private initModules() {
190
- this.Layer = layer(this);
191
- this.Point = point(this);
192
- this.NumPoint = numPoint(this);
193
- this.ImagePoint = imagePoint(this);
194
- this.DivPoint = divPoint(this);
195
- this.Label = label(this);
196
- this.Line = line(this);
197
- this.Circle = circle(this);
198
- this.Rectangle = rectangle(this);
199
- this.Polygon = polygon(this);
200
- this.Route = route(this);
201
- this.HeatMap = heatMap(this);
202
- this.Cluster = cluster(this);
203
- this.PointCloud = pointCloud(this);
204
-
205
- this.GeoJson = geoJson(this);
289
+ this.Layer = layer(this); // 基础图层类
290
+ this.Point = point(this); // 点要素类
291
+ this.NumPoint = numPoint(this); // 数字点要素类
292
+ this.ImagePoint = imagePoint(this); // 图片点要素类
293
+ this.DivPoint = divPoint(this); // DOM点要素类
294
+ this.Label = label(this); // 标签要素类
295
+ this.Line = line(this); // 线要素类
296
+ this.Circle = circle(this); // 圆要素类
297
+ this.Rectangle = rectangle(this); // 矩形要素类
298
+ this.Polygon = polygon(this); // 多边形要素类
299
+ this.Route = route(this); // 路线规划类
300
+ this.HeatMap = heatMap(this); // 热力图类
301
+ this.Cluster = cluster(this); // 聚类图层类
302
+ this.PointCloud = pointCloud(this); // 点云图层类
303
+ this.GeoJson = geoJson(this); // GeoJSON图层类
206
304
  }
207
305
  }
208
306