my-openlayer 2.1.1 → 2.1.2

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.
@@ -1,7 +1,7 @@
1
1
  import Map from "ol/Map";
2
2
  import { FeatureLike } from "ol/Feature";
3
3
  import { Style } from "ol/style";
4
- import { SelectOptions, SelectMode } from "../types";
4
+ import { SelectOptions, SelectMode, ProgrammaticSelectOptions } from "../types";
5
5
  /**
6
6
  * 要素选择处理器类
7
7
  * 用于在地图上选择和高亮显示要素,支持单选、多选等多种选择模式
@@ -57,7 +57,7 @@ export default class SelectHandler {
57
57
  constructor(map: Map);
58
58
  /**
59
59
  * 启用要素选择
60
- * @param mode 选择模式:'click'(点击)、'hover'(悬停)、'box'(框选)、'ctrl'(Ctrl+点击)
60
+ * @param mode 选择模式:'click'(点击)、'hover'(悬停)、'ctrl'(Ctrl+点击)
61
61
  * @param options 选择配置选项
62
62
  * @returns SelectHandler 实例(支持链式调用)
63
63
  */
@@ -80,20 +80,18 @@ export default class SelectHandler {
80
80
  /**
81
81
  * 通过要素ID选择要素
82
82
  * @param featureIds 要素ID数组
83
- * @param layerName 图层名称(可选)
84
- * @param selectStyle 选中样式(可选,仅作用于此次选择)
83
+ * @param options 编程式选择配置选项
85
84
  * @returns SelectHandler 实例(支持链式调用)
86
85
  */
87
- selectByIds(featureIds: string[], layerName?: string, selectStyle?: Style | Style[] | ((feature: FeatureLike) => Style | Style[])): this;
86
+ selectByIds(featureIds: string[], options?: ProgrammaticSelectOptions): this;
88
87
  /**
89
88
  * 通过属性选择要素
90
89
  * @param propertyName 属性名称
91
90
  * @param propertyValue 属性值
92
- * @param layerName 图层名称(可选)
93
- * @param selectStyle 选中样式(可选,仅作用于此次选择)
91
+ * @param options 编程式选择配置选项
94
92
  * @returns SelectHandler 实例(支持链式调用)
95
93
  */
96
- selectByProperty(propertyName: string, propertyValue: any, layerName?: string, selectStyle?: Style | Style[] | ((feature: FeatureLike) => Style | Style[])): this;
94
+ selectByProperty(propertyName: string, propertyValue: any, options?: ProgrammaticSelectOptions): this;
97
95
  /**
98
96
  * 判断选择是否已启用
99
97
  * @returns 是否已启用
@@ -104,6 +102,14 @@ export default class SelectHandler {
104
102
  * @returns 当前选择模式
105
103
  */
106
104
  getCurrentMode(): SelectMode | undefined;
105
+ /**
106
+ * 定位至要素
107
+ * @private
108
+ * @param features 要素数组
109
+ * @param duration 动画持续时间(毫秒),默认500
110
+ * @param padding 边距(像素),默认100
111
+ */
112
+ private fitToFeatures;
107
113
  /**
108
114
  * 销毁选择处理器,清理所有资源
109
115
  */
@@ -74,7 +74,7 @@ export default class SelectHandler {
74
74
  }
75
75
  /**
76
76
  * 启用要素选择
77
- * @param mode 选择模式:'click'(点击)、'hover'(悬停)、'box'(框选)、'ctrl'(Ctrl+点击)
77
+ * @param mode 选择模式:'click'(点击)、'hover'(悬停)、'ctrl'(Ctrl+点击)
78
78
  * @param options 选择配置选项
79
79
  * @returns SelectHandler 实例(支持链式调用)
80
80
  */
@@ -146,11 +146,10 @@ export default class SelectHandler {
146
146
  /**
147
147
  * 通过要素ID选择要素
148
148
  * @param featureIds 要素ID数组
149
- * @param layerName 图层名称(可选)
150
- * @param selectStyle 选中样式(可选,仅作用于此次选择)
149
+ * @param options 编程式选择配置选项
151
150
  * @returns SelectHandler 实例(支持链式调用)
152
151
  */
153
- selectByIds(featureIds, layerName, selectStyle) {
152
+ selectByIds(featureIds, options) {
154
153
  try {
155
154
  if (!this.selectInteraction) {
156
155
  console.warn('选择交互未启用,无法选择要素');
@@ -162,11 +161,13 @@ export default class SelectHandler {
162
161
  }
163
162
  // 清除当前选择
164
163
  this.clearSelection();
164
+ // 临时存储选中的要素
165
+ const selectedFeatures = [];
165
166
  // 获取所有图层
166
167
  const layers = this.map.getLayers().getArray();
167
168
  for (const layer of layers) {
168
169
  // 过滤图层
169
- if (layerName && layer.get('layerName') !== layerName) {
170
+ if (options?.layerName && layer.get('layerName') !== options.layerName) {
170
171
  continue;
171
172
  }
172
173
  if (layer instanceof VectorLayer) {
@@ -177,24 +178,31 @@ export default class SelectHandler {
177
178
  for (const featureId of featureIds) {
178
179
  const feature = source.getFeatureById(featureId);
179
180
  if (feature) {
180
- // 如果传入了自定义样式,仅为该要素设置样式
181
- if (selectStyle) {
182
- if (typeof selectStyle === 'function') {
183
- feature.setStyle(selectStyle(feature));
184
- }
185
- else {
186
- feature.setStyle(selectStyle);
187
- }
188
- }
181
+ selectedFeatures.push(feature);
189
182
  this.selectInteraction.getFeatures().push(feature);
190
183
  }
191
184
  }
192
185
  }
193
186
  }
187
+ // 如果传入了自定义样式,为选中的要素设置样式
188
+ if (options?.selectStyle && selectedFeatures.length > 0) {
189
+ for (const feature of selectedFeatures) {
190
+ if (typeof options.selectStyle === 'function') {
191
+ feature.setStyle(options.selectStyle(feature));
192
+ }
193
+ else {
194
+ feature.setStyle(options.selectStyle);
195
+ }
196
+ }
197
+ }
198
+ // 定位至选中要素
199
+ if (options?.fitView && selectedFeatures.length > 0) {
200
+ this.fitToFeatures(selectedFeatures, options.fitDuration ?? 500, options.fitPadding ?? 100);
201
+ }
194
202
  return this;
195
203
  }
196
204
  catch (error) {
197
- this.errorHandler.handleError(new MyOpenLayersError(`通过ID选择要素失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.COMPONENT_ERROR, { featureIds, layerName }));
205
+ this.errorHandler.handleError(new MyOpenLayersError(`通过ID选择要素失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.COMPONENT_ERROR, { featureIds, options }));
198
206
  throw error;
199
207
  }
200
208
  }
@@ -202,11 +210,10 @@ export default class SelectHandler {
202
210
  * 通过属性选择要素
203
211
  * @param propertyName 属性名称
204
212
  * @param propertyValue 属性值
205
- * @param layerName 图层名称(可选)
206
- * @param selectStyle 选中样式(可选,仅作用于此次选择)
213
+ * @param options 编程式选择配置选项
207
214
  * @returns SelectHandler 实例(支持链式调用)
208
215
  */
209
- selectByProperty(propertyName, propertyValue, layerName, selectStyle) {
216
+ selectByProperty(propertyName, propertyValue, options) {
210
217
  try {
211
218
  if (!this.selectInteraction) {
212
219
  console.warn('选择交互未启用,无法选择要素');
@@ -217,11 +224,13 @@ export default class SelectHandler {
217
224
  }
218
225
  // 清除当前选择
219
226
  this.clearSelection();
227
+ // 临时存储选中的要素
228
+ const selectedFeatures = [];
220
229
  // 获取所有图层
221
230
  const layers = this.map.getLayers().getArray();
222
231
  for (const layer of layers) {
223
232
  // 过滤图层
224
- if (layerName && layer.get('layerName') !== layerName) {
233
+ if (options?.layerName && layer.get('layerName') !== options.layerName) {
225
234
  continue;
226
235
  }
227
236
  if (layer instanceof VectorLayer) {
@@ -232,24 +241,31 @@ export default class SelectHandler {
232
241
  const features = source.getFeatures();
233
242
  for (const feature of features) {
234
243
  if (feature.get(propertyName) === propertyValue) {
235
- // 如果传入了自定义样式,仅为该要素设置样式
236
- if (selectStyle) {
237
- if (typeof selectStyle === 'function') {
238
- feature.setStyle(selectStyle(feature));
239
- }
240
- else {
241
- feature.setStyle(selectStyle);
242
- }
243
- }
244
+ selectedFeatures.push(feature);
244
245
  this.selectInteraction.getFeatures().push(feature);
245
246
  }
246
247
  }
247
248
  }
248
249
  }
250
+ // 如果传入了自定义样式,为选中的要素设置样式
251
+ if (options?.selectStyle && selectedFeatures.length > 0) {
252
+ for (const feature of selectedFeatures) {
253
+ if (typeof options.selectStyle === 'function') {
254
+ feature.setStyle(options.selectStyle(feature));
255
+ }
256
+ else {
257
+ feature.setStyle(options.selectStyle);
258
+ }
259
+ }
260
+ }
261
+ // 定位至选中要素
262
+ if (options?.fitView && selectedFeatures.length > 0) {
263
+ this.fitToFeatures(selectedFeatures, options.fitDuration ?? 500, options.fitPadding ?? 100);
264
+ }
249
265
  return this;
250
266
  }
251
267
  catch (error) {
252
- this.errorHandler.handleError(new MyOpenLayersError(`通过属性选择要素失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.COMPONENT_ERROR, { propertyName, propertyValue, layerName }));
268
+ this.errorHandler.handleError(new MyOpenLayersError(`通过属性选择要素失败: ${error instanceof Error ? error.message : '未知错误'}`, ErrorType.COMPONENT_ERROR, { propertyName, propertyValue, options }));
253
269
  throw error;
254
270
  }
255
271
  }
@@ -267,6 +283,49 @@ export default class SelectHandler {
267
283
  getCurrentMode() {
268
284
  return this.currentMode;
269
285
  }
286
+ /**
287
+ * 定位至要素
288
+ * @private
289
+ * @param features 要素数组
290
+ * @param duration 动画持续时间(毫秒),默认500
291
+ * @param padding 边距(像素),默认100
292
+ */
293
+ fitToFeatures(features, duration = 500, padding = 100) {
294
+ try {
295
+ if (!features || features.length === 0) {
296
+ return;
297
+ }
298
+ // 创建一个包含所有要素的范围
299
+ let extent;
300
+ for (const feature of features) {
301
+ const geometry = feature.getGeometry();
302
+ if (geometry) {
303
+ const featureExtent = geometry.getExtent();
304
+ if (!extent) {
305
+ extent = featureExtent;
306
+ }
307
+ else {
308
+ // 扩展范围以包含当前要素
309
+ extent = [
310
+ Math.min(extent[0], featureExtent[0]),
311
+ Math.min(extent[1], featureExtent[1]),
312
+ Math.max(extent[2], featureExtent[2]),
313
+ Math.max(extent[3], featureExtent[3])
314
+ ];
315
+ }
316
+ }
317
+ }
318
+ if (extent) {
319
+ this.map.getView().fit(extent, {
320
+ duration,
321
+ padding: [padding, padding, padding, padding]
322
+ });
323
+ }
324
+ }
325
+ catch (error) {
326
+ console.error('定位至要素失败:', error);
327
+ }
328
+ }
270
329
  /**
271
330
  * 销毁选择处理器,清理所有资源
272
331
  */
@@ -329,9 +388,6 @@ export default class SelectHandler {
329
388
  return pointerMove;
330
389
  case 'ctrl':
331
390
  return platformModifierKeyOnly;
332
- case 'box':
333
- // 框选需要额外的 DragBox 交互,这里暂不实现
334
- return click;
335
391
  default:
336
392
  return click;
337
393
  }
package/index.d.ts CHANGED
@@ -16,4 +16,4 @@ export { ValidationUtils } from './utils/ValidationUtils';
16
16
  export type { BaseOptions, StyleOptions, TextOptions } from './types';
17
17
  export type { PointOptions, LineOptions, PolygonOptions } from './types';
18
18
  export type { OptionsType } from './types';
19
- export type { MapInitType, MapLayersOptions, HeatMapOptions, ImageLayerData, MaskLayerOptions, ColorMap, FeatureColorUpdateOptions, PointData, LineData, ClusterOptions, MeasureHandlerType, VueTemplatePointOptions, MapJSONData, FeatureData, AnnotationType, TiandituType, MapLayers, AnnotationLayerOptions, SelectOptions, SelectMode, SelectCallbackEvent } from './types';
19
+ export type { MapInitType, MapLayersOptions, HeatMapOptions, ImageLayerData, MaskLayerOptions, ColorMap, FeatureColorUpdateOptions, PointData, LineData, ClusterOptions, MeasureHandlerType, VueTemplatePointOptions, MapJSONData, FeatureData, AnnotationType, TiandituType, MapLayers, AnnotationLayerOptions, SelectOptions, SelectMode, SelectCallbackEvent, ProgrammaticSelectOptions } from './types';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "2.1.1",
4
+ "version": "2.1.2",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
package/types.d.ts CHANGED
@@ -306,7 +306,7 @@ export interface VueTemplatePointInstance {
306
306
  /**
307
307
  * 选择模式类型
308
308
  */
309
- export type SelectMode = 'click' | 'hover' | 'box' | 'ctrl';
309
+ export type SelectMode = 'click' | 'hover' | 'ctrl';
310
310
  /**
311
311
  * 选择回调事件接口
312
312
  */
@@ -337,4 +337,19 @@ export interface SelectOptions {
337
337
  /** 取消选中要素时的回调函数 */
338
338
  onDeselect?: (event: SelectCallbackEvent) => void;
339
339
  }
340
+ /**
341
+ * 编程式选择选项接口
342
+ */
343
+ export interface ProgrammaticSelectOptions {
344
+ /** 图层名称,指定在哪个图层中选择要素 */
345
+ layerName?: string;
346
+ /** 自定义选中样式(仅作用于此次选择) */
347
+ selectStyle?: Style | Style[] | ((feature: FeatureLike) => Style | Style[]);
348
+ /** 是否定位至选中要素,默认 false */
349
+ fitView?: boolean;
350
+ /** 定位动画持续时间(毫秒),默认 500 */
351
+ fitDuration?: number;
352
+ /** 定位时的边距(像素),默认 100 */
353
+ fitPadding?: number;
354
+ }
340
355
  export {};