my-openlayer 2.4.2 → 2.4.4

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.
@@ -0,0 +1,126 @@
1
+ # SelectHandler 要素选择类
2
+
3
+ `SelectHandler` 负责地图要素的交互选择,支持点击、悬停等多种模式,以及编程式选择。
4
+
5
+ ## 核心特性
6
+
7
+ - **独立渲染机制**:采用独立的 Select 实例渲染选中要素,确保样式完全隔离,互不干扰。
8
+ - **静态样式固化**:在选中瞬间计算并固化样式,支持基于原始样式的复杂计算(如 `feature.getStyle()`),彻底避免递归调用风险。
9
+ - **多选隔离**:支持不同要素同时应用完全不同的高亮样式。
10
+ - **自动清理**:自动管理渲染实例的生命周期,防止内存泄漏。
11
+
12
+ ## 方法
13
+
14
+ ### 交互选择
15
+
16
+ - **enableSelect(mode: SelectMode, options?: SelectOptions)**: 启用选择交互。
17
+ - `mode`: `'click'` (点击), `'hover'` (悬停), `'ctrl'` (Ctrl+点击)。
18
+ - `options`:
19
+ - `multi`: 是否允许多选。
20
+ - `layerFilter`: 图层过滤器(图层名称数组)。
21
+ - `onSelect`: 选中回调。
22
+ - `onDeselect`: 取消选中回调。
23
+ - `selectStyle`: 选中时的样式。支持 `Style` 对象、数组或函数 `(feature, resolution) => Style`。**推荐使用函数**以实现基于原始样式的动态高亮。
24
+
25
+ - **disableSelect()**: 禁用选择交互(停止响应用户操作,并清理交互式高亮)。
26
+ - **isSelectEnabled()**: 检查是否已启用。
27
+
28
+ ### 编程式选择
29
+
30
+ - **selectByIds(featureIds: string[], options?)**: 根据 ID 选中要素。
31
+ - **selectByProperty(propertyName: string, propertyValue: any, options?)**: 根据属性值选中要素。
32
+ - **clearSelection()**: 清除当前所有选中状态(包括交互式和编程式)。
33
+ - **getSelectedFeatures()**: 获取当前选中的要素列表(仅包含交互式选择的要素)。
34
+
35
+ ## 使用示例
36
+
37
+ ### 1. 基础用法
38
+
39
+ ```typescript
40
+ const selectHandler = map.getSelectHandler();
41
+
42
+ // 启用点击选择
43
+ selectHandler.enableSelect('click', {
44
+ multi: false, // 单选
45
+ layerFilter: ['target-layer'], // 仅选择特定图层的要素
46
+ onSelect: (event) => {
47
+ const feature = event.selected[0];
48
+ console.log('选中要素:', feature.getProperties());
49
+ },
50
+ onDeselect: (event) => {
51
+ console.log('取消选中:', event.deselected);
52
+ },
53
+ // 自定义选中样式(简单对象)
54
+ selectStyle: new Style({
55
+ image: new Circle({
56
+ radius: 8,
57
+ fill: new Fill({ color: 'red' }),
58
+ stroke: new Stroke({ color: 'white', width: 2 })
59
+ })
60
+ })
61
+ });
62
+ ```
63
+
64
+ ### 2. 高级用法:基于原始样式的动态高亮
65
+
66
+ 利用函数式 `selectStyle`,可以在选中时动态获取要素的原始样式,并在此基础上修改(例如只改边框颜色,保留填充)。由于采用了**样式固化**机制,这种写法是安全的。
67
+
68
+ ```typescript
69
+ selectHandler.enableSelect('click', {
70
+ selectStyle: (feature, resolution) => {
71
+ // 获取要素的原始样式
72
+ // 注意:feature.getStyle() 可能返回 Style 对象、数组或函数,需要自行处理归一化
73
+ const originStyleLike = feature.getStyle();
74
+ let originStyle = originStyleLike;
75
+
76
+ // 如果原始样式是函数,执行它获取 Style 对象
77
+ if (typeof originStyleLike === 'function') {
78
+ originStyle = originStyleLike(feature, resolution);
79
+ }
80
+
81
+ // 归一化为数组
82
+ const styles = Array.isArray(originStyle) ? originStyle : [originStyle];
83
+
84
+ // 克隆并修改样式(例如:将边框改为青色)
85
+ return styles.map(s => {
86
+ const clone = s.clone();
87
+ const stroke = clone.getStroke();
88
+ if (stroke) {
89
+ stroke.setColor('#00FFFF');
90
+ stroke.setWidth(3);
91
+ } else {
92
+ // 如果没有边框,添加一个
93
+ clone.setStroke(new Stroke({ color: '#00FFFF', width: 3 }));
94
+ }
95
+ return clone;
96
+ });
97
+ }
98
+ });
99
+ ```
100
+
101
+ ### 3. 编程式选择(带独立样式)
102
+
103
+ 编程式选择支持传入特定的样式,该样式将独立于主交互样式生效,且支持多要素使用不同样式。
104
+
105
+ ```typescript
106
+ // 选中 ID 为 'feature-1' 的要素,并用红色高亮
107
+ selectHandler.selectByIds(['feature-1'], {
108
+ selectStyle: new Style({ stroke: new Stroke({ color: 'red', width: 4 }) }),
109
+ fitView: true // 自动定位
110
+ });
111
+
112
+ // 同时选中 ID 为 'feature-2' 的要素,但用蓝色高亮(互不冲突)
113
+ selectHandler.selectByIds(['feature-2'], {
114
+ selectStyle: new Style({ stroke: new Stroke({ color: 'blue', width: 4 }) })
115
+ });
116
+ ```
117
+
118
+ ### 4. 禁用与清理
119
+
120
+ ```typescript
121
+ // 禁用用户交互(停止响应点击,但保留编程式高亮)
122
+ selectHandler.disableSelect();
123
+
124
+ // 清除所有选择(包括交互式和编程式的高亮)
125
+ selectHandler.clearSelection();
126
+ ```
@@ -0,0 +1,38 @@
1
+ # ValidationUtils 验证工具类
2
+
3
+ `ValidationUtils` 提供了一系列静态方法用于参数校验。
4
+
5
+ ## 方法
6
+
7
+ - **isValidCoordinate(lng, lat)**: 检查坐标是否有效(数字且在范围内)。
8
+ - **validateLngLat(lng, lat)**: 简化的经纬度检查。
9
+ - **validateLayerName(name)**: 验证图层名称非空。
10
+ - **validateGeoJSONData(data)**: 验证 GeoJSON 数据格式。
11
+ - **isValidColor(color)**: 验证颜色字符串格式。
12
+ - **validateVueParams(...)**: 验证 Vue 组件点位所需的参数。
13
+
14
+ ## 使用示例
15
+
16
+ ```typescript
17
+ import { ValidationUtils } from 'my-openlayer';
18
+
19
+ // 1. 验证坐标
20
+ const lng = 120.1;
21
+ const lat = 30.2;
22
+
23
+ if (ValidationUtils.isValidCoordinate(lng, lat)) {
24
+ console.log('坐标有效');
25
+ } else {
26
+ console.error('坐标无效');
27
+ }
28
+
29
+ // 2. 验证颜色
30
+ if (ValidationUtils.isValidColor('#ff0000')) {
31
+ // ...
32
+ }
33
+
34
+ // 3. 验证 GeoJSON
35
+ if (ValidationUtils.validateGeoJSONData(jsonData)) {
36
+ // 安全地加载数据
37
+ }
38
+ ```
@@ -0,0 +1,85 @@
1
+ # VueTemplatePoint Vue 组件点位类
2
+
3
+ `VueTemplatePoint` 允许将 Vue 组件直接渲染为地图上的点位覆盖物(Overlay),支持 Vue 2 和 Vue 3。
4
+
5
+ ## 方法
6
+
7
+ - **addVueTemplatePoint(pointDataList: any[], template: any, options?)**: 添加 Vue 组件点位。
8
+ - `pointDataList`: 点位数据列表。
9
+ - `template`: Vue 组件对象。
10
+ - `options`:
11
+ - `positioning`: 覆盖物定位点(如 `'center-center'`)。
12
+ - `stopEvent`: 是否阻止事件冒泡。
13
+
14
+ ### 返回值
15
+
16
+ 该方法返回一个控制对象,包含以下方法:
17
+
18
+ - **setVisible(visible: boolean)**: 设置所有点位的可见性。
19
+ - **setOneVisibleByProp(propName: string, propValue: any, visible: boolean)**: 根据属性值控制特定点位的可见性。
20
+ - **remove()**: 移除所有点位并销毁组件实例。
21
+ - **getPoints()**: 获取底层点位实例列表。
22
+
23
+ ## 使用示例
24
+
25
+ ### Vue 3 示例
26
+
27
+ ```typescript
28
+ import { MyOl } from 'my-openlayer';
29
+ import MyPopup from './MyPopup.vue'; // 你的 Vue 组件
30
+
31
+ const pointData = [
32
+ { lgtd: 120.1, lttd: 30.2, title: '位置1', status: 'normal' },
33
+ { lgtd: 120.2, lttd: 30.3, title: '位置2', status: 'warning' }
34
+ ];
35
+
36
+ // 添加组件点位
37
+ const vuePoints = map.getPoint().addVueTemplatePoint(
38
+ pointData,
39
+ MyPopup,
40
+ {
41
+ positioning: 'bottom-center',
42
+ stopEvent: true // 允许点击组件交互
43
+ }
44
+ );
45
+
46
+ // 控制显示隐藏
47
+ vuePoints.setVisible(false);
48
+
49
+ // 根据属性控制
50
+ vuePoints.setOneVisibleByProp('status', 'warning', true);
51
+
52
+ // 移除
53
+ // vuePoints.remove();
54
+ ```
55
+
56
+ ### Vue 组件编写规范 (MyPopup.vue)
57
+
58
+ 组件会接收 `pointData` 作为 props。
59
+
60
+ ```vue
61
+ <template>
62
+ <div class="popup">
63
+ <h3>{{ pointData.title }}</h3>
64
+ <p>状态: {{ pointData.status }}</p>
65
+ </div>
66
+ </template>
67
+
68
+ <script setup>
69
+ defineProps({
70
+ pointData: {
71
+ type: Object,
72
+ required: true
73
+ }
74
+ });
75
+ </script>
76
+
77
+ <style scoped>
78
+ .popup {
79
+ background: white;
80
+ padding: 10px;
81
+ border-radius: 4px;
82
+ box-shadow: 0 2px 10px rgba(0,0,0,0.2);
83
+ }
84
+ </style>
85
+ ```
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "2.4.2",
4
+ "version": "2.4.4",
5
5
  "type": "module",
6
+ "license": "MIT",
6
7
  "main": "index.js",
7
8
  "types": "index.d.ts",
8
9
  "keywords": [
@@ -12,10 +13,21 @@
12
13
  "map",
13
14
  "vue"
14
15
  ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/cuteyuchen/my-openlayer.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/cuteyuchen/my-openlayer/issues"
22
+ },
23
+ "homepage": "https://github.com/cuteyuchen/my-openlayer#readme",
15
24
  "files": [
16
25
  "**/*",
17
26
  "LICENSE",
18
27
  "README.md",
28
+ "CHANGELOG.md",
29
+ "AI_CONTEXT.md",
30
+ "docs",
19
31
  "package.json"
20
32
  ],
21
33
  "scripts": {