ly-utils-lib 1.0.12 → 2.4.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 (103) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +889 -686
  3. package/dist/array.cjs +237 -0
  4. package/dist/array.cjs.map +1 -0
  5. package/dist/array.d.cts +2 -0
  6. package/dist/array.d.ts +2 -0
  7. package/dist/array.js +150 -0
  8. package/dist/array.js.map +1 -0
  9. package/dist/crypto.cjs +193 -0
  10. package/dist/crypto.cjs.map +1 -0
  11. package/dist/crypto.d.cts +3 -0
  12. package/dist/crypto.d.ts +3 -0
  13. package/dist/crypto.js +144 -0
  14. package/dist/crypto.js.map +1 -0
  15. package/dist/date.cjs +563 -0
  16. package/dist/date.cjs.map +1 -0
  17. package/dist/date.d.cts +2 -0
  18. package/dist/date.d.ts +2 -0
  19. package/dist/date.js +451 -0
  20. package/dist/date.js.map +1 -0
  21. package/dist/excel.cjs +227 -0
  22. package/dist/excel.cjs.map +1 -0
  23. package/dist/excel.d.cts +2 -0
  24. package/dist/excel.d.ts +2 -0
  25. package/dist/excel.js +196 -0
  26. package/dist/excel.js.map +1 -0
  27. package/dist/index-B80SEVzM.d.cts +382 -0
  28. package/dist/index-B80SEVzM.d.ts +382 -0
  29. package/dist/index-Ba1rjTzj.d.cts +299 -0
  30. package/dist/index-Ba1rjTzj.d.ts +299 -0
  31. package/dist/index-Bg1ise7y.d.cts +253 -0
  32. package/dist/index-Bg1ise7y.d.ts +253 -0
  33. package/dist/index-BoqNpwNa.d.cts +203 -0
  34. package/dist/index-BoqNpwNa.d.ts +203 -0
  35. package/dist/index-C0qUnb9Y.d.cts +533 -0
  36. package/dist/index-C0qUnb9Y.d.ts +533 -0
  37. package/dist/index-Cy-mb5v_.d.cts +262 -0
  38. package/dist/index-Cy-mb5v_.d.ts +262 -0
  39. package/dist/index-D1f9Sym2.d.cts +148 -0
  40. package/dist/index-D1f9Sym2.d.ts +148 -0
  41. package/dist/index-Dan5oF-5.d.cts +213 -0
  42. package/dist/index-Dan5oF-5.d.ts +213 -0
  43. package/dist/index-XABfrs7z.d.cts +596 -0
  44. package/dist/index-XABfrs7z.d.ts +596 -0
  45. package/dist/index-YXWfKCK7.d.cts +109 -0
  46. package/dist/index-YXWfKCK7.d.ts +109 -0
  47. package/dist/index.cjs +3355 -0
  48. package/dist/index.cjs.map +1 -0
  49. package/dist/index.d.cts +21 -0
  50. package/dist/index.d.ts +21 -0
  51. package/dist/index.js +3294 -0
  52. package/dist/index.js.map +1 -0
  53. package/dist/map.cjs +839 -0
  54. package/dist/map.cjs.map +1 -0
  55. package/dist/map.d.cts +6 -0
  56. package/dist/map.d.ts +6 -0
  57. package/dist/map.js +811 -0
  58. package/dist/map.js.map +1 -0
  59. package/dist/object.cjs +316 -0
  60. package/dist/object.cjs.map +1 -0
  61. package/dist/object.d.cts +2 -0
  62. package/dist/object.d.ts +2 -0
  63. package/dist/object.js +247 -0
  64. package/dist/object.js.map +1 -0
  65. package/dist/pdf.cjs +197 -0
  66. package/dist/pdf.cjs.map +1 -0
  67. package/dist/pdf.d.cts +3 -0
  68. package/dist/pdf.d.ts +3 -0
  69. package/dist/pdf.js +173 -0
  70. package/dist/pdf.js.map +1 -0
  71. package/dist/storage.cjs +255 -0
  72. package/dist/storage.cjs.map +1 -0
  73. package/dist/storage.d.cts +1 -0
  74. package/dist/storage.d.ts +1 -0
  75. package/dist/storage.js +226 -0
  76. package/dist/storage.js.map +1 -0
  77. package/dist/string.cjs +232 -0
  78. package/dist/string.cjs.map +1 -0
  79. package/dist/string.d.cts +2 -0
  80. package/dist/string.d.ts +2 -0
  81. package/dist/string.js +170 -0
  82. package/dist/string.js.map +1 -0
  83. package/dist/utils.cjs +429 -0
  84. package/dist/utils.cjs.map +1 -0
  85. package/dist/utils.d.cts +2 -0
  86. package/dist/utils.d.ts +2 -0
  87. package/dist/utils.js +371 -0
  88. package/dist/utils.js.map +1 -0
  89. package/package.json +159 -33
  90. package/dist/ly-utils-lib.cjs.js +0 -115
  91. package/dist/ly-utils-lib.cjs.js.map +0 -1
  92. package/dist/ly-utils-lib.es.js +0 -58589
  93. package/dist/ly-utils-lib.es.js.map +0 -1
  94. package/dist/ly-utils-lib.umd.js +0 -115
  95. package/dist/ly-utils-lib.umd.js.map +0 -1
  96. package/dist/types/index.d.ts +0 -15
  97. package/dist/types/utils/esToolkit.d.ts +0 -2
  98. package/dist/types/utils/ol.d.ts +0 -175
  99. package/dist/types/utils/router.d.ts +0 -10
  100. package/dist/types/utils/storage.d.ts +0 -42
  101. package/dist/types/utils/time.d.ts +0 -66
  102. package/dist/types/utils/tool.d.ts +0 -80
  103. package/dist/vite.svg +0 -1
package/dist/map.cjs ADDED
@@ -0,0 +1,839 @@
1
+ 'use strict';
2
+
3
+ var ol = require('ol');
4
+ var control = require('ol/control');
5
+ var geom = require('ol/geom');
6
+ var interaction = require('ol/interaction');
7
+ var Overlay = require('ol/Overlay');
8
+ var TileLayer = require('ol/layer/Tile');
9
+ var VectorLayer = require('ol/layer/Vector');
10
+ var proj = require('ol/proj');
11
+ var OSM = require('ol/source/OSM');
12
+ var VectorSource = require('ol/source/Vector');
13
+ var XYZ = require('ol/source/XYZ');
14
+ var GeoJSON = require('ol/format/GeoJSON');
15
+ var style = require('ol/style');
16
+ var extent = require('ol/extent');
17
+
18
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
19
+
20
+ var Overlay__default = /*#__PURE__*/_interopDefault(Overlay);
21
+ var TileLayer__default = /*#__PURE__*/_interopDefault(TileLayer);
22
+ var VectorLayer__default = /*#__PURE__*/_interopDefault(VectorLayer);
23
+ var OSM__default = /*#__PURE__*/_interopDefault(OSM);
24
+ var VectorSource__default = /*#__PURE__*/_interopDefault(VectorSource);
25
+ var XYZ__default = /*#__PURE__*/_interopDefault(XYZ);
26
+ var GeoJSON__default = /*#__PURE__*/_interopDefault(GeoJSON);
27
+
28
+ // src/modules/map/index.ts
29
+ var MapInstance = class {
30
+ constructor(options) {
31
+ this.vectorSources = /* @__PURE__ */ new Map();
32
+ this.vectorLayers = /* @__PURE__ */ new Map();
33
+ this.overlays = /* @__PURE__ */ new Map();
34
+ this.themeLayers = /* @__PURE__ */ new Map();
35
+ this.drawInteraction = null;
36
+ this.modifyInteraction = null;
37
+ this.snapInteraction = null;
38
+ this.selectInteraction = null;
39
+ this.currentTheme = "light";
40
+ this.icons = /* @__PURE__ */ new Map();
41
+ this.enableDynamicScale = false;
42
+ this.projection = "EPSG:4326";
43
+ this.enableDynamicScale = options.enableDynamicScale || false;
44
+ this.projection = options.projection || "EPSG:4326";
45
+ this.currentTheme = options.theme || "light";
46
+ if (options.icons) {
47
+ Object.entries(options.icons).forEach(([name, config]) => {
48
+ this.icons.set(name, config);
49
+ });
50
+ }
51
+ const controls = control.defaults({
52
+ zoom: false,
53
+ rotate: false,
54
+ attribution: false
55
+ });
56
+ if (options.showScaleLine !== false) {
57
+ controls.push(new control.ScaleLine());
58
+ }
59
+ if (options.showFullScreen !== false) {
60
+ controls.push(new control.FullScreen());
61
+ }
62
+ this.map = new ol.Map({
63
+ target: options.container,
64
+ layers: [],
65
+ view: new ol.View({
66
+ center: proj.fromLonLat(options.center || [116.3974, 39.9093]),
67
+ // 默认北京
68
+ zoom: options.zoom || 10,
69
+ minZoom: options.minZoom || 1,
70
+ maxZoom: options.maxZoom || 20,
71
+ projection: this.projection,
72
+ constrainResolution: true,
73
+ smoothResolutionConstraint: false
74
+ }),
75
+ controls
76
+ });
77
+ if (options.themeConfig) {
78
+ this.setupThemeLayers(options.themeConfig);
79
+ }
80
+ if (options.layers) {
81
+ options.layers.forEach((layer) => this.map.addLayer(layer.layer));
82
+ }
83
+ if (this.enableDynamicScale) {
84
+ this.map.on("moveend", () => this.handleZoomChange());
85
+ }
86
+ }
87
+ // ==================== 主题管理 ====================
88
+ /**
89
+ * 设置主题图层
90
+ */
91
+ setupThemeLayers(config) {
92
+ const lightUrl = config.lightUrl || "https://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}";
93
+ const darkUrl = config.darkUrl || "https://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=9&x={x}&y={y}&z={z}";
94
+ const lightLayer = new TileLayer__default.default({
95
+ preload: Infinity,
96
+ source: new XYZ__default.default({ url: lightUrl })
97
+ });
98
+ const darkLayer = new TileLayer__default.default({
99
+ preload: Infinity,
100
+ source: new XYZ__default.default({ url: darkUrl })
101
+ });
102
+ this.themeLayers.set("light", lightLayer);
103
+ this.themeLayers.set("dark", darkLayer);
104
+ this.map.addLayer(lightLayer);
105
+ this.map.addLayer(darkLayer);
106
+ this.setTheme(this.currentTheme);
107
+ }
108
+ /**
109
+ * 切换主题
110
+ */
111
+ setTheme(theme) {
112
+ this.currentTheme = theme;
113
+ this.themeLayers.forEach((layer, key) => {
114
+ layer.setVisible(key === theme);
115
+ });
116
+ }
117
+ /**
118
+ * 获取当前主题
119
+ */
120
+ getTheme() {
121
+ return this.currentTheme;
122
+ }
123
+ // ==================== 图层管理 ====================
124
+ /**
125
+ * 创建矢量图层
126
+ */
127
+ createVectorLayer(config) {
128
+ if (this.vectorLayers.has(config.name)) {
129
+ console.warn(`\u56FE\u5C42 "${config.name}" \u5DF2\u5B58\u5728`);
130
+ return;
131
+ }
132
+ const source = new VectorSource__default.default({
133
+ format: new GeoJSON__default.default()
134
+ });
135
+ const layer = new VectorLayer__default.default({
136
+ source,
137
+ style: config.style,
138
+ minZoom: config.minZoom,
139
+ maxZoom: config.maxZoom,
140
+ zIndex: config.zIndex,
141
+ visible: config.visible !== false
142
+ });
143
+ this.vectorSources.set(config.name, source);
144
+ this.vectorLayers.set(config.name, layer);
145
+ this.map.addLayer(layer);
146
+ }
147
+ /**
148
+ * 获取矢量图层
149
+ */
150
+ getVectorLayer(name) {
151
+ return this.vectorLayers.get(name);
152
+ }
153
+ /**
154
+ * 获取矢量数据源
155
+ */
156
+ getVectorSource(name) {
157
+ return this.vectorSources.get(name);
158
+ }
159
+ /**
160
+ * 显示/隐藏矢量图层
161
+ */
162
+ setLayerVisible(name, visible) {
163
+ const layer = this.vectorLayers.get(name);
164
+ if (layer) {
165
+ layer.setVisible(visible);
166
+ }
167
+ }
168
+ /**
169
+ * 设置矢量图层样式
170
+ */
171
+ setLayerStyle(name, style) {
172
+ const layer = this.vectorLayers.get(name);
173
+ if (layer) {
174
+ layer.setStyle(style);
175
+ }
176
+ }
177
+ /**
178
+ * 清除矢量图层
179
+ */
180
+ clearLayer(name) {
181
+ const source = this.vectorSources.get(name);
182
+ if (source) {
183
+ source.clear();
184
+ }
185
+ }
186
+ /**
187
+ * 删除矢量图层
188
+ */
189
+ removeLayer(name) {
190
+ const layer = this.vectorLayers.get(name);
191
+ if (layer) {
192
+ this.map.removeLayer(layer);
193
+ this.vectorLayers.delete(name);
194
+ this.vectorSources.delete(name);
195
+ }
196
+ }
197
+ // ==================== 要素管理 ====================
198
+ /**
199
+ * 添加标记
200
+ */
201
+ addMarker(options, layerName = "default") {
202
+ if (!this.vectorLayers.has(layerName)) {
203
+ this.createVectorLayer({ name: layerName });
204
+ }
205
+ const source = this.vectorSources.get(layerName);
206
+ const feature = new ol.Feature({
207
+ geometry: new geom.Point(proj.fromLonLat(options.coordinate))
208
+ });
209
+ const style = options.style || this.createMarkerStyle(options);
210
+ feature.setStyle(style);
211
+ source.addFeature(feature);
212
+ return feature;
213
+ }
214
+ /**
215
+ * 创建标记样式
216
+ */
217
+ createMarkerStyle(options) {
218
+ if (options.icon && this.icons.has(options.icon)) {
219
+ const iconConfig = this.icons.get(options.icon);
220
+ return new style.Style({
221
+ image: new style.Icon({
222
+ anchor: iconConfig.anchor || [0.5, 0.5],
223
+ src: iconConfig.src,
224
+ scale: iconConfig.scale || 1
225
+ })
226
+ });
227
+ }
228
+ if (options.iconConfig) {
229
+ return new style.Style({
230
+ image: new style.Icon({
231
+ anchor: options.iconConfig.anchor || [0.5, 0.5],
232
+ src: options.iconConfig.src,
233
+ scale: options.iconConfig.scale || 1
234
+ })
235
+ });
236
+ }
237
+ return new style.Style({
238
+ image: new style.Circle({
239
+ radius: options.radius || 7,
240
+ fill: new style.Fill({ color: options.color || "#ff3333" }),
241
+ stroke: new style.Stroke({
242
+ color: options.strokeColor || "#ffffff",
243
+ width: options.strokeWidth || 2
244
+ })
245
+ })
246
+ });
247
+ }
248
+ /**
249
+ * 移除标记
250
+ */
251
+ removeMarker(feature, layerName = "default") {
252
+ const source = this.vectorSources.get(layerName);
253
+ if (source) {
254
+ source.removeFeature(feature);
255
+ }
256
+ }
257
+ /**
258
+ * 从 GeoJSON 添加要素
259
+ */
260
+ addFeaturesFromGeoJSON(geoJson, layerName = "default") {
261
+ if (!this.vectorLayers.has(layerName)) {
262
+ this.createVectorLayer({ name: layerName });
263
+ }
264
+ const source = this.vectorSources.get(layerName);
265
+ const format = new GeoJSON__default.default();
266
+ const features = format.readFeatures(typeof geoJson === "string" ? geoJson : JSON.stringify(geoJson));
267
+ source.addFeatures(features);
268
+ return features;
269
+ }
270
+ /**
271
+ * 添加要素
272
+ */
273
+ addFeature(feature, layerName = "default") {
274
+ if (!this.vectorLayers.has(layerName)) {
275
+ this.createVectorLayer({ name: layerName });
276
+ }
277
+ const source = this.vectorSources.get(layerName);
278
+ source.addFeature(feature);
279
+ }
280
+ /**
281
+ * 批量添加要素
282
+ */
283
+ addFeatures(features, layerName = "default") {
284
+ if (!this.vectorLayers.has(layerName)) {
285
+ this.createVectorLayer({ name: layerName });
286
+ }
287
+ const source = this.vectorSources.get(layerName);
288
+ source.addFeatures(features);
289
+ }
290
+ /**
291
+ * 移除要素
292
+ */
293
+ removeFeature(feature, layerName = "default") {
294
+ const source = this.vectorSources.get(layerName);
295
+ if (source) {
296
+ source.removeFeature(feature);
297
+ }
298
+ }
299
+ /**
300
+ * 清除所有矢量要素
301
+ */
302
+ clearFeatures(layerName = "default") {
303
+ const source = this.vectorSources.get(layerName);
304
+ if (source) {
305
+ source.clear();
306
+ }
307
+ }
308
+ /**
309
+ * 获取图层中的所有要素
310
+ */
311
+ getFeatures(layerName = "default") {
312
+ const source = this.vectorSources.get(layerName);
313
+ return source ? source.getFeatures() : [];
314
+ }
315
+ /**
316
+ * 创建 GeoJSON
317
+ */
318
+ createGeoJSON(type, data) {
319
+ const coordinates = this.getCoordinates(type, data);
320
+ const id = this.generateUUID();
321
+ return {
322
+ type: "FeatureCollection",
323
+ features: [{
324
+ type: "Feature",
325
+ geometry: {
326
+ type,
327
+ coordinates
328
+ },
329
+ properties: {
330
+ id,
331
+ value: data
332
+ }
333
+ }]
334
+ };
335
+ }
336
+ /**
337
+ * 获取坐标
338
+ */
339
+ getCoordinates(type, data) {
340
+ if (type === "Point") {
341
+ return proj.fromLonLat(proj.transform(data.points, "EPSG:3857", "EPSG:4326"));
342
+ } else if (type === "LineString") {
343
+ return this.setPointArray(data.points);
344
+ } else if (type === "Polygon") {
345
+ return [this.setPointArray(data.points)];
346
+ }
347
+ return [];
348
+ }
349
+ /**
350
+ * 设置点数组
351
+ */
352
+ setPointArray(points) {
353
+ return points.map((item) => proj.fromLonLat(proj.transform(item, "EPSG:3857", "EPSG:4326")));
354
+ }
355
+ /**
356
+ * 生成 UUID
357
+ */
358
+ generateUUID() {
359
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
360
+ const r = Math.random() * 16 | 0;
361
+ const v = c === "x" ? r : r & 3 | 8;
362
+ return v.toString(16);
363
+ });
364
+ }
365
+ /**
366
+ * 设置要素样式
367
+ */
368
+ setFeatureStyle(feature, options) {
369
+ const style = this.createFeatureStyle(options);
370
+ feature.setStyle(style);
371
+ }
372
+ /**
373
+ * 创建要素样式
374
+ */
375
+ createFeatureStyle(options) {
376
+ if (options.style) {
377
+ return options.style;
378
+ }
379
+ if (options.icon && this.icons.has(options.icon)) {
380
+ const iconConfig = this.icons.get(options.icon);
381
+ return new style.Style({
382
+ image: new style.Icon({
383
+ anchor: iconConfig.anchor || [0.5, 0.5],
384
+ src: iconConfig.src,
385
+ scale: iconConfig.scale || 1
386
+ })
387
+ });
388
+ }
389
+ if (options.iconConfig) {
390
+ return new style.Style({
391
+ image: new style.Icon({
392
+ anchor: options.iconConfig.anchor || [0.5, 0.5],
393
+ src: options.iconConfig.src,
394
+ scale: options.iconConfig.scale || 1
395
+ })
396
+ });
397
+ }
398
+ return new style.Style({
399
+ stroke: new style.Stroke({
400
+ color: options.strokeColor || "#3388ff",
401
+ width: options.strokeWidth || 2
402
+ }),
403
+ fill: new style.Fill({
404
+ color: options.fillColor || "rgba(51, 136, 255, 0.2)"
405
+ }),
406
+ image: new style.Circle({
407
+ radius: 7,
408
+ fill: new style.Fill({ color: options.strokeColor || "#3388ff" }),
409
+ stroke: new style.Stroke({
410
+ color: "#ffffff",
411
+ width: 2
412
+ })
413
+ })
414
+ });
415
+ }
416
+ // ==================== 动态缩放 ====================
417
+ /**
418
+ * 处理缩放变化
419
+ */
420
+ handleZoomChange() {
421
+ const zoom = this.getZoom();
422
+ if (zoom !== void 0) {
423
+ this.updateIconScale(zoom);
424
+ }
425
+ }
426
+ /**
427
+ * 更新图标缩放比例
428
+ */
429
+ updateIconScale(zoom) {
430
+ let scale = 1;
431
+ if (zoom >= 14) {
432
+ scale = 0.5;
433
+ } else if (zoom < 14 && zoom >= 12) {
434
+ scale = 0.4;
435
+ } else if (zoom < 12) {
436
+ scale = 0.3;
437
+ }
438
+ this.vectorLayers.forEach((layer) => {
439
+ const features = layer.getSource()?.getFeatures();
440
+ if (features) {
441
+ features.forEach((feature) => {
442
+ const style$1 = feature.getStyle();
443
+ if (style$1) {
444
+ const image = style$1.getImage();
445
+ if (image instanceof style.Icon) {
446
+ image.setScale(scale);
447
+ }
448
+ }
449
+ });
450
+ }
451
+ });
452
+ }
453
+ // ==================== Overlay 管理 ====================
454
+ /**
455
+ * 创建 Overlay
456
+ */
457
+ createOverlay(options) {
458
+ const overlay = new Overlay__default.default({
459
+ element: options.element,
460
+ autoPan: options.autoPan !== false,
461
+ offset: options.offset,
462
+ positioning: options.positioning || "bottom-center"
463
+ });
464
+ this.overlays.set(options.name, overlay);
465
+ this.map.addOverlay(overlay);
466
+ }
467
+ /**
468
+ * 显示 Overlay
469
+ */
470
+ showOverlay(name, coordinate) {
471
+ const overlay = this.overlays.get(name);
472
+ if (overlay) {
473
+ overlay.setPosition(proj.fromLonLat(coordinate));
474
+ }
475
+ }
476
+ /**
477
+ * 隐藏 Overlay
478
+ */
479
+ hideOverlay(name) {
480
+ const overlay = this.overlays.get(name);
481
+ if (overlay) {
482
+ overlay.setPosition(void 0);
483
+ }
484
+ }
485
+ /**
486
+ * 移除 Overlay
487
+ */
488
+ removeOverlay(name) {
489
+ const overlay = this.overlays.get(name);
490
+ if (overlay) {
491
+ this.map.removeOverlay(overlay);
492
+ this.overlays.delete(name);
493
+ }
494
+ }
495
+ // ==================== 交互管理 ====================
496
+ /**
497
+ * 开始绘制
498
+ */
499
+ startDraw(options) {
500
+ this.stopDraw();
501
+ const source = options.layerName && this.vectorSources.has(options.layerName) ? this.vectorSources.get(options.layerName) : new VectorSource__default.default();
502
+ this.drawInteraction = new interaction.Draw({
503
+ source,
504
+ type: options.type,
505
+ style: options.style
506
+ });
507
+ this.map.addInteraction(this.drawInteraction);
508
+ if (options.onDrawEnd) {
509
+ this.drawInteraction.on("drawend", (evt) => {
510
+ options.onDrawEnd?.(evt.feature);
511
+ });
512
+ }
513
+ this.snapInteraction = new interaction.Snap({ source });
514
+ this.map.addInteraction(this.snapInteraction);
515
+ }
516
+ /**
517
+ * 停止绘制
518
+ */
519
+ stopDraw() {
520
+ if (this.drawInteraction) {
521
+ this.map.removeInteraction(this.drawInteraction);
522
+ this.drawInteraction = null;
523
+ }
524
+ if (this.snapInteraction) {
525
+ this.map.removeInteraction(this.snapInteraction);
526
+ this.snapInteraction = null;
527
+ }
528
+ }
529
+ /**
530
+ * 开始编辑
531
+ */
532
+ startModify(layerName = "default") {
533
+ this.stopModify();
534
+ const source = this.vectorSources.get(layerName);
535
+ if (!source) {
536
+ console.warn(`\u56FE\u5C42 "${layerName}" \u4E0D\u5B58\u5728`);
537
+ return;
538
+ }
539
+ this.modifyInteraction = new interaction.Modify({
540
+ source
541
+ });
542
+ this.map.addInteraction(this.modifyInteraction);
543
+ this.snapInteraction = new interaction.Snap({ source });
544
+ this.map.addInteraction(this.snapInteraction);
545
+ }
546
+ /**
547
+ * 停止编辑
548
+ */
549
+ stopModify() {
550
+ if (this.modifyInteraction) {
551
+ this.map.removeInteraction(this.modifyInteraction);
552
+ this.modifyInteraction = null;
553
+ }
554
+ if (this.snapInteraction) {
555
+ this.map.removeInteraction(this.snapInteraction);
556
+ this.snapInteraction = null;
557
+ }
558
+ }
559
+ /**
560
+ * 开始选择
561
+ */
562
+ startSelect(layerName = "default", onSelect) {
563
+ this.stopSelect();
564
+ this.selectInteraction = new interaction.Select({
565
+ layers: this.vectorLayers.get(layerName) ? [this.vectorLayers.get(layerName)] : void 0
566
+ });
567
+ this.map.addInteraction(this.selectInteraction);
568
+ if (onSelect) {
569
+ this.selectInteraction.on("select", (evt) => {
570
+ onSelect(evt.selected);
571
+ });
572
+ }
573
+ }
574
+ /**
575
+ * 停止选择
576
+ */
577
+ stopSelect() {
578
+ if (this.selectInteraction) {
579
+ this.map.removeInteraction(this.selectInteraction);
580
+ this.selectInteraction = null;
581
+ }
582
+ }
583
+ /**
584
+ * 获取选中的要素
585
+ */
586
+ getSelectedFeatures() {
587
+ return this.selectInteraction?.getFeatures().getArray() || [];
588
+ }
589
+ // ==================== 地图控制 ====================
590
+ /**
591
+ * 获取原始地图实例
592
+ */
593
+ getOlMap() {
594
+ return this.map;
595
+ }
596
+ /**
597
+ * 获取视图
598
+ */
599
+ getView() {
600
+ return this.map.getView();
601
+ }
602
+ /**
603
+ * 设置中心点
604
+ */
605
+ setCenter(coord) {
606
+ const view = this.getView();
607
+ if (view) {
608
+ view.setCenter(proj.fromLonLat(coord));
609
+ this.map.render();
610
+ }
611
+ }
612
+ /**
613
+ * 获取中心点
614
+ */
615
+ getCenter() {
616
+ const view = this.getView();
617
+ if (view) {
618
+ const center = view.getCenter();
619
+ return center ? proj.toLonLat(center) : void 0;
620
+ }
621
+ return void 0;
622
+ }
623
+ /**
624
+ * 设置缩放级别
625
+ */
626
+ setZoom(zoom) {
627
+ const view = this.getView();
628
+ if (view) {
629
+ view.setZoom(zoom);
630
+ this.map.render();
631
+ }
632
+ }
633
+ /**
634
+ * 获取缩放级别
635
+ */
636
+ getZoom() {
637
+ return this.getView()?.getZoom();
638
+ }
639
+ /**
640
+ * 缩放到指定范围
641
+ */
642
+ fit(extent, options) {
643
+ if (!extent) return;
644
+ const view = this.getView();
645
+ if (view) {
646
+ view.fit(extent, options);
647
+ this.map.render();
648
+ }
649
+ }
650
+ /**
651
+ * 缩放到图层
652
+ */
653
+ fitLayer(layerName, options) {
654
+ const source = this.vectorSources.get(layerName);
655
+ if (source) {
656
+ const extent = source.getExtent();
657
+ if (extent && extent[0] !== Infinity) {
658
+ this.fit(extent, options);
659
+ }
660
+ }
661
+ }
662
+ /**
663
+ * 缩放到坐标数组
664
+ */
665
+ fitCoordinates(coordinates, options) {
666
+ const extent$1 = extent.boundingExtent(coordinates);
667
+ this.fit(extent$1, options);
668
+ }
669
+ /**
670
+ * 添加图层
671
+ */
672
+ addLayer(layer) {
673
+ this.map.addLayer(layer.layer);
674
+ }
675
+ /**
676
+ * 移除图层
677
+ */
678
+ removeMapLayer(layer) {
679
+ this.map.removeLayer(layer.layer);
680
+ }
681
+ /**
682
+ * 更新地图大小
683
+ */
684
+ updateSize() {
685
+ this.map.updateSize();
686
+ }
687
+ /**
688
+ * 地图事件监听
689
+ */
690
+ on(event, callback) {
691
+ this.map.on(event, callback);
692
+ }
693
+ /**
694
+ * 移除事件监听
695
+ */
696
+ off(event, callback) {
697
+ this.map.un(event, callback);
698
+ }
699
+ /**
700
+ * 销毁地图
701
+ */
702
+ destroy() {
703
+ this.stopDraw();
704
+ this.stopModify();
705
+ this.stopSelect();
706
+ this.overlays.forEach((overlay) => this.map.removeOverlay(overlay));
707
+ this.overlays.clear();
708
+ this.vectorLayers.forEach((layer) => this.map.removeLayer(layer));
709
+ this.vectorLayers.clear();
710
+ this.vectorSources.clear();
711
+ this.map.setTarget(void 0);
712
+ }
713
+ };
714
+ function createOSMLayer(name = "OSM") {
715
+ return {
716
+ name,
717
+ layer: new TileLayer__default.default({
718
+ source: new OSM__default.default()
719
+ })
720
+ };
721
+ }
722
+ function createXYZLayer(url, name = "XYZ") {
723
+ return {
724
+ name,
725
+ layer: new TileLayer__default.default({
726
+ source: new XYZ__default.default({
727
+ url
728
+ })
729
+ })
730
+ };
731
+ }
732
+ function createGaodeLayer(name = "\u9AD8\u5FB7\u5730\u56FE") {
733
+ return createXYZLayer(
734
+ "https://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}",
735
+ name
736
+ );
737
+ }
738
+ function createTiandituLayer(token, type = "vec", name = "\u5929\u5730\u56FE") {
739
+ const url = `https://t{s}.tianditu.gov.cn/DataServer?T=${type}_w&x={x}&y={y}&l={z}&tk=${token}`;
740
+ return createXYZLayer(url, name);
741
+ }
742
+ function lonLatToXY(lon, lat) {
743
+ return proj.fromLonLat([lon, lat]);
744
+ }
745
+ function xyToLonLat(x, y) {
746
+ return proj.toLonLat([x, y]);
747
+ }
748
+ function createPointFeature(coordinate) {
749
+ return new ol.Feature({
750
+ geometry: new geom.Point(proj.fromLonLat(coordinate))
751
+ });
752
+ }
753
+ function createLineFeature(coordinates) {
754
+ return new ol.Feature({
755
+ geometry: new geom.LineString(coordinates.map((coord) => proj.fromLonLat(coord)))
756
+ });
757
+ }
758
+ function createPolygonFeature(coordinates) {
759
+ return new ol.Feature({
760
+ geometry: new geom.Polygon(coordinates.map((ring) => ring.map((coord) => proj.fromLonLat(coord))))
761
+ });
762
+ }
763
+ function createStyledPointFeature(coordinate, style) {
764
+ const feature = createPointFeature(coordinate);
765
+ feature.setStyle(style);
766
+ return feature;
767
+ }
768
+ function createStyledLineFeature(coordinates, style) {
769
+ const feature = createLineFeature(coordinates);
770
+ feature.setStyle(style);
771
+ return feature;
772
+ }
773
+ function createStyledPolygonFeature(coordinates, style) {
774
+ const feature = createPolygonFeature(coordinates);
775
+ feature.setStyle(style);
776
+ return feature;
777
+ }
778
+ function createIconStyle(iconUrl, options) {
779
+ return new style.Style({
780
+ image: new style.Icon({
781
+ anchor: options?.anchor || [0.5, 0.5],
782
+ src: iconUrl,
783
+ scale: options?.scale || 1,
784
+ color: options?.color
785
+ })
786
+ });
787
+ }
788
+ function createCircleStyle(options) {
789
+ return new style.Style({
790
+ image: new style.Circle({
791
+ radius: options?.radius || 7,
792
+ fill: new style.Fill({ color: options?.fillColor || "#3388ff" }),
793
+ stroke: new style.Stroke({
794
+ color: options?.strokeColor || "#ffffff",
795
+ width: options?.strokeWidth || 2
796
+ })
797
+ })
798
+ });
799
+ }
800
+ function createStrokeStyle(options) {
801
+ return new style.Style({
802
+ stroke: new style.Stroke({
803
+ color: options?.color || "#3388ff",
804
+ width: options?.width || 2,
805
+ lineDash: options?.lineDash
806
+ })
807
+ });
808
+ }
809
+ function createFillStyle(options) {
810
+ return new style.Style({
811
+ stroke: new style.Stroke({
812
+ color: options?.strokeColor || "#3388ff",
813
+ width: options?.strokeWidth || 2
814
+ }),
815
+ fill: new style.Fill({
816
+ color: options?.fillColor || "rgba(51, 136, 255, 0.2)"
817
+ })
818
+ });
819
+ }
820
+
821
+ exports.MapInstance = MapInstance;
822
+ exports.createCircleStyle = createCircleStyle;
823
+ exports.createFillStyle = createFillStyle;
824
+ exports.createGaodeLayer = createGaodeLayer;
825
+ exports.createIconStyle = createIconStyle;
826
+ exports.createLineFeature = createLineFeature;
827
+ exports.createOSMLayer = createOSMLayer;
828
+ exports.createPointFeature = createPointFeature;
829
+ exports.createPolygonFeature = createPolygonFeature;
830
+ exports.createStrokeStyle = createStrokeStyle;
831
+ exports.createStyledLineFeature = createStyledLineFeature;
832
+ exports.createStyledPointFeature = createStyledPointFeature;
833
+ exports.createStyledPolygonFeature = createStyledPolygonFeature;
834
+ exports.createTiandituLayer = createTiandituLayer;
835
+ exports.createXYZLayer = createXYZLayer;
836
+ exports.lonLatToXY = lonLatToXY;
837
+ exports.xyToLonLat = xyToLonLat;
838
+ //# sourceMappingURL=map.cjs.map
839
+ //# sourceMappingURL=map.cjs.map