hn-map 1.0.9 → 1.1.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.
Files changed (45) hide show
  1. package/README.md +5 -22
  2. package/dist/index.js +2317 -609
  3. package/package.json +11 -8
  4. package/src/base/gaode_entity.ts +61 -0
  5. package/src/base/mars3d_entity.ts +64 -0
  6. package/src/base/siji_entity.ts +118 -0
  7. package/src/graphic/circle.ts +218 -0
  8. package/src/graphic/divPoint.ts +133 -0
  9. package/src/graphic/imagePoint.ts +237 -0
  10. package/src/graphic/label.ts +330 -0
  11. package/src/graphic/line.ts +345 -0
  12. package/src/graphic/numPoint.ts +290 -0
  13. package/src/graphic/point.ts +234 -0
  14. package/src/graphic/polygon.ts +188 -0
  15. package/src/graphic/rectangle.ts +202 -0
  16. package/src/index.ts +202 -0
  17. package/src/layer/cluster.ts +276 -0
  18. package/src/layer/geoJson.ts +174 -0
  19. package/src/layer/heatMap.ts +163 -0
  20. package/src/layer/layer.ts +464 -0
  21. package/src/layer/pointCloud.ts +78 -0
  22. package/src/map.ts +433 -0
  23. package/src/other/route.ts +457 -0
  24. package/src/types/globals.d.ts +5 -0
  25. package/src/util.ts +217 -0
  26. package/src/base/gaode_entity.js +0 -59
  27. package/src/base/mars3d_entity.js +0 -50
  28. package/src/graphic/circle.js +0 -159
  29. package/src/graphic/divPoint.js +0 -86
  30. package/src/graphic/imagePoint.js +0 -163
  31. package/src/graphic/label.js +0 -176
  32. package/src/graphic/line.js +0 -203
  33. package/src/graphic/numPoint.js +0 -119
  34. package/src/graphic/point.js +0 -144
  35. package/src/graphic/polygon.js +0 -111
  36. package/src/graphic/rectangle.js +0 -115
  37. package/src/index.js +0 -104
  38. package/src/layer/cluster.js +0 -277
  39. package/src/layer/geoJson.js +0 -174
  40. package/src/layer/heatMap.js +0 -163
  41. package/src/layer/layer.js +0 -311
  42. package/src/layer/pointCloud.js +0 -78
  43. package/src/map.js +0 -303
  44. package/src/other/route.js +0 -217
  45. package/src/util.js +0 -103
package/src/map.ts ADDED
@@ -0,0 +1,433 @@
1
+ import {deepMerge, getHeightToLevel, getLevelMiddleHeight} from "./util";
2
+
3
+ export default (hnMap: any) => {
4
+ const defaultOption = {
5
+ // 经度
6
+ lat: 37.7979042,
7
+ // 纬度
8
+ lng: 112.55074,
9
+ // 缩放级别
10
+ level: 11,
11
+ // 旋转角度
12
+ heading: 0,
13
+ // 俯仰角度
14
+ pitch: -60,
15
+ // 翻滚角度
16
+ roll: 0,
17
+ mars3d_config: "",
18
+ // 高德地图key
19
+ gaode_key: "",
20
+ // 思极key
21
+ sj_app_key: "",
22
+ sj_app_secret: "",
23
+ style: "aegis://styles/aegis/Streets-Raster512",
24
+ };
25
+
26
+ class mars3d_map {
27
+ map: any = null;
28
+
29
+ option: any = JSON.parse(JSON.stringify(defaultOption));
30
+ config: any = null;
31
+
32
+ // 图层集合
33
+ layerList: any = [];
34
+
35
+ event: any = {};
36
+
37
+ level: any = null;
38
+
39
+ private constructor(id: string, option: any) {
40
+ this.layerList = [];
41
+ this.level = 10;
42
+ deepMerge(this.option, option);
43
+ this.config = this.formatConfig(this.option);
44
+ this.map = new mars3d.Map(id, this.config);
45
+
46
+ this.map.on("cameraMoveEnd", (e: any) => {
47
+ const height = this.map.getCameraView().alt;
48
+ this.level = getHeightToLevel(height);
49
+ });
50
+ }
51
+
52
+ static async create(id: string, option: any) {
53
+ const instance = new mars3d_map(id, option);
54
+ // 返回一个 Promise,等待地图的 'load' 事件
55
+ await new Promise<void>((resolve) => {
56
+ resolve();
57
+ });
58
+ // 地图加载完成后,返回实例
59
+ return instance;
60
+ }
61
+
62
+ formatConfig(option: any) {
63
+ const alt = getLevelMiddleHeight(option.level);
64
+ return {
65
+ ...option.mars3d_config,
66
+ control: {
67
+ locationBar: {fps: true},
68
+ },
69
+ scene: {
70
+ center: {
71
+ lat: option.lat,
72
+ lng: option.lng,
73
+ alt: alt,
74
+ heading: 360 - option.heading || 0,
75
+ pitch: option.pitch || 0,
76
+ roll: option.roll || 0,
77
+ },
78
+ fxaa: true,
79
+ requestRenderMode: true, // 显式渲染
80
+ },
81
+ };
82
+ }
83
+
84
+ addLayer(layer: any) {
85
+ if (this.layerList.find((v: any) => v.id === layer.id)) {
86
+ console.error("已存在同名图层" + layer.id);
87
+ } else {
88
+ this.layerList.push(layer);
89
+ this.map.addLayer(layer.layerEntity);
90
+ return layer;
91
+ }
92
+ }
93
+
94
+ getLayer(layerId: any) {
95
+ return this.layerList.find((v: any) => v.id === layerId);
96
+ }
97
+
98
+ // 删除图层
99
+ removeLayer(layerId: any) {
100
+ const layer = this.getLayer(layerId);
101
+ if (layer) {
102
+ this.layerList = this.layerList.filter((v: any) => v.id !== layerId);
103
+ layer.destroy();
104
+ }
105
+ }
106
+
107
+ // 清空图层
108
+ clearLayer(layerId: any) {
109
+ const layer = this.getLayer(layerId);
110
+ if (layer) {
111
+ layer.children = [];
112
+ layer.clearEntity();
113
+ }
114
+ }
115
+
116
+ on(eventType: any, callback: any) {
117
+ this.off(eventType);
118
+ switch (eventType) {
119
+ case "click":
120
+ this.event[eventType] = (event: any) => {
121
+ const point = mars3d.LngLatPoint.fromCartesian(event.cartesian); //转为经纬度
122
+ point.format(); // 经度、纬度、高度
123
+ const position = {
124
+ lng: point.lng,
125
+ lat: point.lat,
126
+ alt: point.alt,
127
+ };
128
+ callback(position);
129
+ };
130
+ break;
131
+ case "cameraMoveEnd":
132
+ this.event[eventType] = (event: any) => {
133
+ callback(event);
134
+ };
135
+ break;
136
+ }
137
+ this.map.on(eventType, this.event[eventType]);
138
+ }
139
+
140
+ off(eventType: any) {
141
+ if (this.event[eventType]) {
142
+ this.map.off(eventType, this.event[eventType]);
143
+ delete this.event[eventType];
144
+ }
145
+ }
146
+
147
+ /**
148
+ * 获取当前视口的经纬度范围
149
+ * 返回参数 {xmin,xmax,ymin,ymax}
150
+ * @returns {*}
151
+ */
152
+ getExtent() {
153
+ return this.map.getExtent();
154
+ }
155
+
156
+ /**
157
+ * 获取当前视高
158
+ * 返回参数 {alt}
159
+ * @returns {any}
160
+ */
161
+ getCameraView() {
162
+ return this.map.getCameraView();
163
+ }
164
+
165
+ flyToPoint(position: any) {
166
+ const pos = new mars3d.LngLatPoint(position[0], position[1], position[2]);
167
+ this.map.flyToPoint(pos);
168
+ }
169
+
170
+ closePopup() {
171
+ this.map.closePopup();
172
+ }
173
+
174
+ // 设置投影模式 2d/3d
175
+ setMode(mode: any) {
176
+ const obj: any = {
177
+ "2d": Cesium.SceneMode.SCENE2D,
178
+ "3d": Cesium.SceneMode.SCENE3D,
179
+ };
180
+ this.map.scene.mode = obj[mode.toLowerCase()];
181
+ }
182
+ }
183
+
184
+ class gaode_map {
185
+ map: any = null;
186
+
187
+ option: any = JSON.parse(JSON.stringify(defaultOption));
188
+ config: any = null;
189
+
190
+ // 图层集合
191
+ layerList: any = [];
192
+
193
+ private constructor(id: any, option: any) {
194
+ this.layerList = [];
195
+ deepMerge(this.option, option);
196
+ this.config = this.formatConfig(this.option);
197
+ this.map = new AMap.Map(id, this.config);
198
+ }
199
+
200
+ static async create(id: string, option: any) {
201
+ const instance = new gaode_map(id, option);
202
+
203
+ // 返回一个 Promise,等待地图的 'load' 事件
204
+ await new Promise<void>((resolve) => {
205
+ resolve();
206
+ });
207
+
208
+ // 地图加载完成后,返回实例
209
+ return instance;
210
+ }
211
+
212
+ formatConfig(option: any) {
213
+ return {
214
+ viewMode: "2D", // 是否为3D地图模式
215
+ level: option.level, // 初始化地图级别
216
+ pitch: option.pitch,
217
+ rotation: option.heading,
218
+ rotateEnable: true,
219
+ pitchEnable: true,
220
+ terrain: true, // 开启地形图
221
+ center: [option.lng, option.lat], // 初始化地图中心点位置
222
+ };
223
+ }
224
+
225
+ addLayer(layer: any) {
226
+ if (this.layerList.find((v: any) => v.id === layer.id)) {
227
+ console.error("已存在同名图层");
228
+ } else {
229
+ this.layerList.push(layer);
230
+ this.map.add(layer.layerEntity);
231
+ return layer;
232
+ }
233
+ }
234
+
235
+ getLayer(layerId: any) {
236
+ return this.layerList.find((v: any) => v.id === layerId);
237
+ }
238
+
239
+ removeLayer(layerId: any) {
240
+ const layer = this.getLayer(layerId);
241
+ if (layer) {
242
+ layer.destroy();
243
+ this.layerList = this.layerList.filter((v: any) => v.id !== layerId);
244
+ }
245
+ }
246
+
247
+ clearLayer(layerId: any) {
248
+ const layer = this.getLayer(layerId);
249
+ if (layer) {
250
+ layer.children = [];
251
+ layer.clearEntity();
252
+ }
253
+ }
254
+ }
255
+
256
+ class siji_map {
257
+ id: any = null;
258
+ map: any = null;
259
+
260
+ option: any = JSON.parse(JSON.stringify(defaultOption));
261
+ config: any = null;
262
+
263
+ // 图层集合
264
+ layerList: any = [];
265
+ event: any = {};
266
+ level: any = null;
267
+
268
+ private constructor(id: any, option: any) {
269
+ this.id = id;
270
+ this.layerList = [];
271
+
272
+ this.level = this.option.level;
273
+ deepMerge(this.option, option);
274
+ this.config = this.formatConfig(this.option);
275
+ this.map = new SGMap.Map(this.config);
276
+ this.map.on("moveend", (e: any) => this.updateMapParams(e)); // 地图移动完成
277
+ this.map.on("zoomend", (e: any) => this.updateMapParams(e)); // 地图缩放完成
278
+ this.map.on("pitchend", (e: any) => this.updateMapParams(e)); // 地图俯仰角度完成
279
+ }
280
+
281
+ static async create(id: string, option: any) {
282
+ const instance = new siji_map(id, option);
283
+
284
+ // 返回一个 Promise,等待地图的 'load' 事件
285
+ await new Promise((resolve) => {
286
+ instance.map.on("load", (e: any) => {
287
+ resolve(e);
288
+ });
289
+ });
290
+
291
+ // 地图加载完成后,返回实例
292
+ return instance;
293
+ }
294
+
295
+ formatConfig(option: any) {
296
+ return {
297
+ container: this.id,
298
+ style: "aegis://styles/aegis/Streets-v2",
299
+ // 默认缩放层级
300
+ zoom: option.level,
301
+ // 地图中心点
302
+ center: [option.lng, option.lat],
303
+ // 地图默认字体
304
+ localIdeographFontFamily: "Microsoft YaHei Regular",
305
+ };
306
+ }
307
+
308
+ addLayer(layer: any) {
309
+ if (this.layerList.find((v: any) => v.id === layer.id)) {
310
+ console.error("已存在同名图层");
311
+ } else {
312
+ if (this.map) {
313
+ this.layerList.push(layer);
314
+ // this.map.addLayer(layer.config);
315
+ return layer;
316
+ }
317
+ }
318
+ }
319
+
320
+ getLayer(layerId: any) {
321
+ return this.layerList.find((v: any) => v.id === layerId);
322
+ }
323
+
324
+ removeLayer(layerId: any) {
325
+ const layer = this.getLayer(layerId);
326
+
327
+ if (layer.id) {
328
+ this.layerList = this.layerList.filter((v: any) => v.id !== layerId);
329
+ layer.destroy();
330
+ }
331
+ }
332
+
333
+ clearLayer(layerId: any) {
334
+ const layer = this.getLayer(layerId);
335
+ if (layer) {
336
+ layer.children = [];
337
+ layer.clearEntity();
338
+ }
339
+ }
340
+
341
+ getExtent() {
342
+ const data = this.map.getBounds();
343
+ return {
344
+ xmin: data._sw.lng,
345
+ ymin: data._sw.lat,
346
+ xmax: data._ne.lng,
347
+ ymax: data._ne.lat,
348
+ };
349
+ }
350
+
351
+ on(eventType: any, callback: any) {
352
+ this.off(eventType);
353
+ switch (eventType) {
354
+ case "click":
355
+ this.event[eventType] = (event: any) => {
356
+ callback(event.lngLat);
357
+ };
358
+ break;
359
+ case "dblclick":
360
+ this.event[eventType] = (event: any) => {
361
+ callback(event);
362
+ };
363
+ break;
364
+ case "cameraMoveEnd":
365
+ this.event[eventType] = () => {
366
+ this.map.on("moveend", (event: any) => callback(event)); // 地图移动完成
367
+ this.map.on("zoomend", (event: any) => callback(event)); // 地图缩放完成
368
+ this.map.on("pitchend", (event: any) => callback(event)); // 地图俯仰角度完成
369
+ };
370
+ this.event[eventType]()
371
+ break;
372
+ //镜头移动事件
373
+ case "moveend":
374
+ this.event[eventType] = (event: any) => {
375
+ callback(event);
376
+ };
377
+ break;
378
+ case "zoomend": //"levelend"
379
+ this.event[eventType] = (event: any) => {
380
+ callback(event);
381
+ };
382
+ break;
383
+ case "mouseenter":
384
+ this.event[eventType] = (event: any) => {
385
+ callback(event);
386
+ };
387
+ break;
388
+ case "mouseleave":
389
+ this.event[eventType] = (event: any) => {
390
+ callback(event);
391
+ };
392
+ break;
393
+ case "mousemove":
394
+ this.event[eventType] = (event: any) => {
395
+ callback(event);
396
+ };
397
+ break;
398
+ }
399
+ this.map.on(eventType, this.event[eventType]);
400
+ }
401
+
402
+ off(eventType: any) {
403
+ if (this.event[eventType]) {
404
+ this.map.off(eventType, this.event[eventType]);
405
+ delete this.event[eventType];
406
+ }
407
+ }
408
+
409
+ // 更新当前地图参数
410
+ private updateMapParams(e: any) {
411
+ this.level = this.map.getZoom();
412
+ let viewPos = this.getExtent()
413
+ console.log("当前缩放级别level:", this.level);
414
+ if (this.level !== this.option.level) {
415
+ this.layerList.forEach((layer: any) => {
416
+ layer.children.forEach((entity: any) => {
417
+ entity.option.distanceDisplayCondition && layer.updateEntity(entity,viewPos)
418
+ });
419
+ });
420
+ } else {
421
+ return;
422
+ }
423
+
424
+ }
425
+ }
426
+
427
+ const map: any = {
428
+ mars3d: mars3d_map,
429
+ gaode: gaode_map,
430
+ siji: siji_map,
431
+ };
432
+ return map[hnMap.mapType];
433
+ };