my-openlayer 2.1.11 → 2.2.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.
@@ -62,6 +62,15 @@ export default class MapTools {
62
62
  * @throws 当参数无效时抛出错误
63
63
  */
64
64
  static setLayerVisible: (map: Map, layerName: string, visible: boolean) => void;
65
+ /**
66
+ * 地图定位
67
+ * @param lgtd 经度
68
+ * @param lttd 纬度
69
+ * @param zoom 缩放级别
70
+ * @param duration 动画时长
71
+ * @returns 定位是否成功
72
+ */
73
+ locationAction(lgtd: number, lttd: number, zoom?: number, duration?: number): boolean;
65
74
  /**
66
75
  * 获取地图实例
67
76
  * @returns 地图实例
package/core/MapTools.js CHANGED
@@ -189,6 +189,30 @@ class MapTools {
189
189
  throw new Error('Failed to set layer visibility');
190
190
  }
191
191
  }
192
+ /**
193
+ * 地图定位
194
+ * @param lgtd 经度
195
+ * @param lttd 纬度
196
+ * @param zoom 缩放级别
197
+ * @param duration 动画时长
198
+ * @returns 定位是否成功
199
+ */
200
+ locationAction(lgtd, lttd, zoom = 20, duration = 3000) {
201
+ if (!this.map) {
202
+ throw new Error('Map instance is not available');
203
+ }
204
+ if (!ValidationUtils.validateLngLat(lgtd, lttd)) {
205
+ return false;
206
+ }
207
+ try {
208
+ this.map.getView().animate({ center: [lgtd, lttd], zoom, duration });
209
+ return true;
210
+ }
211
+ catch (error) {
212
+ this.errorHandler.error('[地图定位]', '定位失败:', error);
213
+ return false;
214
+ }
215
+ }
192
216
  /**
193
217
  * 获取地图实例
194
218
  * @returns 地图实例
package/core/Point.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import Map from "ol/Map";
2
+ import Overlay from 'ol/Overlay';
2
3
  import VectorLayer from "ol/layer/Vector";
3
4
  import VectorSource from "ol/source/Vector";
4
- import { MapJSONData, PointOptions, ClusterOptions, PointData, VueTemplatePointInstance } from '../types';
5
+ import { PointOptions, ClusterOptions, PointData, VueTemplatePointInstance, TwinkleItem } from '../types';
5
6
  export default class Point {
6
7
  private map;
7
8
  constructor(map: Map);
@@ -54,27 +55,16 @@ export default class Point {
54
55
  */
55
56
  addPoint(pointData: PointData[], options: PointOptions): VectorLayer<VectorSource> | null;
56
57
  addClusterPoint(pointData: PointData[], options: ClusterOptions): VectorLayer<VectorSource> | null;
57
- addTwinkleLayerFromPolygon(twinkleList: any[], className: string, key: string, json: MapJSONData): void;
58
58
  /**
59
59
  * 添加闪烁点
60
- * @param twinkleList 闪烁点数据 - 二维数组 [[],[]]
61
- * @param className 闪烁点样式,需要和id保持一致
62
- * @param key 闪烁点索引
60
+ * @param twinkleList 闪烁点数据
63
61
  * @param callback
64
62
  */
65
- addTwinkleLayer(twinkleList: any[], className: string | undefined, key: string, callback?: Function): void;
66
- /**
67
- * 地图定位
68
- * @param lgtd 经度
69
- * @param lttd 纬度
70
- * @param zoom 缩放级别
71
- * @param duration 动画时长
72
- */
73
- locationAction(lgtd: number, lttd: number, zoom?: number, duration?: number): boolean;
74
- /**
75
- * 设置dom元素为点位
76
- */
77
- addDomPoint(id: string, lgtd: number, lttd: number): boolean;
63
+ addDomPoint(twinkleList: TwinkleItem[], callback?: Function): {
64
+ anchors: Overlay[];
65
+ remove: () => void;
66
+ setVisible: (visible: boolean) => void;
67
+ };
78
68
  /**
79
69
  * 添加vue组件为点位
80
70
  * @param pointDataList 点位信息列表
@@ -92,4 +82,13 @@ export default class Point {
92
82
  remove: () => void;
93
83
  getPoints: () => VueTemplatePointInstance[];
94
84
  };
85
+ /**
86
+ * 地图定位
87
+ * @deprecated 请使用 MapTools.locationAction 方法代替
88
+ * @param lgtd 经度
89
+ * @param lttd 纬度
90
+ * @param zoom 缩放级别
91
+ * @param duration 动画时长
92
+ */
93
+ locationAction(lgtd: number, lttd: number, zoom?: number, duration?: number): boolean;
95
94
  }
package/core/Point.js CHANGED
@@ -6,12 +6,9 @@ import { Text, Style, Fill, Stroke, Icon } from "ol/style";
6
6
  import VectorLayer from "ol/layer/Vector";
7
7
  import VectorSource from "ol/source/Vector";
8
8
  import { Cluster } from 'ol/source';
9
- import * as turf from '@turf/turf';
10
- import GeoJSON from "ol/format/GeoJSON";
11
9
  import VueTemplatePoint from './VueTemplatePoint';
12
- import MapTools from "./MapTools";
13
10
  import { ValidationUtils } from '../utils/ValidationUtils';
14
- import { ErrorHandler } from '../utils/ErrorHandler';
11
+ import MapTools from './MapTools';
15
12
  export default class Point {
16
13
  constructor(map) {
17
14
  this.map = map;
@@ -179,160 +176,122 @@ export default class Point {
179
176
  this.configureLayer(clusterLayer, options);
180
177
  return clusterLayer;
181
178
  }
182
- // 在流域中心添加闪烁点位
183
- addTwinkleLayerFromPolygon(twinkleList, className, key, json) {
184
- new MapTools(this.map).removeLayer('twinklePoint');
185
- // 计算多边形的中心点坐标
186
- const calculatePolygonCenter = (polygonCoordinates) => {
187
- const polygon = turf.polygon(polygonCoordinates[0]);
188
- const centroid = turf.centroid(polygon);
189
- return centroid.geometry.coordinates;
190
- };
191
- const features = json.features;
192
- const vectorSource = new VectorSource({
193
- format: new GeoJSON(),
194
- });
195
- twinkleList.forEach(item => {
196
- const feature = features.find((ele) => {
197
- return ele.properties.BASIN === item.idx;
198
- });
199
- if (!feature)
200
- return;
201
- feature.properties.level = item.lev;
202
- const geojson = new GeoJSON();
203
- const olFeature = geojson.readFeature(feature);
204
- if (Array.isArray(olFeature)) {
205
- vectorSource.addFeatures(olFeature);
206
- }
207
- else {
208
- vectorSource.addFeature(olFeature);
209
- }
210
- if (feature) {
211
- const polygonCenter = calculatePolygonCenter(feature.geometry.coordinates);
212
- item.lgtd = polygonCenter[0];
213
- item.lttd = polygonCenter[1];
214
- }
215
- });
216
- const basinLayer = new VectorLayer({
217
- name: 'twinklePoint',
218
- layerName: 'twinklePoint',
219
- source: vectorSource,
220
- style: function (feature) {
221
- return new Style({
222
- stroke: new Stroke({
223
- color: 'rgb(139,188,245)',
224
- width: 3
225
- }),
226
- fill: new Fill({ color: 'rgba(255, 255, 255, 0)' }),
227
- text: new Text({
228
- text: feature.values_['BASIN'] || "",
229
- font: '14px Calibri,sans-serif',
230
- fill: new Fill({ color: '#FFF' }),
231
- stroke: new Stroke({
232
- color: '#409EFF', width: 2
233
- }),
234
- })
235
- });
236
- },
237
- zIndex: 21
238
- });
239
- this.map.addLayer(basinLayer);
240
- this.addTwinkleLayer(twinkleList, className, key, (twinkleItem) => {
241
- });
242
- }
179
+ // // 在流域中心添加闪烁点位
180
+ // addTwinkleLayerFromPolygon(twinkleList: any[], className: string, key: string, json: MapJSONData, options?: PolygonOptions) {
181
+ // new MapTools(this.map).removeLayer('twinklePoint')
182
+ // // 计算多边形的中心点坐标
183
+ // const calculatePolygonCenter = (polygonCoordinates: any) => {
184
+ // const polygon = turf.polygon(polygonCoordinates[0]);
185
+ // const centroid = turf.centroid(polygon);
186
+ // return centroid.geometry.coordinates;
187
+ // };
188
+ // const features: any[] = json.features
189
+ // const vectorSource = new VectorSource({
190
+ // format: new GeoJSON(),
191
+ // });
192
+ // twinkleList.forEach(item => {
193
+ // const feature = features.find((ele: any) => {
194
+ // return ele.properties.BASIN === item.idx
195
+ // })
196
+ // if (!feature) return
197
+ // feature.properties.level = item.lev
198
+ // const geojson = new GeoJSON();
199
+ // const olFeature = geojson.readFeature(feature);
200
+ // if (Array.isArray(olFeature)) {
201
+ // vectorSource.addFeatures(olFeature);
202
+ // } else {
203
+ // vectorSource.addFeature(olFeature);
204
+ // }
205
+ // if (feature) {
206
+ // const polygonCenter = calculatePolygonCenter(feature.geometry.coordinates)
207
+ // item.lgtd = polygonCenter[0]
208
+ // item.lttd = polygonCenter[1]
209
+ // }
210
+ // })
211
+ // const basinLayer = new VectorLayer({
212
+ // name: 'twinklePoint',
213
+ // layerName: 'twinklePoint',
214
+ // source: vectorSource,
215
+ // style: function (feature: any) {
216
+ // if (options?.style) {
217
+ // if (typeof options.style === 'function') {
218
+ // return options.style(feature);
219
+ // } else {
220
+ // return options.style;
221
+ // }
222
+ // }
223
+ // return new Style({
224
+ // stroke: new Stroke({
225
+ // color: 'rgb(139,188,245)',
226
+ // width: 3
227
+ // }),
228
+ // fill: new Fill({ color: 'rgba(255, 255, 255, 0)' }),
229
+ // text: new Text({
230
+ // text: feature.values_['BASIN'] || "",
231
+ // font: '14px Calibri,sans-serif',
232
+ // fill: new Fill({ color: '#FFF' }),
233
+ // stroke: new Stroke({
234
+ // color: '#409EFF', width: 2
235
+ // }),
236
+ // })
237
+ // })
238
+ // },
239
+ // zIndex: 21
240
+ // } as any)
241
+ // this.map.addLayer(basinLayer)
242
+ // this.addTwinkleLayer(twinkleList.map(item => ({...item, className: item[key]})), className, key)
243
+ // }
243
244
  /**
244
245
  * 添加闪烁点
245
- * @param twinkleList 闪烁点数据 - 二维数组 [[],[]]
246
- * @param className 闪烁点样式,需要和id保持一致
247
- * @param key 闪烁点索引
246
+ * @param twinkleList 闪烁点数据
248
247
  * @param callback
249
248
  */
250
- addTwinkleLayer(twinkleList, className = 'marker_warning', key, callback) {
251
- // 查找class是warn-points的dom,并删除
252
- const arr = document.getElementsByClassName(className);
253
- const l = arr.length;
254
- for (let i = l - 1; i >= 0; i--) {
255
- if (arr[i] !== null) {
256
- arr[i].parentNode?.removeChild(arr[i]);
249
+ addDomPoint(twinkleList, callback) {
250
+ let anchors = [];
251
+ twinkleList.forEach(twinkleItem => {
252
+ // 创建DOM元素
253
+ const element = document.createElement('div');
254
+ element.className = twinkleItem.className || '';
255
+ // 添加点击事件
256
+ if (callback) {
257
+ element.addEventListener('click', () => {
258
+ callback(twinkleItem);
259
+ });
257
260
  }
258
- }
259
- const el = document.getElementById(className);
260
- for (let i = 0; i < twinkleList.length; i++) {
261
- const twinkleItem = twinkleList[i];
262
- // 定义图标Dom
263
- const el2 = document.createElement('div');
264
- el2.id = className + i;
265
- el2.className = className + twinkleItem[key];
266
- el2.onclick = () => {
267
- callback && callback(twinkleItem);
268
- // bus.emit('twinkleClick', twinkleItem)
269
- };
270
- // 插入图标
271
- if (el)
272
- el.insertAdjacentElement('afterend', el2);
273
261
  // 创建一个覆盖物
274
262
  const anchor = new Overlay({
275
- element: document.getElementById(className + i) || undefined,
263
+ element: element,
276
264
  positioning: 'center-center',
277
- className: className
265
+ stopEvent: false // 允许事件穿透,但我们在上面阻止了冒泡
278
266
  });
279
267
  // 关键的一点,需要设置附加到地图上的位置
280
268
  anchor.setPosition([twinkleItem.lgtd, twinkleItem.lttd]);
281
269
  // 然后添加到map上
282
270
  this.map.addOverlay(anchor);
283
- }
284
- }
285
- /**
286
- * 地图定位
287
- * @param lgtd 经度
288
- * @param lttd 纬度
289
- * @param zoom 缩放级别
290
- * @param duration 动画时长
291
- */
292
- locationAction(lgtd, lttd, zoom = 20, duration = 3000) {
293
- if (!ValidationUtils.validateLngLat(lgtd, lttd)) {
294
- return false;
295
- }
296
- try {
297
- this.map.getView().animate({ center: [lgtd, lttd], zoom, duration });
298
- return true;
299
- }
300
- catch (error) {
301
- ErrorHandler.getInstance().error('[地图定位]', '定位失败:', error);
302
- return false;
303
- }
304
- }
305
- /**
306
- * 设置dom元素为点位
307
- */
308
- addDomPoint(id, lgtd, lttd) {
309
- if (!id) {
310
- ErrorHandler.getInstance().error('Element ID is required');
311
- return false;
312
- }
313
- if (!ValidationUtils.validateLngLat(lgtd, lttd)) {
314
- return false;
315
- }
316
- const el = document.getElementById(id);
317
- if (!el) {
318
- ErrorHandler.getInstance().error(`Element with id '${id}' not found`);
319
- return false;
320
- }
321
- try {
322
- const anchor = new Overlay({
323
- id: id,
324
- element: el,
325
- positioning: 'center-center',
326
- stopEvent: false
327
- });
328
- anchor.setPosition([lgtd, lttd]);
329
- this.map.addOverlay(anchor);
330
- return true;
331
- }
332
- catch (error) {
333
- ErrorHandler.getInstance().error('Failed to set DOM point:', error);
334
- return false;
335
- }
271
+ anchors.push(anchor);
272
+ });
273
+ return {
274
+ anchors,
275
+ remove: () => {
276
+ anchors.forEach(anchor => {
277
+ console.log('Removing overlay:', anchor);
278
+ const element = anchor.getElement();
279
+ if (element) {
280
+ element.remove();
281
+ }
282
+ this.map.removeOverlay(anchor);
283
+ });
284
+ anchors = [];
285
+ },
286
+ setVisible: (visible) => {
287
+ anchors.forEach(anchor => {
288
+ const element = anchor.getElement();
289
+ if (element) {
290
+ element.style.display = visible ? '' : 'none';
291
+ }
292
+ });
293
+ }
294
+ };
336
295
  }
337
296
  /**
338
297
  * 添加vue组件为点位
@@ -357,4 +316,15 @@ export default class Point {
357
316
  throw new Error(`Failed to create Vue template points: ${error}`);
358
317
  }
359
318
  }
319
+ /**
320
+ * 地图定位
321
+ * @deprecated 请使用 MapTools.locationAction 方法代替
322
+ * @param lgtd 经度
323
+ * @param lttd 纬度
324
+ * @param zoom 缩放级别
325
+ * @param duration 动画时长
326
+ */
327
+ locationAction(lgtd, lttd, zoom = 20, duration = 3000) {
328
+ return new MapTools(this.map).locationAction(lgtd, lttd, zoom, duration);
329
+ }
360
330
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "2.1.11",
4
+ "version": "2.2.0",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
package/types.d.ts CHANGED
@@ -233,6 +233,13 @@ export interface ClusterOptions extends PointOptions {
233
233
  /** 最小聚合距离 */
234
234
  minDistance?: number;
235
235
  }
236
+ /**
237
+ * 闪烁点数据接口
238
+ */
239
+ export interface TwinkleItem extends PointData {
240
+ className?: string;
241
+ [key: string]: any;
242
+ }
236
243
  /**
237
244
  * 测量处理器类型
238
245
  */