my-openlayer 2.3.2 → 2.4.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.
- package/MyOl.d.ts +1 -1
- package/MyOl.js +6 -3
- package/core/ConfigManager.d.ts +0 -3
- package/core/ConfigManager.js +0 -3
- package/core/Polygon.d.ts +4 -4
- package/core/Polygon.js +78 -51
- package/package.json +1 -1
- package/types.d.ts +4 -0
package/MyOl.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export default class MyOl {
|
|
|
33
33
|
* @param id 地图容器 DOM 元素 ID
|
|
34
34
|
* @param options 地图初始化配置
|
|
35
35
|
*/
|
|
36
|
-
constructor(id: string, options?: Partial<MapInitType>);
|
|
36
|
+
constructor(id: string | HTMLElement, options?: Partial<MapInitType>);
|
|
37
37
|
/**
|
|
38
38
|
* 验证构造函数参数
|
|
39
39
|
* @private
|
package/MyOl.js
CHANGED
|
@@ -69,16 +69,19 @@ class MyOl {
|
|
|
69
69
|
* @private
|
|
70
70
|
*/
|
|
71
71
|
validateConstructorParams(id, options) {
|
|
72
|
-
if (!id
|
|
72
|
+
if (!id) {
|
|
73
|
+
throw new Error('地图容器 ID 或 HTMLElement 不能为空');
|
|
74
|
+
}
|
|
75
|
+
if (typeof id === 'string' && id.trim() === '') {
|
|
73
76
|
throw new Error('地图容器 ID 必须是非空字符串');
|
|
74
77
|
}
|
|
75
78
|
if (!options || typeof options !== 'object') {
|
|
76
79
|
throw new Error('地图配置选项不能为空');
|
|
77
80
|
}
|
|
78
81
|
// 检查 DOM 元素是否存在
|
|
79
|
-
const element = document.getElementById(id);
|
|
82
|
+
const element = typeof id === 'string' ? document.getElementById(id) : id;
|
|
80
83
|
if (!element) {
|
|
81
|
-
throw new Error(`找不到 ID 为 '${id}' 的 DOM 元素`);
|
|
84
|
+
throw new Error(typeof id === 'string' ? `找不到 ID 为 '${id}' 的 DOM 元素` : '提供的 DOM 元素无效');
|
|
82
85
|
}
|
|
83
86
|
}
|
|
84
87
|
/**
|
package/core/ConfigManager.d.ts
CHANGED
|
@@ -44,9 +44,6 @@ export declare class ConfigManager {
|
|
|
44
44
|
static readonly DEFAULT_POLYGON_OPTIONS: {
|
|
45
45
|
zIndex: number;
|
|
46
46
|
visible: boolean;
|
|
47
|
-
strokeColor: string;
|
|
48
|
-
strokeWidth: number;
|
|
49
|
-
fillColor: string;
|
|
50
47
|
textFont: string;
|
|
51
48
|
textFillColor: string;
|
|
52
49
|
textStrokeColor: string;
|
package/core/ConfigManager.js
CHANGED
|
@@ -88,9 +88,6 @@ ConfigManager.DEFAULT_LINE_OPTIONS = {
|
|
|
88
88
|
ConfigManager.DEFAULT_POLYGON_OPTIONS = {
|
|
89
89
|
zIndex: 11,
|
|
90
90
|
visible: true,
|
|
91
|
-
strokeColor: '#EBEEF5',
|
|
92
|
-
strokeWidth: 2,
|
|
93
|
-
fillColor: 'rgba(255, 255, 255, 0)',
|
|
94
91
|
textFont: '14px Calibri,sans-serif',
|
|
95
92
|
textFillColor: '#FFF',
|
|
96
93
|
textStrokeColor: '#409EFF',
|
package/core/Polygon.d.ts
CHANGED
|
@@ -50,11 +50,11 @@ export default class Polygon {
|
|
|
50
50
|
*/
|
|
51
51
|
addPolygonByUrl(url: string, options?: PolygonOptions): VectorLayer<VectorSource>;
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
54
|
-
* @param
|
|
55
|
-
* @
|
|
53
|
+
* 创建样式或样式函数
|
|
54
|
+
* @param options 配置选项
|
|
55
|
+
* @returns 样式或样式函数
|
|
56
56
|
*/
|
|
57
|
-
private
|
|
57
|
+
private createStyle;
|
|
58
58
|
/**
|
|
59
59
|
* 获取要素文本
|
|
60
60
|
* @param feature 要素对象
|
package/core/Polygon.js
CHANGED
|
@@ -92,10 +92,9 @@ export default class Polygon {
|
|
|
92
92
|
layerName: mergedOptions.layerName
|
|
93
93
|
},
|
|
94
94
|
source: new VectorSource({ features }),
|
|
95
|
+
style: this.createStyle(mergedOptions),
|
|
95
96
|
zIndex: mergedOptions.zIndex
|
|
96
97
|
});
|
|
97
|
-
// 设置要素样式
|
|
98
|
-
this.setFeatureStyles(features, mergedOptions);
|
|
99
98
|
layer.setVisible(mergedOptions.visible);
|
|
100
99
|
this.map.addLayer(layer);
|
|
101
100
|
// 如果需要适应视图
|
|
@@ -130,13 +129,9 @@ export default class Polygon {
|
|
|
130
129
|
layerName: mergedOptions.layerName
|
|
131
130
|
},
|
|
132
131
|
source,
|
|
132
|
+
style: this.createStyle(mergedOptions),
|
|
133
133
|
zIndex: mergedOptions.zIndex
|
|
134
134
|
});
|
|
135
|
-
// 在数据加载后设置样式
|
|
136
|
-
source.once('featuresloadend', () => {
|
|
137
|
-
const loadedFeatures = source.getFeatures();
|
|
138
|
-
this.setFeatureStyles(loadedFeatures, mergedOptions);
|
|
139
|
-
});
|
|
140
135
|
layer.setVisible(mergedOptions.visible);
|
|
141
136
|
this.map.addLayer(layer);
|
|
142
137
|
// 如果需要适应视图
|
|
@@ -148,51 +143,78 @@ export default class Polygon {
|
|
|
148
143
|
return layer;
|
|
149
144
|
}
|
|
150
145
|
/**
|
|
151
|
-
*
|
|
152
|
-
* @param
|
|
153
|
-
* @
|
|
146
|
+
* 创建样式或样式函数
|
|
147
|
+
* @param options 配置选项
|
|
148
|
+
* @returns 样式或样式函数
|
|
154
149
|
*/
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
150
|
+
//************* 创建样式功能块 *************//
|
|
151
|
+
createStyle(options) {
|
|
152
|
+
// 如果传入了自定义样式,直接使用
|
|
153
|
+
if (options.style) {
|
|
154
|
+
return options.style;
|
|
155
|
+
}
|
|
156
|
+
const withDefaultStroke = options.withDefaultStroke ?? true;
|
|
157
|
+
const withDefaultFill = options.withDefaultFill ?? true;
|
|
158
|
+
const strokeColor = options.strokeColor ?? (withDefaultStroke ? '#EBEEF5' : undefined);
|
|
159
|
+
const strokeWidth = options.strokeWidth ?? 2;
|
|
160
|
+
const staticFillColor = options.fillColor ?? (withDefaultFill ? 'rgba(255, 255, 255, 0)' : undefined);
|
|
161
|
+
const stroke = strokeColor ? new Stroke({
|
|
162
|
+
color: strokeColor,
|
|
163
|
+
width: strokeWidth,
|
|
164
|
+
lineDash: options.lineDash,
|
|
165
|
+
lineDashOffset: options.lineDashOffset
|
|
166
|
+
}) : undefined;
|
|
167
|
+
// 预先创建基础样式(如果不需要基于feature动态计算fillColor)
|
|
168
|
+
let baseStyle;
|
|
169
|
+
if (!options.fillColorCallBack) {
|
|
170
|
+
const fill = staticFillColor ? new Fill({ color: staticFillColor }) : undefined;
|
|
171
|
+
if (stroke || fill) {
|
|
172
|
+
baseStyle = new Style({
|
|
173
|
+
stroke,
|
|
174
|
+
fill
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// 如果没有动态部分(没有回调且不显示文本),直接返回基础样式
|
|
179
|
+
if (baseStyle && !options.textVisible) {
|
|
180
|
+
return baseStyle;
|
|
181
|
+
}
|
|
182
|
+
// 返回样式函数
|
|
183
|
+
return (feature, resolution) => {
|
|
184
|
+
const styles = [];
|
|
185
|
+
// 1. 处理几何样式
|
|
186
|
+
if (baseStyle) {
|
|
187
|
+
styles.push(baseStyle);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
const fillColor = options.fillColorCallBack ? options.fillColorCallBack(feature) : staticFillColor;
|
|
191
|
+
const fill = fillColor ? new Fill({ color: fillColor }) : undefined;
|
|
192
|
+
if (stroke || fill) {
|
|
193
|
+
styles.push(new Style({
|
|
194
|
+
stroke,
|
|
195
|
+
fill
|
|
196
|
+
}));
|
|
166
197
|
}
|
|
167
|
-
return;
|
|
168
198
|
}
|
|
169
|
-
|
|
170
|
-
const featureStyle = new Style({
|
|
171
|
-
stroke: new Stroke({
|
|
172
|
-
color: options.strokeColor,
|
|
173
|
-
width: options.strokeWidth,
|
|
174
|
-
lineDash: options.lineDash,
|
|
175
|
-
lineDashOffset: options.lineDashOffset
|
|
176
|
-
}),
|
|
177
|
-
fill: new Fill({ color: fillColor })
|
|
178
|
-
});
|
|
179
|
-
// 添加文本样式
|
|
199
|
+
// 2. 处理文本样式
|
|
180
200
|
if (options.textVisible) {
|
|
181
201
|
const text = this.getFeatureText(feature, options);
|
|
182
202
|
if (text) {
|
|
183
|
-
|
|
184
|
-
text
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
203
|
+
styles.push(new Style({
|
|
204
|
+
text: new Text({
|
|
205
|
+
text,
|
|
206
|
+
font: options.textFont,
|
|
207
|
+
fill: new Fill({ color: options.textFillColor }),
|
|
208
|
+
stroke: new Stroke({
|
|
209
|
+
color: options.textStrokeColor,
|
|
210
|
+
width: options.textStrokeWidth
|
|
211
|
+
})
|
|
190
212
|
})
|
|
191
213
|
}));
|
|
192
214
|
}
|
|
193
215
|
}
|
|
194
|
-
|
|
195
|
-
}
|
|
216
|
+
return styles;
|
|
217
|
+
};
|
|
196
218
|
}
|
|
197
219
|
/**
|
|
198
220
|
* 获取要素文本
|
|
@@ -237,9 +259,6 @@ export default class Polygon {
|
|
|
237
259
|
throw new Error(`Layer '${layerName}' is not a vector layer`);
|
|
238
260
|
}
|
|
239
261
|
const mergedOptions = {
|
|
240
|
-
strokeColor: '#EBEEF5',
|
|
241
|
-
strokeWidth: 2,
|
|
242
|
-
fillColor: 'rgba(255, 255, 255, 0.3)',
|
|
243
262
|
textFont: '14px Calibri,sans-serif',
|
|
244
263
|
textFillColor: '#FFF',
|
|
245
264
|
textStrokeWidth: 2,
|
|
@@ -262,13 +281,21 @@ export default class Polygon {
|
|
|
262
281
|
*/
|
|
263
282
|
updateSingleFeatureColor(feature, colorObj, options) {
|
|
264
283
|
const name = options?.textKey ? feature.get(options.textKey) : '';
|
|
265
|
-
const
|
|
284
|
+
const withDefaultStroke = options?.withDefaultStroke ?? true;
|
|
285
|
+
const withDefaultFill = options?.withDefaultFill ?? true;
|
|
286
|
+
const strokeColor = options?.strokeColor ?? (withDefaultStroke ? '#EBEEF5' : undefined);
|
|
287
|
+
const strokeWidth = options?.strokeWidth ?? 2;
|
|
288
|
+
const defaultFillColor = 'rgba(255, 255, 255, 0.3)';
|
|
289
|
+
const resolvedFillColor = options?.fillColor ?? (withDefaultFill ? defaultFillColor : undefined);
|
|
290
|
+
const newColor = colorObj?.[name] || resolvedFillColor;
|
|
291
|
+
const stroke = strokeColor ? new Stroke({
|
|
292
|
+
color: strokeColor,
|
|
293
|
+
width: strokeWidth
|
|
294
|
+
}) : undefined;
|
|
295
|
+
const fill = newColor ? new Fill({ color: newColor }) : undefined;
|
|
266
296
|
const featureStyle = new Style({
|
|
267
|
-
stroke
|
|
268
|
-
|
|
269
|
-
width: options?.strokeWidth
|
|
270
|
-
}),
|
|
271
|
-
fill: new Fill({ color: newColor })
|
|
297
|
+
stroke,
|
|
298
|
+
fill
|
|
272
299
|
});
|
|
273
300
|
// 添加文本样式
|
|
274
301
|
if (options?.textVisible) {
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -114,6 +114,10 @@ export interface StyleOptions {
|
|
|
114
114
|
fillColor?: string;
|
|
115
115
|
/** 填充颜色回调函数 */
|
|
116
116
|
fillColorCallBack?: (feature: Feature) => string;
|
|
117
|
+
/** 是否使用默认描边 */
|
|
118
|
+
withDefaultStroke?: boolean;
|
|
119
|
+
/** 是否使用默认填充 */
|
|
120
|
+
withDefaultFill?: boolean;
|
|
117
121
|
}
|
|
118
122
|
/**
|
|
119
123
|
* 文本选项接口 - 文本标注相关配置
|