my-openlayer 2.5.4 → 3.0.1

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 (48) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/MyOl.d.ts +3 -22
  3. package/MyOl.js +52 -112
  4. package/README.md +116 -29
  5. package/core/line/Line.d.ts +24 -5
  6. package/core/line/Line.js +38 -10
  7. package/core/map/ConfigManager.d.ts +169 -89
  8. package/core/map/ConfigManager.js +157 -175
  9. package/core/map/MapBaseLayers.d.ts +6 -0
  10. package/core/map/MapBaseLayers.js +9 -0
  11. package/core/map/MapTools.d.ts +17 -1
  12. package/core/map/MapTools.js +39 -5
  13. package/core/point/Point.d.ts +54 -14
  14. package/core/point/Point.js +132 -76
  15. package/core/point/PointOverlay.d.ts +2 -4
  16. package/core/point/PointOverlay.js +2 -1
  17. package/core/point/PointPulseLayer.js +6 -4
  18. package/core/polygon/Polygon.d.ts +32 -17
  19. package/core/polygon/Polygon.js +87 -64
  20. package/core/polygon/PolygonHeatmapLayer.js +15 -2
  21. package/core/polygon/PolygonMaskLayer.d.ts +2 -2
  22. package/core/polygon/PolygonMaskLayer.js +3 -1
  23. package/core/polygon/PolygonStyleFactory.d.ts +4 -3
  24. package/core/projection/ProjectionManager.d.ts +66 -0
  25. package/core/projection/ProjectionManager.js +144 -0
  26. package/core/projection/index.d.ts +2 -0
  27. package/core/projection/index.js +1 -0
  28. package/core/select/SelectHandler.d.ts +1 -1
  29. package/core/vue-template-point/VueTemplatePoint.d.ts +2 -4
  30. package/core/vue-template-point/VueTemplatePoint.js +16 -2
  31. package/docs/.vitepress/config.mts +1 -0
  32. package/docs/Line.md +4 -4
  33. package/docs/MIGRATION-3.0.md +221 -0
  34. package/docs/Point.md +24 -6
  35. package/docs/Polygon.md +14 -5
  36. package/index.d.ts +6 -3
  37. package/index.js +4 -1
  38. package/package.json +6 -3
  39. package/types/base.d.ts +4 -4
  40. package/types/common.d.ts +8 -6
  41. package/types/handle.d.ts +34 -0
  42. package/types/handle.js +1 -0
  43. package/types/index.d.ts +1 -0
  44. package/types/line.d.ts +3 -2
  45. package/types/map.d.ts +1 -1
  46. package/types/point.d.ts +11 -3
  47. package/utils/ErrorHandler.d.ts +12 -0
  48. package/utils/ErrorHandler.js +21 -0
package/core/line/Line.js CHANGED
@@ -61,6 +61,15 @@ export default class Line {
61
61
  this.map.addLayer(layer);
62
62
  return layer;
63
63
  }
64
+ /** *********************统一句柄:静态线图层*********************/
65
+ toLayerHandle(layer) {
66
+ const map = this.map;
67
+ return {
68
+ layer,
69
+ setVisible(visible) { layer.setVisible(visible); },
70
+ remove() { map.removeLayer(layer); }
71
+ };
72
+ }
64
73
  /**
65
74
  * 注册流动线句柄。
66
75
  */
@@ -73,26 +82,32 @@ export default class Line {
73
82
  unregisterFlowLineHandle(layerName) {
74
83
  this.flowLineRegistry.delete(layerName);
75
84
  }
76
- addLine(data, options) {
85
+ /** *********************创建静态线图层*********************/
86
+ createLineLayer(data, options) {
77
87
  ValidationUtils.validateRequired(data, 'GeoJSON data is required');
78
88
  const mergedOptions = this.mergeDefaultOptions(options);
79
89
  const features = new GeoJSON().readFeatures(data, ProjectionUtils.getGeoJSONReadOptions(mergedOptions));
80
90
  return this.createStaticLayer(new VectorSource({ features }), mergedOptions);
81
91
  }
82
- addLineByUrl(url, options = {}) {
92
+ /** *********************添加静态线图层*********************/
93
+ addLine(data, options) {
94
+ return this.toLayerHandle(this.createLineLayer(data, options));
95
+ }
96
+ /** *********************从 URL 添加静态线图层*********************/
97
+ async addLineByUrl(url, options) {
83
98
  ValidationUtils.validateNonEmptyString(url, 'Line url is required');
84
- const mergedOptions = this.mergeDefaultOptions(options);
85
- const source = new VectorSource({
86
- url,
87
- format: new GeoJSON(ProjectionUtils.getGeoJSONReadOptions(mergedOptions))
88
- });
89
- return this.createStaticLayer(source, mergedOptions);
99
+ const response = await fetch(url);
100
+ if (!response.ok) {
101
+ throw new Error(`Failed to fetch line GeoJSON: ${response.status}`);
102
+ }
103
+ const json = await response.json();
104
+ return this.addLine(json, options);
90
105
  }
91
106
  removeLineLayer(layerName) {
92
107
  ValidationUtils.validateLayerName(layerName);
93
108
  MapTools.removeLayer(this.map, layerName);
94
109
  }
95
- addFlowLine(data, options = {}) {
110
+ addFlowLine(data, options) {
96
111
  const mergedOptions = this.mergeFlowLineOptions(options);
97
112
  const layerName = mergedOptions.layerName;
98
113
  try {
@@ -113,7 +128,7 @@ export default class Line {
113
128
  return null;
114
129
  }
115
130
  }
116
- async addFlowLineByUrl(url, options = {}) {
131
+ async addFlowLineByUrl(url, options) {
117
132
  const mergedOptions = this.mergeFlowLineOptions(options);
118
133
  try {
119
134
  ValidationUtils.validateNonEmptyString(url, 'Flow line url is required');
@@ -141,4 +156,17 @@ export default class Line {
141
156
  }
142
157
  MapTools.removeLayer(this.map, [layerName, `${layerName}__flow-animation`]);
143
158
  }
159
+ /**
160
+ * 销毁本实例创建的所有流动线动画。供 MyOl.destroy 调用,
161
+ * 确保地图销毁后所有 requestAnimationFrame / postrender 监听被回收。
162
+ */
163
+ destroyAllFlowLines() {
164
+ Array.from(this.flowLineRegistry.values()).forEach(handle => {
165
+ try {
166
+ handle.remove();
167
+ }
168
+ catch { /* ignore */ }
169
+ });
170
+ this.flowLineRegistry.clear();
171
+ }
144
172
  }
@@ -1,36 +1,160 @@
1
+ import type { Positioning } from "ol/Overlay";
2
+ /**
3
+ * P2-1:内部默认配置存储与 setDefaults 覆盖机制。
4
+ *
5
+ * 设计要点:
6
+ * - 每组默认值都通过 getter 暴露,运行时 setDefaults 可叠加 partial 覆盖
7
+ * - 旧 callsite 直接 spread `ConfigManager.DEFAULT_X` 不需要改一行代码
8
+ * - 嵌套对象(如 flowSymbol)做深合并
9
+ */
10
+ interface DefaultsRegistry {
11
+ POINT_OPTIONS: {
12
+ visible: boolean;
13
+ zIndex: number;
14
+ };
15
+ POINT_TEXT_OPTIONS: {
16
+ textFont: string;
17
+ textFillColor: string;
18
+ textStrokeColor: string;
19
+ textStrokeWidth: number;
20
+ textOffsetY: number;
21
+ };
22
+ POINT_ICON_SCALE: number;
23
+ CLUSTER_OPTIONS: {
24
+ distance: number;
25
+ minDistance: number;
26
+ zIndex: number;
27
+ };
28
+ DOM_POINT_OVERLAY_OPTIONS: {
29
+ positioning: Positioning;
30
+ stopEvent: boolean;
31
+ };
32
+ LINE_OPTIONS: {
33
+ type: string;
34
+ strokeColor: string;
35
+ strokeWidth: number;
36
+ visible: boolean;
37
+ layerName: string;
38
+ zIndex: number;
39
+ };
40
+ FLOW_LINE_OPTIONS: any;
41
+ POLYGON_OPTIONS: {
42
+ zIndex: number;
43
+ visible: boolean;
44
+ textFont: string;
45
+ textFillColor: string;
46
+ textStrokeColor: string;
47
+ textStrokeWidth: number;
48
+ };
49
+ POLYGON_COLOR_MAP: Record<string, string>;
50
+ IMAGE_OPTIONS: {
51
+ opacity: number;
52
+ visible: boolean;
53
+ layerName: string;
54
+ zIndex: number;
55
+ };
56
+ MASK_OPTIONS: {
57
+ fillColor: string;
58
+ opacity: number;
59
+ visible: boolean;
60
+ layerName: string;
61
+ zIndex: number;
62
+ };
63
+ TEXT_OPTIONS: {
64
+ textFont: string;
65
+ textFillColor: string;
66
+ textStrokeColor: string;
67
+ textStrokeWidth: number;
68
+ };
69
+ HEATMAP_OPTIONS: {
70
+ blur: number;
71
+ radius: number;
72
+ zIndex: number;
73
+ opacity: number;
74
+ };
75
+ HEATMAP_VALUE_KEY: string;
76
+ TIANDITU_CONFIG: {
77
+ BASE_URL: string;
78
+ PROJECTION: string;
79
+ DEFAULT_ZINDEX: number;
80
+ ANNOTATION_ZINDEX_OFFSET: number;
81
+ };
82
+ MAP_LAYERS_OPTIONS: {
83
+ zIndex: number;
84
+ annotation: boolean;
85
+ mapClip: boolean;
86
+ mapClipData: any;
87
+ };
88
+ MYOL_OPTIONS: {
89
+ layers: any;
90
+ zoom: number;
91
+ center: number[];
92
+ extent: any;
93
+ };
94
+ VUE_TEMPLATE_POINT_OPTIONS: {
95
+ positioning: Positioning;
96
+ stopEvent: boolean;
97
+ visible: boolean;
98
+ zIndex: number;
99
+ };
100
+ RIVER_LEVEL_WIDTH_MAP: Record<number, number>;
101
+ RIVER_LAYERS_BY_ZOOM_OPTIONS: {
102
+ type: string;
103
+ levelCount: number;
104
+ zoomOffset: number;
105
+ strokeColor: string;
106
+ strokeWidth: number;
107
+ visible: boolean;
108
+ zIndex: number;
109
+ layerName: string;
110
+ removeExisting: boolean;
111
+ };
112
+ RIVER_WIDTH_BY_LEVEL_OPTIONS: {
113
+ type: string;
114
+ layerName: string;
115
+ strokeColor: string;
116
+ strokeWidth: number;
117
+ visible: boolean;
118
+ zIndex: number;
119
+ removeExisting: boolean;
120
+ };
121
+ }
1
122
  /**
2
123
  * 配置管理类
3
124
  * 用于统一管理默认配置和验证
125
+ *
126
+ * 运行时修改默认值:调用 ConfigManager.setDefaults('LINE_OPTIONS', { strokeWidth: 4 })
127
+ * 之后所有新创建的 Line/FlowLine 都将以此为默认。
4
128
  */
5
129
  export default class ConfigManager {
6
- /**
7
- * 默认点位配置
8
- */
9
- static readonly DEFAULT_POINT_OPTIONS: {
130
+ /** 运行时覆盖默认配置。partial 会与现有 default 做深合并,未提到的键保持原值。 */
131
+ static setDefaults<K extends keyof DefaultsRegistry>(group: K, partial: Partial<DefaultsRegistry[K]>): void;
132
+ /** 取当前运行时 default(含 setDefaults 覆盖后的结果),返回深拷贝避免被外部修改。 */
133
+ static getDefaults<K extends keyof DefaultsRegistry>(group: K): DefaultsRegistry[K];
134
+ /** 把所有 setDefaults 覆盖清掉,恢复到内置默认值。 */
135
+ static resetDefaults(group?: keyof DefaultsRegistry): void;
136
+ static get DEFAULT_POINT_OPTIONS(): {
10
137
  visible: boolean;
11
138
  zIndex: number;
12
139
  };
13
- static readonly DEFAULT_POINT_TEXT_OPTIONS: {
140
+ static get DEFAULT_POINT_TEXT_OPTIONS(): {
14
141
  textFont: string;
15
142
  textFillColor: string;
16
143
  textStrokeColor: string;
17
144
  textStrokeWidth: number;
18
145
  textOffsetY: number;
19
146
  };
20
- static readonly DEFAULT_POINT_ICON_SCALE = 1;
21
- static readonly DEFAULT_CLUSTER_OPTIONS: {
147
+ static get DEFAULT_POINT_ICON_SCALE(): number;
148
+ static get DEFAULT_CLUSTER_OPTIONS(): {
22
149
  distance: number;
23
150
  minDistance: number;
24
151
  zIndex: number;
25
152
  };
26
- static readonly DEFAULT_DOM_POINT_OVERLAY_OPTIONS: {
27
- readonly positioning: "center-center";
28
- readonly stopEvent: false;
153
+ static get DEFAULT_DOM_POINT_OVERLAY_OPTIONS(): {
154
+ positioning: Positioning;
155
+ stopEvent: boolean;
29
156
  };
30
- /**
31
- * 默认线配置
32
- */
33
- static readonly DEFAULT_LINE_OPTIONS: {
157
+ static get DEFAULT_LINE_OPTIONS(): {
34
158
  type: string;
35
159
  strokeColor: string;
36
160
  strokeWidth: number;
@@ -38,34 +162,8 @@ export default class ConfigManager {
38
162
  layerName: string;
39
163
  zIndex: number;
40
164
  };
41
- /**
42
- * 默认流动线动画配置
43
- */
44
- static readonly DEFAULT_FLOW_LINE_OPTIONS: {
45
- readonly duration: 4000;
46
- readonly loop: true;
47
- readonly autoStart: true;
48
- readonly showBaseLine: true;
49
- readonly animationMode: "icon";
50
- readonly flowSymbol: {
51
- readonly scale: 0.8;
52
- readonly rotateWithView: true;
53
- readonly count: 1;
54
- readonly spacing: 0.15;
55
- };
56
- readonly trailEnabled: false;
57
- readonly trailLength: 0;
58
- readonly zIndex: 16;
59
- readonly type: string;
60
- readonly strokeColor: string;
61
- readonly strokeWidth: number;
62
- readonly visible: boolean;
63
- readonly layerName: string;
64
- };
65
- /**
66
- * 默认面配置
67
- */
68
- static readonly DEFAULT_POLYGON_OPTIONS: {
165
+ static get DEFAULT_FLOW_LINE_OPTIONS(): any;
166
+ static get DEFAULT_POLYGON_OPTIONS(): {
69
167
  zIndex: number;
70
168
  visible: boolean;
71
169
  textFont: string;
@@ -73,78 +171,59 @@ export default class ConfigManager {
73
171
  textStrokeColor: string;
74
172
  textStrokeWidth: number;
75
173
  };
76
- static readonly DEFAULT_POLYGON_COLOR_MAP: {
77
- '0': string;
78
- '1': string;
79
- '2': string;
80
- '3': string;
81
- };
82
- /**
83
- * 默认图片图层配置
84
- */
85
- static readonly DEFAULT_IMAGE_OPTIONS: {
174
+ static get DEFAULT_POLYGON_COLOR_MAP(): Record<string, string>;
175
+ static get DEFAULT_IMAGE_OPTIONS(): {
86
176
  opacity: number;
87
177
  visible: boolean;
88
178
  layerName: string;
89
179
  zIndex: number;
90
180
  };
91
- /**
92
- * 默认遮罩图层配置
93
- */
94
- static readonly DEFAULT_MASK_OPTIONS: {
181
+ static get DEFAULT_MASK_OPTIONS(): {
95
182
  fillColor: string;
96
183
  opacity: number;
97
184
  visible: boolean;
98
185
  layerName: string;
186
+ zIndex: number;
99
187
  };
100
- /**
101
- * 默认文本配置
102
- */
103
- static readonly DEFAULT_TEXT_OPTIONS: {
188
+ static get DEFAULT_TEXT_OPTIONS(): {
104
189
  textFont: string;
105
190
  textFillColor: string;
106
191
  textStrokeColor: string;
107
192
  textStrokeWidth: number;
108
193
  };
109
- static readonly DEFAULT_HEATMAP_OPTIONS: {
194
+ static get DEFAULT_HEATMAP_OPTIONS(): {
110
195
  blur: number;
111
196
  radius: number;
112
197
  zIndex: number;
113
198
  opacity: number;
114
199
  };
115
- static readonly DEFAULT_HEATMAP_VALUE_KEY = "value";
116
- static readonly TIANDITU_CONFIG: {
117
- readonly BASE_URL: "//t{0-7}.tianditu.gov.cn/DataServer";
118
- readonly PROJECTION: "EPSG:4326";
119
- readonly DEFAULT_ZINDEX: 9;
120
- readonly ANNOTATION_ZINDEX_OFFSET: 10;
200
+ static get DEFAULT_HEATMAP_VALUE_KEY(): string;
201
+ static get TIANDITU_CONFIG(): {
202
+ BASE_URL: string;
203
+ PROJECTION: string;
204
+ DEFAULT_ZINDEX: number;
205
+ ANNOTATION_ZINDEX_OFFSET: number;
121
206
  };
122
- static readonly DEFAULT_MAP_LAYERS_OPTIONS: {
123
- zIndex: 9;
207
+ static get DEFAULT_MAP_LAYERS_OPTIONS(): {
208
+ zIndex: number;
124
209
  annotation: boolean;
125
210
  mapClip: boolean;
126
- mapClipData: undefined;
211
+ mapClipData: any;
127
212
  };
128
- static readonly DEFAULT_MYOL_OPTIONS: {
129
- layers: undefined;
213
+ static get DEFAULT_MYOL_OPTIONS(): {
214
+ layers: any;
130
215
  zoom: number;
131
216
  center: number[];
132
- extent: undefined;
133
- };
134
- static readonly DEFAULT_VUE_TEMPLATE_POINT_OPTIONS: {
135
- readonly positioning: "center-center";
136
- readonly stopEvent: false;
137
- readonly visible: true;
138
- readonly zIndex: 1;
139
- };
140
- static readonly DEFAULT_RIVER_LEVEL_WIDTH_MAP: {
141
- 1: number;
142
- 2: number;
143
- 3: number;
144
- 4: number;
145
- 5: number;
146
- };
147
- static readonly DEFAULT_RIVER_LAYERS_BY_ZOOM_OPTIONS: {
217
+ extent: any;
218
+ };
219
+ static get DEFAULT_VUE_TEMPLATE_POINT_OPTIONS(): {
220
+ positioning: Positioning;
221
+ stopEvent: boolean;
222
+ visible: boolean;
223
+ zIndex: number;
224
+ };
225
+ static get DEFAULT_RIVER_LEVEL_WIDTH_MAP(): Record<number, number>;
226
+ static get DEFAULT_RIVER_LAYERS_BY_ZOOM_OPTIONS(): {
148
227
  type: string;
149
228
  levelCount: number;
150
229
  zoomOffset: number;
@@ -155,7 +234,7 @@ export default class ConfigManager {
155
234
  layerName: string;
156
235
  removeExisting: boolean;
157
236
  };
158
- static readonly DEFAULT_RIVER_WIDTH_BY_LEVEL_OPTIONS: {
237
+ static get DEFAULT_RIVER_WIDTH_BY_LEVEL_OPTIONS(): {
159
238
  type: string;
160
239
  layerName: string;
161
240
  strokeColor: string;
@@ -184,3 +263,4 @@ export default class ConfigManager {
184
263
  */
185
264
  static deepClone<T>(obj: T): T;
186
265
  }
266
+ export {};