my-openlayer 1.0.9 → 1.0.11

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.
package/MyOl.js CHANGED
@@ -40,13 +40,16 @@ class MyOl {
40
40
  // 准备图层
41
41
  const layers = Array.isArray(this.options.layers) ? this.options.layers : [];
42
42
  // 创建地图实例
43
- this.map = new Map({
43
+ // 确保 view 选项不会传递给 OpenLayers Map 构造函数,避免 "then is not a function" 错误
44
+ // 我们完全控制传递给 Map 的选项,不直接传递用户的 options
45
+ const mapOptions = {
44
46
  target: id,
45
- view: MyOl.createView(this.options),
47
+ view: this.options.view || MyOl.createView(this.options),
46
48
  layers: layers,
47
49
  controls: this.createControls()
48
- });
49
- if (layers.length === 0 || this.options.annotation) {
50
+ };
51
+ this.map = new Map(mapOptions);
52
+ if (this.options.token && (layers.length === 0 || this.options.annotation)) {
50
53
  this.getMapBaseLayers();
51
54
  }
52
55
  // 初始化基础事件监听(地图错误等)
@@ -136,8 +139,8 @@ class MyOl {
136
139
  projection,
137
140
  center: olProjFromLonLat(options.center, projection),
138
141
  zoom: options.zoom ?? MyOl.DefaultOptions.zoom,
139
- minZoom: options.minZoom ?? MyOl.DefaultOptions.minZoom,
140
- maxZoom: options.maxZoom ?? MyOl.DefaultOptions.maxZoom,
142
+ minZoom: options.minZoom,
143
+ maxZoom: options.maxZoom,
141
144
  ...(options.extent && { extent: options.extent })
142
145
  };
143
146
  return new View(viewOptions);
@@ -369,8 +372,6 @@ MyOl.DefaultOptions = {
369
372
  layers: undefined,
370
373
  zoom: 10,
371
374
  center: [119.81, 29.969],
372
- minZoom: 8,
373
- maxZoom: 20,
374
375
  extent: undefined
375
376
  };
376
377
  // 坐标系配置
package/core/Point.d.ts CHANGED
@@ -79,12 +79,11 @@ export default class Point {
79
79
  * 添加vue组件为点位
80
80
  * @param pointDataList 点位信息列表
81
81
  * @param template vue组件模板
82
- * @param options 选项,包含Vue实例
82
+ * @param Vue Vue实例
83
83
  * @returns 返回控制对象,包含显示、隐藏、移除方法
84
84
  * @throws 当参数无效时抛出错误
85
85
  */
86
86
  addVueTemplatePoint(pointDataList: PointData[], template: any, options?: {
87
- Vue?: any;
88
87
  positioning?: 'bottom-left' | 'bottom-center' | 'bottom-right' | 'center-left' | 'center-center' | 'center-right' | 'top-left' | 'top-center' | 'top-right';
89
88
  stopEvent?: boolean;
90
89
  }): {
package/core/Point.js CHANGED
@@ -321,7 +321,7 @@ export default class Point {
321
321
  * 添加vue组件为点位
322
322
  * @param pointDataList 点位信息列表
323
323
  * @param template vue组件模板
324
- * @param options 选项,包含Vue实例
324
+ * @param Vue Vue实例
325
325
  * @returns 返回控制对象,包含显示、隐藏、移除方法
326
326
  * @throws 当参数无效时抛出错误
327
327
  */
@@ -332,9 +332,6 @@ export default class Point {
332
332
  if (!template) {
333
333
  throw new Error('Vue template is required');
334
334
  }
335
- if (!options?.Vue) {
336
- throw new Error('Vue instance is required in options');
337
- }
338
335
  try {
339
336
  const vueTemplatePoint = new VueTemplatePoint(this.map);
340
337
  return vueTemplatePoint.addVueTemplatePoint(pointDataList, template, options);
package/core/Polygon.js CHANGED
@@ -117,11 +117,9 @@ export default class Polygon {
117
117
  */
118
118
  setFeatureStyles(features, options) {
119
119
  features.forEach(feature => {
120
- feature.set('type', options.type || options.layerName);
121
- feature.set('layerName', options.type || options.layerName);
122
- const fillColor = options.fillColorCallBack
123
- ? options.fillColorCallBack(feature)
124
- : options.fillColor;
120
+ feature.set('type', options.layerName);
121
+ feature.set('layerName', options.layerName);
122
+ const fillColor = options.fillColorCallBack ? options.fillColorCallBack(feature) : options.fillColor;
125
123
  const featureStyle = new Style({
126
124
  stroke: new Stroke({
127
125
  color: options.strokeColor,
@@ -21,7 +21,6 @@ export default class VueTemplatePoint {
21
21
  * @returns 点位控制器
22
22
  */
23
23
  addVueTemplatePoint(pointDataList: any[], template: any, options?: {
24
- Vue?: any;
25
24
  positioning?: 'bottom-left' | 'bottom-center' | 'bottom-right' | 'center-left' | 'center-center' | 'center-right' | 'top-left' | 'top-center' | 'top-right';
26
25
  stopEvent?: boolean;
27
26
  }): {
@@ -2,13 +2,59 @@ import Overlay from 'ol/Overlay';
2
2
  import { VueTemplatePointState } from '../types';
3
3
  import { ErrorHandler, ErrorType } from '../utils/ErrorHandler';
4
4
  import { ValidationUtils } from '../utils/ValidationUtils';
5
- /**
6
- * 检测Vue版本
7
- * @param Vue Vue实例
8
- * @returns 是否为Vue3
9
- */
10
- function isVue3(Vue) {
11
- return !!(Vue && (Vue.version?.startsWith('3') || Vue.createApp));
5
+ // 动态导入Vue
6
+ let Vue = null;
7
+ let isVue3 = false;
8
+ // 检测Vue版本并导入
9
+ async function detectAndImportVue() {
10
+ try {
11
+ // 尝试动态导入Vue
12
+ const vueModule = await import('vue');
13
+ Vue = vueModule.default || vueModule;
14
+ // 检测Vue版本
15
+ if (Vue && (Vue.version?.startsWith('3') || Vue.createApp)) {
16
+ isVue3 = true;
17
+ }
18
+ else {
19
+ isVue3 = false;
20
+ }
21
+ }
22
+ catch (e) {
23
+ console.warn('Vue not found. Please ensure Vue is installed in your project.');
24
+ Vue = null;
25
+ }
26
+ }
27
+ // 同步版本的Vue检测(用于兼容性)
28
+ function detectVueSync() {
29
+ try {
30
+ // 尝试从全局对象获取Vue
31
+ if (typeof window !== 'undefined' && window.Vue) {
32
+ Vue = window.Vue;
33
+ isVue3 = !!(Vue.version?.startsWith('3') || Vue.createApp);
34
+ return;
35
+ }
36
+ // 如果在Node.js环境中,尝试require
37
+ if (typeof window === 'undefined') {
38
+ try {
39
+ // 使用eval来避免TypeScript编译时的require检查
40
+ const requireFunc = eval('require');
41
+ Vue = requireFunc('vue');
42
+ isVue3 = !!(Vue.version?.startsWith('3') || Vue.createApp);
43
+ }
44
+ catch (e) {
45
+ console.warn('Vue not found. Please ensure Vue is installed in your project.');
46
+ Vue = null;
47
+ }
48
+ }
49
+ }
50
+ catch (e) {
51
+ console.warn('Failed to detect Vue:', e);
52
+ }
53
+ }
54
+ // 初始化Vue导入
55
+ detectVueSync();
56
+ if (!Vue) {
57
+ detectAndImportVue();
12
58
  }
13
59
  /**
14
60
  * Vue模板点位管理类
@@ -39,12 +85,7 @@ export default class VueTemplatePoint {
39
85
  if (!template) {
40
86
  throw new Error('Vue template is required');
41
87
  }
42
- if (!options?.Vue) {
43
- throw new Error('Vue instance is required in options');
44
- }
45
88
  try {
46
- const Vue = options.Vue;
47
- const vueIsVue3 = isVue3(Vue);
48
89
  const instances = [];
49
90
  pointDataList.forEach((pointData) => {
50
91
  if (!ValidationUtils.validateLngLat(pointData.lgtd, pointData.lttd)) {
@@ -63,7 +104,7 @@ export default class VueTemplatePoint {
63
104
  positioning: options?.positioning,
64
105
  stopEvent: options?.stopEvent,
65
106
  };
66
- const instance = new VueTemplatePointInstanceImpl(this.map, pointOptions, this.errorHandler, Vue, vueIsVue3);
107
+ const instance = new VueTemplatePointInstanceImpl(this.map, pointOptions, this.errorHandler);
67
108
  instances.push(instance);
68
109
  this.vuePoints.set(instance.id, instance);
69
110
  });
@@ -123,13 +164,11 @@ export default class VueTemplatePoint {
123
164
  * @internal
124
165
  */
125
166
  class VueTemplatePointInstanceImpl {
126
- constructor(map, options, errorHandler, Vue, isVue3) {
167
+ constructor(map, options, errorHandler) {
127
168
  this.app = null;
128
169
  this.state = VueTemplatePointState.CREATED;
129
170
  this.map = map;
130
171
  this.errorHandler = errorHandler;
131
- this.Vue = Vue;
132
- this.isVue3 = isVue3;
133
172
  this.options = this.mergeDefaultOptions(options);
134
173
  this.id = this.generateUniqueId();
135
174
  this.position = [this.options.lgtd, this.options.lttd];
@@ -214,13 +253,13 @@ class VueTemplatePointInstanceImpl {
214
253
  */
215
254
  createVueApp() {
216
255
  const { Template, props } = this.options;
217
- if (!this.Vue) {
218
- throw new Error('Vue is not available. Please ensure Vue instance is provided.');
256
+ if (!Vue) {
257
+ throw new Error('Vue is not available. Please ensure Vue is installed in your project.');
219
258
  }
220
259
  try {
221
- if (this.isVue3) {
260
+ if (isVue3) {
222
261
  // Vue 3
223
- this.app = this.Vue.createApp({
262
+ this.app = Vue.createApp({
224
263
  ...Template,
225
264
  props: props || {}
226
265
  });
@@ -228,7 +267,7 @@ class VueTemplatePointInstanceImpl {
228
267
  }
229
268
  else {
230
269
  // Vue 2
231
- this.app = new this.Vue({
270
+ this.app = new Vue({
232
271
  el: this.dom,
233
272
  render: (h) => h(Template, { props: props || {} })
234
273
  });
@@ -395,7 +434,7 @@ class VueTemplatePointInstanceImpl {
395
434
  destroyVueApp() {
396
435
  if (this.app) {
397
436
  try {
398
- if (this.isVue3) {
437
+ if (isVue3) {
399
438
  // Vue 3: 使用 unmount
400
439
  this.app.unmount();
401
440
  }
package/index.d.ts CHANGED
@@ -15,4 +15,4 @@ export { ValidationUtils } from './utils/ValidationUtils';
15
15
  export type { BaseOptions, StyleOptions, TextOptions } from './types';
16
16
  export type { PointOptions, LineOptions, PolygonOptions } from './types';
17
17
  export type { OptionsType } from './types';
18
- export type { MapInitType, MapLayersOptions, HeatMapOptions, ImageLayerData, MaskLayerOptions, ColorMap, FeatureColorUpdateOptions, PointData, LineData, ClusterOptions, MeasureHandlerType, VueTemplatePointOptions, MapJSONData, Feature, AnnotationType, TiandituType, MapLayers, AnnotationLayerOptions } from './types';
18
+ export type { MapInitType, MapLayersOptions, HeatMapOptions, ImageLayerData, MaskLayerOptions, ColorMap, FeatureColorUpdateOptions, PointData, LineData, ClusterOptions, MeasureHandlerType, VueTemplatePointOptions, MapJSONData, FeatureData, AnnotationType, TiandituType, MapLayers, AnnotationLayerOptions } from './types';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "1.0.9",
4
+ "version": "1.0.11",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
@@ -29,7 +29,8 @@
29
29
  "dependencies": {
30
30
  "@turf/turf": "^7.2.0",
31
31
  "ol": "^6.15.1",
32
- "proj4": "^2.7.5"
32
+ "proj4": "^2.7.5",
33
+ "my-openlayer": "^1.0.9"
33
34
  },
34
35
  "devDependencies": {
35
36
  "@types/proj4": "^2.5.2",
package/types.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import BaseLayer from "ol/layer/Base";
2
2
  import TileLayer from "ol/layer/Tile";
3
3
  import { WMTS } from "ol/source";
4
- export interface Feature {
4
+ import View from "ol/View";
5
+ import Feature from "ol/Feature";
6
+ export interface FeatureData {
5
7
  type: string;
6
8
  properties: any;
7
9
  geometry: {
@@ -12,7 +14,7 @@ export interface Feature {
12
14
  export interface MapJSONData {
13
15
  type: string;
14
16
  name?: string;
15
- features: Feature[];
17
+ features: FeatureData[];
16
18
  }
17
19
  type LayerItem = BaseLayer | TileLayer<WMTS>;
18
20
  export interface MapInitType {
@@ -21,7 +23,7 @@ export interface MapInitType {
21
23
  };
22
24
  zoom?: number;
23
25
  center?: number[];
24
- view?: any;
26
+ view?: View;
25
27
  minZoom?: number;
26
28
  maxZoom?: number;
27
29
  extent?: number[];
@@ -81,8 +83,6 @@ export interface BaseOptions {
81
83
  mapClipData?: MapJSONData;
82
84
  /** 投影选项 */
83
85
  projectionOptOptions?: any;
84
- /** 扩展属性 */
85
- [propName: string]: any;
86
86
  }
87
87
  /**
88
88
  * 样式选项接口 - 图形样式相关配置
@@ -99,7 +99,7 @@ export interface StyleOptions {
99
99
  /** 填充颜色 */
100
100
  fillColor?: string;
101
101
  /** 填充颜色回调函数 */
102
- fillColorCallBack?: (feature: any) => string;
102
+ fillColorCallBack?: (feature: Feature) => string;
103
103
  }
104
104
  /**
105
105
  * 文本选项接口 - 文本标注相关配置
@@ -108,7 +108,7 @@ export interface TextOptions {
108
108
  /** 文本可见性 */
109
109
  textVisible?: boolean;
110
110
  /** 文本内容回调函数 */
111
- textCallBack?: (feature: any) => string;
111
+ textCallBack?: (feature: Feature) => string;
112
112
  /** 文本字体 */
113
113
  textFont?: string;
114
114
  /** 文本填充颜色 */
@@ -1,163 +0,0 @@
1
- import { Map } from 'ol';
2
- import Overlay from 'ol/Overlay';
3
- import { Coordinate } from 'ol/coordinate';
4
- import { DomPointOptions, DomPointState } from '../types';
5
- /**
6
- * DOM点位管理类
7
- * 用于在地图上添加和管理DOM元素覆盖物
8
- */
9
- export default class DomPoint {
10
- private readonly map;
11
- private readonly errorHandler;
12
- private app;
13
- private readonly anchor;
14
- private readonly dom;
15
- private readonly id;
16
- private readonly options;
17
- private state;
18
- private position;
19
- /**
20
- * 构造函数
21
- * @param map OpenLayers地图实例
22
- * @param options 配置选项
23
- * @throws 当参数无效时抛出错误
24
- */
25
- constructor(map: Map, options: DomPointOptions);
26
- /**
27
- * 验证构造函数参数
28
- * @param map 地图实例
29
- * @param options 配置选项
30
- * @private
31
- */
32
- private validateConstructorParams;
33
- /**
34
- * 合并默认配置选项
35
- * @param options 用户配置选项
36
- * @returns 合并后的配置选项
37
- * @private
38
- */
39
- private mergeDefaultOptions;
40
- /**
41
- * 生成唯一ID
42
- * @returns 唯一标识符
43
- * @private
44
- */
45
- private generateUniqueId;
46
- /**
47
- * 创建DOM元素
48
- * @returns DOM元素
49
- * @private
50
- */
51
- private createDomElement;
52
- /**
53
- * 创建Vue应用实例
54
- * @private
55
- */
56
- private createVueApp;
57
- /**
58
- * 创建覆盖层
59
- * @returns 覆盖层实例
60
- * @private
61
- */
62
- private createOverlay;
63
- /**
64
- * 错误处理
65
- * @param message 错误消息
66
- * @param error 原始错误
67
- * @param context 错误上下文
68
- * @private
69
- */
70
- private handleError;
71
- /**
72
- * 设置可见性
73
- * @param visible 是否可见
74
- * @throws 当操作失败时抛出错误
75
- */
76
- setVisible(visible: boolean): void;
77
- /**
78
- * 获取当前可见性状态
79
- * @returns 是否可见
80
- */
81
- isVisible(): boolean;
82
- /**
83
- * 更新位置
84
- * @param lgtd 新经度
85
- * @param lttd 新纬度
86
- * @throws 当操作失败时抛出错误
87
- */
88
- updatePosition(lgtd: number, lttd: number): void;
89
- /**
90
- * 获取当前位置
91
- * @returns 当前坐标位置
92
- */
93
- getPosition(): Coordinate;
94
- /**
95
- * 更新组件属性
96
- * @param newProps 新的属性对象
97
- * @throws 当操作失败时抛出错误
98
- */
99
- updateProps(newProps: Record<string, any>): void;
100
- /**
101
- * 设置CSS样式
102
- * @param styles 样式对象
103
- * @throws 当操作失败时抛出错误
104
- */
105
- setStyle(styles: Partial<CSSStyleDeclaration>): void;
106
- /**
107
- * 添加CSS类名
108
- * @param className 要添加的类名
109
- * @throws 当操作失败时抛出错误
110
- */
111
- addClass(className: string): void;
112
- /**
113
- * 移除CSS类名
114
- * @param className 要移除的类名
115
- * @throws 当操作失败时抛出错误
116
- */
117
- removeClass(className: string): void;
118
- /**
119
- * 销毁Vue应用实例
120
- * @private
121
- */
122
- private destroyVueApp;
123
- /**
124
- * 移除点位
125
- * @throws 当移除失败时抛出错误
126
- */
127
- remove(): void;
128
- /**
129
- * 获取覆盖层
130
- * @returns 覆盖层实例
131
- */
132
- getOverlay(): Overlay;
133
- /**
134
- * 获取点位ID
135
- * @returns 点位唯一标识符
136
- */
137
- getId(): string;
138
- /**
139
- * 获取DOM元素
140
- * @returns DOM元素
141
- */
142
- getDomElement(): HTMLElement;
143
- /**
144
- * 获取当前状态
145
- * @returns 当前状态
146
- */
147
- getState(): DomPointState;
148
- /**
149
- * 获取配置选项
150
- * @returns 配置选项的副本
151
- */
152
- getOptions(): Readonly<DomPointOptions>;
153
- /**
154
- * 检查是否已销毁
155
- * @returns 是否已销毁
156
- */
157
- isDestroyed(): boolean;
158
- /**
159
- * 获取地图实例
160
- * @returns 地图实例
161
- */
162
- getMap(): Map;
163
- }