my-openlayer 2.1.4 → 2.1.5

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 (3) hide show
  1. package/README.md +138 -100
  2. package/core/Line.js +2 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -8,6 +8,60 @@ my-openlayer 是一个基于 [OpenLayers](https://openlayers.org/) 的现代地
8
8
 
9
9
  ---
10
10
 
11
+ ## 项目概述
12
+
13
+ - 项目名称:`my-openlayer`
14
+ - 用途:基于 OpenLayers 的 TypeScript 地图组件库,提供点/线/面要素、底图切换、热力图、事件与配置管理等能力,面向 Web GIS 开发者
15
+ - 目标用户:Web GIS 开发者、可视化工程师、前端开发者
16
+ - 状态徽章:
17
+ - [![npm](https://img.shields.io/npm/v/my-openlayer.svg)](https://www.npmjs.com/package/my-openlayer)
18
+ - ![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)
19
+ - ![License](https://img.shields.io/badge/License-MIT-yellow.svg)
20
+ - ![Build](https://img.shields.io/badge/build-local%20(vite)-blue.svg)
21
+ - ![Coverage](https://img.shields.io/badge/coverage-not%20configured-lightgrey.svg)
22
+ - ![Node](https://img.shields.io/badge/node-%3E%3D18.0.0-6DA55F.svg)
23
+ - ![ol](https://img.shields.io/badge/ol-^10.6.1-blue.svg)
24
+
25
+ ## 环境要求
26
+
27
+ - Node.js >= 18(Vite 5 要求)
28
+ - 包管理器:npm / yarn / pnpm
29
+ - 运行时依赖:`ol@^10.6.1`、`proj4@^2.7.5`、`@turf/turf@^7.2.0`
30
+ - 开发依赖:`vite@^5.4.10`、`@vitejs/plugin-vue@^5.0.4`、`typescript@~5.6.2`、`vue-tsc@^2.0.6`
31
+
32
+ ## 安装指南
33
+
34
+ - 安装库:`npm i my-openlayer` 或 `yarn add my-openlayer` 或 `pnpm add my-openlayer`
35
+ - 示例与开发:
36
+ - 安装依赖:`npm i`
37
+ - 开发示例:`npm run dev`
38
+ - 构建库:`npm run build`
39
+ - 配置文件:
40
+ - TypeScript:`tsconfig.json`(声明输出 `dist/`)
41
+ - Vite:`vite.config.ts`(插件:@vitejs/plugin-vue、vite-plugin-libcss)
42
+ - 环境变量:`.env` 设置 `VITE_TIANDITU_TOKEN=your_token`
43
+
44
+ ## 使用说明
45
+
46
+ - 主要功能的使用参考下文「详细用法」与「API 文档与示例」
47
+ - 示例代码与截图:运行 `npm run dev` 后查看 `examples/App.vue` 渲染效果(截图略)
48
+ - FAQ:见下文「常见问题」
49
+
50
+ ## 开发指南
51
+
52
+ - 项目结构:
53
+ - `src/` 核心库源码;`src/core` 包含 BaseLayers/Tools/Point/Line/Polygon/Select 等模块
54
+ - `examples/` Vue3 + Vite 示例;入口 `index.html` → `examples/main.ts`
55
+ - `dist/` 构建输出;`temp-publish/` 发布前整理产物
56
+ - 配置与脚本:`package.json`、`tsconfig.json`、`vite.config.ts`、`scripts/prepare-publish.cjs`
57
+ - 开发环境搭建:Node 18+,`npm i` 安装依赖,`npm run dev` 启动示例,`.env` 设置 `VITE_TIANDITU_TOKEN`
58
+ - 代码贡献规范:分支 `feature/xxx`、`fix/xxx`;`type(scope): subject` 提交信息;TypeScript 严格类型,2 空格缩进
59
+
60
+ ## 许可证信息
61
+
62
+ - 许可证:MIT(见 `LICENSE`)
63
+ - 版权声明:Copyright (c) 2025 cuteyuchen
64
+
11
65
  ## 目录
12
66
 
13
67
  - [功能亮点](#功能亮点)
@@ -86,13 +140,13 @@ pnpm add my-openlayer
86
140
  使用天地图服务需要配置API Token:
87
141
 
88
142
  ```bash
89
- # 在项目根目录创建 .env 文件
90
- VUE_APP_TIANDITU_TOKEN=your_tianditu_token_here
143
+ # 在项目根目录创建 .env 文件(Vite)
144
+ VITE_TIANDITU_TOKEN=your_tianditu_token_here
91
145
 
92
- # 其他可选配置
93
- VUE_APP_MAP_CENTER_LON=119.81
94
- VUE_APP_MAP_CENTER_LAT=29.969
95
- VUE_APP_MAP_ZOOM=10
146
+ # 其他可选配置(示例)
147
+ VITE_MAP_CENTER_LON=119.81
148
+ VITE_MAP_CENTER_LAT=29.969
149
+ VITE_MAP_ZOOM=10
96
150
  ```
97
151
 
98
152
  ### 配置选项
@@ -131,7 +185,7 @@ const mapConfig: MapInitType = {
131
185
  zoom: 10,
132
186
  minZoom: 8,
133
187
  maxZoom: 20,
134
- token: process.env.VUE_APP_TIANDITU_TOKEN, // 从环境变量获取天地图token
188
+ token: import.meta.env.VITE_TIANDITU_TOKEN, // 从环境变量获取天地图 token(Vite)
135
189
  annotation: true,
136
190
  layers: {
137
191
  vec_c: [],
@@ -150,6 +204,29 @@ const map = new MyOl('map-container', mapConfig);
150
204
  <div id="map-container" style="width: 100vw; height: 100vh;"></div>
151
205
  ```
152
206
 
207
+ ### 模块获取与使用模式
208
+
209
+ ```typescript
210
+ // 1) 链式调用(推荐):通过 MyOl 获取模块并直接使用
211
+ const myOl = new MyOl('map-container', mapConfig);
212
+ myOl.getPoint().addPoint(pointData, { layerName: 'p', textKey: 'name', img: 'marker.png' });
213
+ myOl.getLine().addLine(lineGeoJSON, { layerName: 'line', strokeColor: '#037AFF', strokeWidth: 3 });
214
+ myOl.getPolygon().addPolygon(zoneGeoJSON, { layerName: 'zone', textKey: 'name', textVisible: true });
215
+
216
+ // 2) 独立实例化:直接传入原生 ol.Map 创建模块实例
217
+ const olMap = myOl.map; // MyOl 暴露原生 Map 实例
218
+ import { Point, Line, Polygon } from 'my-openlayer';
219
+
220
+ const point = new Point(olMap);
221
+ point.addPoint(pointData, { layerName: 'p', textKey: 'name', img: 'marker.png' });
222
+
223
+ const line = new Line(olMap);
224
+ line.addLine(lineGeoJSON, { layerName: 'line' });
225
+
226
+ const polygon = new Polygon(olMap);
227
+ polygon.addPolygon(zoneGeoJSON, { layerName: 'zone', textKey: 'name' });
228
+ ```
229
+
153
230
  ---
154
231
 
155
232
  ## 详细用法
@@ -187,9 +264,8 @@ const pointData: PointData[] = [
187
264
  // 添加普通点位
188
265
  const pointOptions: PointOptions = {
189
266
  layerName: 'test-point',
190
- nameKey: 'name',
267
+ textKey: 'name',
191
268
  img: 'marker.png',
192
- hasImg: true,
193
269
  scale: 1.2,
194
270
  textFont: '12px sans-serif',
195
271
  textFillColor: '#FFF',
@@ -208,7 +284,7 @@ const clusterData: PointData[] = [
208
284
  ];
209
285
  const clusterOptions: ClusterOptions = {
210
286
  layerName: 'cluster-point',
211
- nameKey: 'name',
287
+ textKey: 'name',
212
288
  img: 'cluster.png',
213
289
  distance: 50,
214
290
  minDistance: 20,
@@ -217,10 +293,10 @@ const clusterOptions: ClusterOptions = {
217
293
  point.addClusterPoint(clusterData, clusterOptions);
218
294
 
219
295
  // 添加 Vue 组件点位
220
- const domPoints = point.setDomPointVue(
296
+ const domPoints = point.addVueTemplatePoint(
221
297
  [{ lgtd: 119.81, lttd: 29.969 }],
222
298
  YourVueComponent,
223
- Vue
299
+ { positioning: 'center-center' }
224
300
  );
225
301
 
226
302
  // 控制组件点位显隐
@@ -323,7 +399,7 @@ const zoneOptions: PolygonOptions = {
323
399
  strokeColor: '#037AFF',
324
400
  strokeWidth: 2,
325
401
  textVisible: true,
326
- nameKey: 'name',
402
+ textKey: 'name',
327
403
  textFont: '14px Calibri,sans-serif',
328
404
  textFillColor: '#FFF',
329
405
  textStrokeColor: '#409EFF',
@@ -334,13 +410,13 @@ polygon.addPolygon(zoneGeoJSON, zoneOptions);
334
410
 
335
411
  // 更新面颜色
336
412
  const colorUpdateOptions: FeatureColorUpdateOptions = {
337
- nameKey: 'name'
413
+ textKey: 'name'
338
414
  };
339
415
  polygon.updateFeatureColor('zone', { 'A区': 'rgba(255,0,0,0.6)' }, colorUpdateOptions);
340
416
 
341
417
  // 添加图片图层
342
- const extent = [119.8, 29.96, 119.82, 29.98]; // [minx, miny, maxx, maxy]
343
- polygon.addImage('imgLayer', 'img.png', extent, { zIndex: 10 });
418
+ const extent = [119.8, 29.96, 119.82, 29.98]; // [minx, miny, maxy]
419
+ polygon.addImageLayer({ img: 'img.png', extent }, { layerName: 'imgLayer', zIndex: 10 });
344
420
 
345
421
  // 添加热力图
346
422
  const heatData: PointData[] = [
@@ -410,10 +486,10 @@ try {
410
486
  ### 事件管理系统
411
487
 
412
488
  ```typescript
413
- import { MyOl, EventManager, MapEventType, EventCallback, MapEventData } from 'my-openlayer';
489
+ import { MapEventType, EventCallback, MapEventData } from 'my-openlayer';
414
490
 
415
- // 创建事件管理器
416
- const eventManager = new EventManager(map.map); // 传入原生 ol.Map
491
+ // 获取事件管理器
492
+ const eventManager = map.getEventManager();
417
493
 
418
494
  // 监听点击事件
419
495
  const clickCallback: EventCallback = (eventData: MapEventData) => {
@@ -517,12 +593,11 @@ tools.removeLayer('layerName');
517
593
  // 设置图层可见性
518
594
  tools.setLayerVisible('layerName', true);
519
595
 
520
- // 事件监听
521
- map.mapOnEvent('click', (feature, event) => {
522
- console.log('点击要素:', feature);
596
+ // 事件监听(使用 EventManager)
597
+ const em = map.getEventManager();
598
+ em.on('click', (eventData) => {
599
+ console.log('点击要素:', eventData.feature);
523
600
  });
524
-
525
- // 支持事件类型:click、moveend、hover
526
601
  ```
527
602
 
528
603
  ### 测量工具
@@ -610,11 +685,7 @@ new MyOl(id: string, options: MapInitType)
610
685
  const configManager = map.getConfigManager();
611
686
  ```
612
687
 
613
- - **getMap()**
614
- > 获取原生 OpenLayers 地图实例。
615
- ```javascript
616
- const olMap = map.getMap();
617
- ```
688
+ - 不直接暴露原生地图实例;推荐通过模块实例(如 `MapTools`)与 `EventManager` 完成操作
618
689
 
619
690
  - **resetPosition(duration?: number)**
620
691
  > 重置地图位置。
@@ -628,13 +699,9 @@ new MyOl(id: string, options: MapInitType)
628
699
  map.locationAction(119.81, 29.969, 15, 1000);
629
700
  ```
630
701
 
631
- - **mapOnEvent(eventType: string, callback: Function, clickType?: string)**
632
- > 地图事件监听。
633
- ```javascript
634
- map.mapOnEvent('click', (feature, event) => {
635
- console.log('点击要素:', feature);
636
- });
637
- ```
702
+ // 事件监听:使用 EventManager 统一管理
703
+ // const eventManager = map.getEventManager();
704
+ // eventManager.on('click', (eventData) => { ... })
638
705
 
639
706
  ---
640
707
 
@@ -679,20 +746,19 @@ new MyOl(id: string, options: MapInitType)
679
746
 
680
747
  ### Point
681
748
 
682
- - **addPoint(pointData: PointData[], options: OptionsType)**
683
- > 添加普通点位到指定图层,支持自定义样式和图标。
749
+ - **addPoint(pointData: PointData[], options: PointOptions)**
750
+ > 添加普通点位到指定图层,支持文本与图标样式。
684
751
  ```javascript
685
752
  point.addPoint([
686
753
  { lgtd: 119.81, lttd: 29.969, name: '测试点位' }
687
754
  ], {
688
755
  layerName: 'test-point',
689
- nameKey: 'name',
690
- img: 'marker.png',
691
- hasImg: true
756
+ textKey: 'name',
757
+ img: 'marker.png'
692
758
  });
693
759
  ```
694
760
 
695
- - **addClusterPoint(pointData: PointData[], options: OptionsType)**
761
+ - **addClusterPoint(pointData: PointData[], options: ClusterOptions)**
696
762
  > 添加聚合点位,自动根据缩放级别聚合显示。
697
763
  ```javascript
698
764
  point.addClusterPoint([
@@ -700,37 +766,27 @@ new MyOl(id: string, options: MapInitType)
700
766
  { lgtd: 119.82, lttd: 29.97, name: 'B' }
701
767
  ], {
702
768
  layerName: 'cluster-point',
703
- nameKey: 'name',
769
+ textKey: 'name',
704
770
  img: 'cluster.png',
705
771
  zIndex: 4
706
772
  });
707
773
  ```
708
774
 
709
- - **addTwinklePoint(pointData: PointData[], options: OptionsType)**
710
- > 添加闪烁点位,具有动画效果。
711
- ```javascript
712
- point.addTwinklePoint([
713
- { lgtd: 119.81, lttd: 29.969, name: '闪烁点位' }
714
- ], {
715
- layerName: 'twinkle-point',
716
- nameKey: 'name',
717
- img: 'twinkle.png',
718
- hasImg: true
719
- });
720
- ```
721
-
722
- - **setDomPointVue(pointInfoList: any[], template: any, Vue: any)**
775
+ - **addVueTemplatePoint(pointDataList: PointData[], template: any, options?)**
723
776
  > 添加 Vue 组件点位。
724
777
  ```javascript
725
- const domPoints = point.setDomPointVue(
778
+ const domPoints = point.addVueTemplatePoint(
726
779
  [{ lgtd: 119.81, lttd: 29.969 }],
727
780
  YourVueComponent,
728
- Vue
781
+ { positioning: 'center-center' }
729
782
  );
730
783
  domPoints.setVisible(true);
731
784
  domPoints.remove();
732
785
  ```
733
786
 
787
+ - **addTwinkleLayer(twinkleList: any[], className?: string, key: string, callback?)**
788
+ > 添加闪烁点覆盖物。
789
+
734
790
  - **locationAction(lgtd: number, lttd: number, zoom?: number, duration?: number)**
735
791
  > 地图定位。
736
792
  ```javascript
@@ -796,7 +852,7 @@ new MyOl(id: string, options: MapInitType)
796
852
  strokeColor: '#037AFF',
797
853
  strokeWidth: 2,
798
854
  textVisible: true,
799
- nameKey: 'name',
855
+ textKey: 'name',
800
856
  textFont: '14px Calibri,sans-serif',
801
857
  textFillColor: '#FFF',
802
858
  textStrokeColor: '#409EFF',
@@ -804,16 +860,16 @@ new MyOl(id: string, options: MapInitType)
804
860
  });
805
861
  ```
806
862
 
807
- - **updateFeatureColor(layerName: string, colorObj?: { [propName: string]: string }, options?: OptionsType)**
863
+ - **updateFeatureColor(layerName: string, colorObj?: { [propName: string]: string }, options?: FeatureColorUpdateOptions)**
808
864
  > 更新面颜色。
809
865
  ```javascript
810
- polygon.updateFeatureColor('zone', { 'A区': 'rgba(255,0,0,0.6)' }, { nameKey: 'name' });
866
+ polygon.updateFeatureColor('zone', { 'A区': 'rgba(255,0,0,0.6)' }, { textKey: 'name' });
811
867
  ```
812
868
 
813
- - **addImage(layerName: string, img?: string, extent?: number[], options?: OptionsType)**
869
+ - **addImageLayer(imageData: ImageLayerData, options?: PolygonOptions)**
814
870
  > 添加图片图层。
815
871
  ```javascript
816
- polygon.addImage('imgLayer', 'img.png', [minx, miny, maxx, maxy], { zIndex: 10 });
872
+ polygon.addImageLayer({ img: 'img.png', extent: [minx, miny, maxx, maxy] }, { layerName: 'imgLayer', zIndex: 10 });
817
873
  ```
818
874
 
819
875
  - **addHeatmap(layerName: string, pointData: PointData[], options: HeatMapOptions)**
@@ -857,13 +913,9 @@ new MyOl(id: string, options: MapInitType)
857
913
  tools.setLayerVisible('myLayer', true);
858
914
  ```
859
915
 
860
- - **mapOnEvent(eventType: string, callback: Function, clickType?: string)**
861
- > 地图事件监听。
862
- ```javascript
863
- tools.mapOnEvent('click', (feature, event) => {
864
- console.log('点击要素:', feature);
865
- });
866
- ```
916
+ // 事件监听请使用 EventManager:
917
+ // const em = map.getEventManager();
918
+ // em.on('click', (eventData) => { console.log(eventData.feature) })
867
919
 
868
920
  - **static setMapClip(baseLayer: any, data: MapJSONData)**
869
921
  > 设置地图裁剪。
@@ -1036,14 +1088,12 @@ interface TextOptions {
1036
1088
 
1037
1089
  // 点位选项 - 点位图层专用配置
1038
1090
  interface PointOptions extends BaseOptions, StyleOptions, TextOptions {
1039
- /** 名称字段键 */
1040
- nameKey?: string;
1091
+ /** 文本字段键 */
1092
+ textKey?: string;
1041
1093
  /** 图标图片 */
1042
1094
  img?: string;
1043
1095
  /** 图标缩放比例 */
1044
1096
  scale?: number;
1045
- /** 是否有图标 */
1046
- hasImg?: boolean;
1047
1097
  /** 图标颜色 */
1048
1098
  iconColor?: string;
1049
1099
  }
@@ -1056,8 +1106,8 @@ interface LineOptions extends BaseOptions, StyleOptions, TextOptions {
1056
1106
 
1057
1107
  // 多边形选项 - 多边形图层专用配置
1058
1108
  interface PolygonOptions extends BaseOptions, StyleOptions, TextOptions {
1059
- /** 名称字段键 */
1060
- nameKey?: string;
1109
+ /** 文本字段键 */
1110
+ textKey?: string;
1061
1111
  /** 是否为蒙版 */
1062
1112
  mask?: boolean;
1063
1113
  }
@@ -1135,10 +1185,9 @@ class MyOpenLayersError extends Error {
1135
1185
  * @deprecated 请使用具体的选项接口:PointOptions, LineOptions, PolygonOptions
1136
1186
  */
1137
1187
  type OptionsType = BaseOptions & StyleOptions & TextOptions & {
1138
- nameKey?: string;
1188
+ textKey?: string;
1139
1189
  img?: string;
1140
1190
  scale?: number;
1141
- hasImg?: boolean;
1142
1191
  iconColor?: string;
1143
1192
  type?: string;
1144
1193
  mask?: boolean;
@@ -1180,25 +1229,20 @@ const options: PointOptions = {
1180
1229
 
1181
1230
  ### 运行时依赖
1182
1231
 
1183
- - **ol**: ^7.5.2 - OpenLayers 地图库
1184
- - **proj4**: ^2.9.2 - 坐标系转换库
1185
- - **@turf/turf**: ^6.5.0 - 地理空间分析库
1232
+ - **ol**: ^10.6.1 - OpenLayers 地图库
1233
+ - **proj4**: ^2.7.5 - 坐标系转换库
1234
+ - **@turf/turf**: ^7.2.0 - 地理空间分析库
1186
1235
 
1187
1236
  ### 开发依赖
1188
1237
 
1189
- - **@types/ol**: ^6.5.3 - OpenLayers TypeScript 类型定义
1190
- - **typescript**: ^5.0.0 - TypeScript 编译器
1191
- - **vite**: ^4.4.5 - 构建工具
1192
- - **@vitejs/plugin-vue**: ^4.2.3 - Vue 插件支持
1193
- - **vue-tsc**: ^1.8.5 - Vue TypeScript 编译器
1238
+ - **typescript**: ~5.6.2 - TypeScript 编译器
1239
+ - **vite**: ^5.4.10 - 构建工具
1240
+ - **@vitejs/plugin-vue**: ^5.0.4 - Vue 插件支持
1241
+ - **vue-tsc**: ^2.0.6 - Vue 类型检查
1194
1242
 
1195
1243
  ### 对等依赖
1196
1244
 
1197
- - **vue**: ^2.6.0 || ^3.0.0 - Vue.js 框架(可选,用于 Vue 组件支持)
1198
- - **element-ui**: ^2.15.0 - Element UI 组件库(Vue 2)
1199
- - **element-plus**: ^2.0.0 - Element Plus 组件库(Vue 3)
1200
-
1201
- > **注意**:本库与 OpenLayers 6.15.1 完全兼容,建议使用相同版本以获得最佳体验。
1245
+ - 无强制对等依赖;推荐 `ol` 与库版本保持一致(^10.6.1)。
1202
1246
 
1203
1247
  ---
1204
1248
 
@@ -1259,18 +1303,12 @@ const options: PointOptions = {
1259
1303
 
1260
1304
  **Q: 如何监听地图事件?**
1261
1305
 
1262
- A: 使用 `EventManager` 或 `mapOnEvent` 方法:
1306
+ A: 使用 `EventManager`:
1263
1307
  ```typescript
1264
- // 使用 EventManager
1265
- const eventManager = new EventManager(map.map);
1308
+ const eventManager = map.getEventManager();
1266
1309
  eventManager.on('click', (eventData) => {
1267
1310
  console.log('点击位置:', eventData.coordinate);
1268
1311
  });
1269
-
1270
- // 使用 mapOnEvent
1271
- map.mapOnEvent('click', (feature, event) => {
1272
- console.log('点击要素:', feature);
1273
- });
1274
1312
  ```
1275
1313
 
1276
1314
  **Q: 如何处理错误?**
package/core/Line.js CHANGED
@@ -64,7 +64,7 @@ export default class Line {
64
64
  style: (feature) => {
65
65
  if (feature instanceof Feature) {
66
66
  feature.set('type', mergedOptions.type);
67
- feature.set('layerName', mergedOptions.type);
67
+ feature.set('layerName', mergedOptions.layerName);
68
68
  }
69
69
  // 如果传入了自定义样式,直接使用
70
70
  if (mergedOptions.style) {
@@ -119,7 +119,7 @@ export default class Line {
119
119
  style: (feature) => {
120
120
  if (feature instanceof Feature) {
121
121
  feature.set('type', mergedOptions.type);
122
- feature.set('layerName', mergedOptions.type);
122
+ feature.set('layerName', mergedOptions.layerName);
123
123
  }
124
124
  // 如果传入了自定义样式,直接使用
125
125
  if (mergedOptions.style) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "2.1.4",
4
+ "version": "2.1.5",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",