my-openlayer 1.0.15 → 2.0.1
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/core/DomPoint.d.ts +21 -0
- package/core/DomPoint.js +36 -0
- package/core/Line.d.ts +24 -3
- package/core/Line.js +113 -42
- package/core/Point.js +6 -1
- package/core/Polygon.d.ts +21 -3
- package/core/Polygon.js +57 -28
- package/package.json +4 -5
- package/types.d.ts +4 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Map from "ol/Map";
|
|
2
|
+
interface Options {
|
|
3
|
+
Vue: any;
|
|
4
|
+
Template: any;
|
|
5
|
+
lgtd: number;
|
|
6
|
+
lttd: number;
|
|
7
|
+
props?: any;
|
|
8
|
+
type?: string;
|
|
9
|
+
sttp?: string;
|
|
10
|
+
zIndex?: number;
|
|
11
|
+
}
|
|
12
|
+
export default class DomPoint {
|
|
13
|
+
private map;
|
|
14
|
+
private app;
|
|
15
|
+
private anchor;
|
|
16
|
+
private dom;
|
|
17
|
+
constructor(map: Map, options: Options);
|
|
18
|
+
setVisible(visible: boolean): void;
|
|
19
|
+
remove(): void;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
package/core/DomPoint.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// import { createApp } from "vue";
|
|
2
|
+
import Overlay from "ol/Overlay";
|
|
3
|
+
export default class DomPoint {
|
|
4
|
+
constructor(map, options) {
|
|
5
|
+
this.map = map;
|
|
6
|
+
const { Vue, Template, lgtd, lttd, props, } = options;
|
|
7
|
+
this.dom = document.createElement('div');
|
|
8
|
+
this.map.getViewport().appendChild(this.dom);
|
|
9
|
+
if (Vue.version.startsWith('3')) {
|
|
10
|
+
this.app = Vue.createApp(Object.assign(Template, {
|
|
11
|
+
props: { ...props }
|
|
12
|
+
}));
|
|
13
|
+
this.app.mount(this.dom);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
this.app = new Vue({
|
|
17
|
+
el: this.dom,
|
|
18
|
+
render: (h) => h(Template, { props })
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
this.anchor = new Overlay({
|
|
22
|
+
element: this.dom,
|
|
23
|
+
positioning: 'center-center',
|
|
24
|
+
stopEvent: false
|
|
25
|
+
});
|
|
26
|
+
this.anchor.setPosition([lgtd, lttd]);
|
|
27
|
+
this.map.addOverlay(this.anchor);
|
|
28
|
+
}
|
|
29
|
+
setVisible(visible) {
|
|
30
|
+
this.dom.style.visibility = visible ? 'visible' : 'hidden';
|
|
31
|
+
}
|
|
32
|
+
remove() {
|
|
33
|
+
this.app.unmount();
|
|
34
|
+
this.map.removeOverlay(this.anchor);
|
|
35
|
+
}
|
|
36
|
+
}
|
package/core/Line.d.ts
CHANGED
|
@@ -52,19 +52,33 @@ export default class Line {
|
|
|
52
52
|
*/
|
|
53
53
|
constructor(map: Map);
|
|
54
54
|
/**
|
|
55
|
-
*
|
|
55
|
+
* 添加线要素(使用GeoJSON数据)
|
|
56
56
|
* @param data GeoJSON格式的线数据
|
|
57
57
|
* @param options 配置项
|
|
58
58
|
* @returns 创建的矢量图层
|
|
59
59
|
*/
|
|
60
60
|
addLine(data: MapJSONData, options?: LineOptions): VectorLayer<VectorSource>;
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
62
|
+
* 添加线要素(从URL加载)
|
|
63
|
+
* @param url 数据URL
|
|
64
|
+
* @param options 配置项
|
|
65
|
+
* @returns 创建的矢量图层
|
|
66
|
+
*/
|
|
67
|
+
addLine(url: string, options?: LineOptions): VectorLayer<VectorSource>;
|
|
68
|
+
/**
|
|
69
|
+
* 添加分级河流图层(使用GeoJSON数据)
|
|
63
70
|
* @param fyRiverJson 河流 GeoJSON 数据
|
|
64
71
|
* @param options 河流图层配置选项
|
|
65
72
|
* @throws {Error} 当数据格式无效时抛出错误
|
|
66
73
|
*/
|
|
67
74
|
addRiverLayersByZoom(fyRiverJson: MapJSONData, options?: RiverLayerOptions): void;
|
|
75
|
+
/**
|
|
76
|
+
* 添加分级河流图层(从URL加载)
|
|
77
|
+
* @param url 河流数据URL
|
|
78
|
+
* @param options 河流图层配置选项
|
|
79
|
+
* @throws {Error} 当数据格式无效时抛出错误
|
|
80
|
+
*/
|
|
81
|
+
addRiverLayersByZoom(url: string, options?: RiverLayerOptions): void;
|
|
68
82
|
/**
|
|
69
83
|
* 显示或隐藏河流图层
|
|
70
84
|
* @param show 是否显示河流图层
|
|
@@ -76,12 +90,19 @@ export default class Line {
|
|
|
76
90
|
*/
|
|
77
91
|
showRiverLayerByZoom(): void;
|
|
78
92
|
/**
|
|
79
|
-
*
|
|
93
|
+
* 添加按级别显示不同宽度的河流图层(使用GeoJSON数据)
|
|
80
94
|
* @param data 河流 GeoJSON 数据
|
|
81
95
|
* @param options 河流图层配置选项
|
|
82
96
|
* @returns 创建的河流图层
|
|
83
97
|
*/
|
|
84
98
|
addRiverWidthByLevel(data: MapJSONData, options?: RiverLayerOptions): VectorLayer<VectorSource>;
|
|
99
|
+
/**
|
|
100
|
+
* 添加按级别显示不同宽度的河流图层(从URL加载)
|
|
101
|
+
* @param url 河流数据URL
|
|
102
|
+
* @param options 河流图层配置选项
|
|
103
|
+
* @returns 创建的河流图层
|
|
104
|
+
*/
|
|
105
|
+
addRiverWidthByLevel(url: string, options?: RiverLayerOptions): VectorLayer<VectorSource>;
|
|
85
106
|
/**
|
|
86
107
|
* 移除线图层
|
|
87
108
|
* @param layerName 图层名称
|
package/core/Line.js
CHANGED
|
@@ -42,14 +42,11 @@ export default class Line {
|
|
|
42
42
|
this.map = map;
|
|
43
43
|
this.eventManager = new EventManager(map);
|
|
44
44
|
}
|
|
45
|
-
/**
|
|
46
|
-
* 添加线要素
|
|
47
|
-
* @param data GeoJSON格式的线数据
|
|
48
|
-
* @param options 配置项
|
|
49
|
-
* @returns 创建的矢量图层
|
|
50
|
-
*/
|
|
51
45
|
addLine(data, options = {}) {
|
|
52
|
-
|
|
46
|
+
const isUrl = typeof data === 'string';
|
|
47
|
+
if (!isUrl) {
|
|
48
|
+
ValidationUtils.validateGeoJSONData(data);
|
|
49
|
+
}
|
|
53
50
|
const defaultOptions = {
|
|
54
51
|
type: 'line',
|
|
55
52
|
strokeColor: 'rgba(3, 122, 255, 1)',
|
|
@@ -59,18 +56,35 @@ export default class Line {
|
|
|
59
56
|
layerName: options.layerName || 'lineLayer'
|
|
60
57
|
};
|
|
61
58
|
const mergedOptions = { ...defaultOptions, ...options };
|
|
62
|
-
|
|
59
|
+
// 根据数据类型创建 VectorSource
|
|
60
|
+
const source = isUrl
|
|
61
|
+
? new VectorSource({
|
|
62
|
+
url: data,
|
|
63
|
+
format: new GeoJSON(options.projectionOptOptions)
|
|
64
|
+
})
|
|
65
|
+
: new VectorSource({
|
|
66
|
+
features: new GeoJSON().readFeatures(data, options.projectionOptOptions)
|
|
67
|
+
});
|
|
63
68
|
const layer = new VectorLayer({
|
|
64
69
|
properties: {
|
|
65
70
|
name: mergedOptions.layerName,
|
|
66
71
|
layerName: mergedOptions.layerName
|
|
67
72
|
},
|
|
68
|
-
source
|
|
73
|
+
source,
|
|
69
74
|
style: (feature) => {
|
|
70
75
|
if (feature instanceof Feature) {
|
|
71
76
|
feature.set('type', mergedOptions.type);
|
|
72
77
|
feature.set('layerName', mergedOptions.type);
|
|
73
78
|
}
|
|
79
|
+
// 如果传入了自定义样式,直接使用
|
|
80
|
+
if (mergedOptions.style) {
|
|
81
|
+
if (typeof mergedOptions.style === 'function') {
|
|
82
|
+
return mergedOptions.style(feature);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return mergedOptions.style;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
74
88
|
return new Style({
|
|
75
89
|
stroke: new Stroke({
|
|
76
90
|
color: mergedOptions.strokeColor,
|
|
@@ -86,14 +100,11 @@ export default class Line {
|
|
|
86
100
|
this.map.addLayer(layer);
|
|
87
101
|
return layer;
|
|
88
102
|
}
|
|
89
|
-
/**
|
|
90
|
-
* 添加分级河流图层,根据缩放级别显示不同级别的河流
|
|
91
|
-
* @param fyRiverJson 河流 GeoJSON 数据
|
|
92
|
-
* @param options 河流图层配置选项
|
|
93
|
-
* @throws {Error} 当数据格式无效时抛出错误
|
|
94
|
-
*/
|
|
95
103
|
addRiverLayersByZoom(fyRiverJson, options = {}) {
|
|
96
|
-
|
|
104
|
+
const isUrl = typeof fyRiverJson === 'string';
|
|
105
|
+
if (!isUrl) {
|
|
106
|
+
ValidationUtils.validateGeoJSONData(fyRiverJson);
|
|
107
|
+
}
|
|
97
108
|
const defaultOptions = {
|
|
98
109
|
type: 'river',
|
|
99
110
|
levelCount: 5,
|
|
@@ -115,23 +126,61 @@ export default class Line {
|
|
|
115
126
|
this.riverLayerList = [];
|
|
116
127
|
// 创建分级河流图层
|
|
117
128
|
for (let level = 1; level <= mergedOptions.levelCount; level++) {
|
|
118
|
-
const vectorSource =
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
const vectorSource = isUrl
|
|
130
|
+
? new VectorSource({
|
|
131
|
+
url: fyRiverJson,
|
|
132
|
+
format: new GeoJSON(),
|
|
133
|
+
loader: function (extent, resolution, projection, success, failure) {
|
|
134
|
+
fetch(fyRiverJson)
|
|
135
|
+
.then(response => response.json())
|
|
136
|
+
.then(data => {
|
|
137
|
+
const geojson = new GeoJSON();
|
|
138
|
+
data.features.forEach((feature) => {
|
|
139
|
+
if (feature.properties && feature.properties.level === level) {
|
|
140
|
+
try {
|
|
141
|
+
const olFeature = geojson.readFeature(feature);
|
|
142
|
+
if (Array.isArray(olFeature)) {
|
|
143
|
+
vectorSource.addFeatures(olFeature);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
vectorSource.addFeature(olFeature);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
console.warn(`Failed to load river feature at level ${level}:`, error);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
success?.(vectorSource.getFeatures());
|
|
155
|
+
})
|
|
156
|
+
.catch(error => {
|
|
157
|
+
console.error('Error loading river data:', error);
|
|
158
|
+
failure?.();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
})
|
|
162
|
+
: new VectorSource({
|
|
163
|
+
format: new GeoJSON(),
|
|
164
|
+
loader: () => {
|
|
165
|
+
const geojson = new GeoJSON();
|
|
166
|
+
fyRiverJson.features.forEach((feature) => {
|
|
167
|
+
if (feature.properties && feature.properties.level === level) {
|
|
168
|
+
try {
|
|
169
|
+
const olFeature = geojson.readFeature(feature);
|
|
170
|
+
if (Array.isArray(olFeature)) {
|
|
171
|
+
vectorSource.addFeatures(olFeature);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
vectorSource.addFeature(olFeature);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
console.warn(`Failed to load river feature at level ${level}:`, error);
|
|
179
|
+
}
|
|
130
180
|
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
});
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
});
|
|
135
184
|
const riverLayer = new VectorLayer({
|
|
136
185
|
properties: {
|
|
137
186
|
name: mergedOptions.layerName,
|
|
@@ -144,6 +193,15 @@ export default class Line {
|
|
|
144
193
|
feature.set('type', mergedOptions.layerName);
|
|
145
194
|
feature.set('layerName', mergedOptions.layerName);
|
|
146
195
|
}
|
|
196
|
+
// 如果传入了自定义样式,直接使用
|
|
197
|
+
if (mergedOptions.style) {
|
|
198
|
+
if (typeof mergedOptions.style === 'function') {
|
|
199
|
+
return mergedOptions.style(feature);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
return mergedOptions.style;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
147
205
|
return new Style({
|
|
148
206
|
stroke: new Stroke({
|
|
149
207
|
color: mergedOptions.strokeColor,
|
|
@@ -192,14 +250,11 @@ export default class Line {
|
|
|
192
250
|
}
|
|
193
251
|
});
|
|
194
252
|
}
|
|
195
|
-
/**
|
|
196
|
-
* 添加按级别显示不同宽度的河流图层
|
|
197
|
-
* @param data 河流 GeoJSON 数据
|
|
198
|
-
* @param options 河流图层配置选项
|
|
199
|
-
* @returns 创建的河流图层
|
|
200
|
-
*/
|
|
201
253
|
addRiverWidthByLevel(data, options = {}) {
|
|
202
|
-
|
|
254
|
+
const isUrl = typeof data === 'string';
|
|
255
|
+
if (!isUrl) {
|
|
256
|
+
ValidationUtils.validateGeoJSONData(data);
|
|
257
|
+
}
|
|
203
258
|
// 合并默认配置
|
|
204
259
|
const mergedOptions = {
|
|
205
260
|
type: 'river',
|
|
@@ -216,16 +271,32 @@ export default class Line {
|
|
|
216
271
|
if (mergedOptions.removeExisting && mergedOptions.layerName) {
|
|
217
272
|
MapTools.removeLayer(this.map, mergedOptions.layerName);
|
|
218
273
|
}
|
|
219
|
-
//
|
|
220
|
-
const
|
|
274
|
+
// 根据数据类型创建 VectorSource
|
|
275
|
+
const source = isUrl
|
|
276
|
+
? new VectorSource({
|
|
277
|
+
url: data,
|
|
278
|
+
format: new GeoJSON(options.projectionOptOptions)
|
|
279
|
+
})
|
|
280
|
+
: new VectorSource({
|
|
281
|
+
features: new GeoJSON().readFeatures(data, options.projectionOptOptions)
|
|
282
|
+
});
|
|
221
283
|
// 创建河流图层
|
|
222
284
|
const riverLayer = new VectorLayer({
|
|
223
285
|
properties: {
|
|
224
286
|
name: mergedOptions.layerName,
|
|
225
287
|
layerName: mergedOptions.layerName
|
|
226
288
|
},
|
|
227
|
-
source
|
|
289
|
+
source,
|
|
228
290
|
style: (feature) => {
|
|
291
|
+
// 如果传入了自定义样式,直接使用
|
|
292
|
+
if (mergedOptions.style) {
|
|
293
|
+
if (typeof mergedOptions.style === 'function') {
|
|
294
|
+
return mergedOptions.style(feature);
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
return mergedOptions.style;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
229
300
|
const level = feature.get('level');
|
|
230
301
|
const levelWidth = mergedOptions.levelWidthMap[Number(level)] || 1;
|
|
231
302
|
return new Style({
|
package/core/Point.js
CHANGED
|
@@ -189,7 +189,12 @@ export default class Point {
|
|
|
189
189
|
feature.properties.level = item.lev;
|
|
190
190
|
const geojson = new GeoJSON();
|
|
191
191
|
const olFeature = geojson.readFeature(feature);
|
|
192
|
-
|
|
192
|
+
if (Array.isArray(olFeature)) {
|
|
193
|
+
vectorSource.addFeatures(olFeature);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
vectorSource.addFeature(olFeature);
|
|
197
|
+
}
|
|
193
198
|
if (feature) {
|
|
194
199
|
const polygonCenter = calculatePolygonCenter(feature.geometry.coordinates);
|
|
195
200
|
item.lgtd = polygonCenter[0];
|
package/core/Polygon.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import Map from "ol/Map";
|
|
|
2
2
|
import VectorLayer from "ol/layer/Vector";
|
|
3
3
|
import VectorSource from "ol/source/Vector";
|
|
4
4
|
import { Image as ImageLayer, Heatmap } from "ol/layer";
|
|
5
|
+
import { Geometry } from "ol/geom";
|
|
6
|
+
import Feature from "ol/Feature";
|
|
5
7
|
import { PolygonOptions, MapJSONData, PointData, HeatMapOptions, ImageLayerData, MaskLayerOptions, FeatureColorUpdateOptions } from '../types';
|
|
6
8
|
/**
|
|
7
9
|
* Polygon 类用于处理地图上的面要素操作
|
|
@@ -23,7 +25,7 @@ export default class Polygon {
|
|
|
23
25
|
*/
|
|
24
26
|
getLevColor(lev: string | number): string;
|
|
25
27
|
/**
|
|
26
|
-
*
|
|
28
|
+
* 添加地图边框图层(使用GeoJSON数据)
|
|
27
29
|
* @param data 图层数据,必须是有效的 GeoJSON 格式
|
|
28
30
|
* @param options 图层配置选项
|
|
29
31
|
* @returns 创建的图层实例
|
|
@@ -31,13 +33,29 @@ export default class Polygon {
|
|
|
31
33
|
*/
|
|
32
34
|
addBorderPolygon(data: MapJSONData, options?: PolygonOptions): VectorLayer<VectorSource>;
|
|
33
35
|
/**
|
|
34
|
-
*
|
|
36
|
+
* 添加地图边框图层(从URL加载)
|
|
37
|
+
* @param url 数据URL
|
|
38
|
+
* @param options 图层配置选项
|
|
39
|
+
* @returns 创建的图层实例
|
|
40
|
+
* @throws 当数据格式无效时抛出错误
|
|
41
|
+
*/
|
|
42
|
+
addBorderPolygon(url: string, options?: PolygonOptions): VectorLayer<VectorSource>;
|
|
43
|
+
/**
|
|
44
|
+
* 添加多边形图层(使用GeoJSON数据)
|
|
35
45
|
* @param dataJSON GeoJSON 数据
|
|
36
46
|
* @param options 图层配置选项
|
|
37
47
|
* @returns 创建的矢量图层
|
|
38
48
|
* @throws 当数据格式无效时抛出错误
|
|
39
49
|
*/
|
|
40
50
|
addPolygon(dataJSON: MapJSONData, options?: PolygonOptions): VectorLayer<VectorSource>;
|
|
51
|
+
/**
|
|
52
|
+
* 添加多边形图层(从URL加载)
|
|
53
|
+
* @param url 数据URL
|
|
54
|
+
* @param options 图层配置选项
|
|
55
|
+
* @returns 创建的矢量图层
|
|
56
|
+
* @throws 当数据格式无效时抛出错误
|
|
57
|
+
*/
|
|
58
|
+
addPolygon(url: string, options?: PolygonOptions): VectorLayer<VectorSource>;
|
|
41
59
|
/**
|
|
42
60
|
* 设置要素样式
|
|
43
61
|
* @param features 要素数组
|
|
@@ -126,7 +144,7 @@ export default class Polygon {
|
|
|
126
144
|
* @param pointData 点数据数组
|
|
127
145
|
* @param options 热力图配置
|
|
128
146
|
*/
|
|
129
|
-
addHeatmap(pointData: PointData[], options?: HeatMapOptions): Heatmap
|
|
147
|
+
addHeatmap(pointData: PointData[], options?: HeatMapOptions): Heatmap<Feature<Geometry>, VectorSource<Feature<Geometry>>>;
|
|
130
148
|
/**
|
|
131
149
|
* 添加遮罩图层
|
|
132
150
|
* @param data GeoJSON格式的遮罩数据
|
package/core/Polygon.js
CHANGED
|
@@ -40,35 +40,30 @@ export default class Polygon {
|
|
|
40
40
|
const key = lev.toString();
|
|
41
41
|
return this.colorMap[key] || 'rgba(128, 128, 128, 0.6)';
|
|
42
42
|
}
|
|
43
|
-
/**
|
|
44
|
-
* 添加地图边框图层
|
|
45
|
-
* @param data 图层数据,必须是有效的 GeoJSON 格式
|
|
46
|
-
* @param options 图层配置选项
|
|
47
|
-
* @returns 创建的图层实例
|
|
48
|
-
* @throws 当数据格式无效时抛出错误
|
|
49
|
-
*/
|
|
50
43
|
addBorderPolygon(data, options) {
|
|
51
|
-
|
|
44
|
+
const isUrl = typeof data === 'string';
|
|
45
|
+
if (!isUrl) {
|
|
46
|
+
ValidationUtils.validateGeoJSONData(data);
|
|
47
|
+
}
|
|
52
48
|
const mergedOptions = {
|
|
53
49
|
layerName: 'border',
|
|
54
50
|
fillColor: 'rgba(255, 255, 255, 0)',
|
|
55
51
|
...options
|
|
56
52
|
};
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
// 使用类型断言来调用重载方法
|
|
54
|
+
const layer = isUrl
|
|
55
|
+
? this.addPolygon(data, mergedOptions)
|
|
56
|
+
: this.addPolygon(data, mergedOptions);
|
|
57
|
+
if (mergedOptions.mask && !isUrl) {
|
|
59
58
|
this.setOutLayer(data);
|
|
60
59
|
}
|
|
61
60
|
return layer;
|
|
62
61
|
}
|
|
63
|
-
/**
|
|
64
|
-
* 添加多边形图层
|
|
65
|
-
* @param dataJSON GeoJSON 数据
|
|
66
|
-
* @param options 图层配置选项
|
|
67
|
-
* @returns 创建的矢量图层
|
|
68
|
-
* @throws 当数据格式无效时抛出错误
|
|
69
|
-
*/
|
|
70
62
|
addPolygon(dataJSON, options) {
|
|
71
|
-
|
|
63
|
+
const isUrl = typeof dataJSON === 'string';
|
|
64
|
+
if (!isUrl) {
|
|
65
|
+
ValidationUtils.validateGeoJSONData(dataJSON);
|
|
66
|
+
}
|
|
72
67
|
const mergedOptions = {
|
|
73
68
|
zIndex: 11,
|
|
74
69
|
visible: true,
|
|
@@ -86,28 +81,52 @@ export default class Polygon {
|
|
|
86
81
|
new MapTools(this.map).removeLayer(mergedOptions.layerName);
|
|
87
82
|
}
|
|
88
83
|
let features;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
84
|
+
// 根据数据类型创建 VectorSource
|
|
85
|
+
const source = isUrl
|
|
86
|
+
? new VectorSource({
|
|
87
|
+
url: dataJSON,
|
|
88
|
+
format: new GeoJSON(mergedOptions.projectionOptOptions ?? {})
|
|
89
|
+
})
|
|
90
|
+
: (() => {
|
|
91
|
+
try {
|
|
92
|
+
features = new GeoJSON().readFeatures(dataJSON, mergedOptions.projectionOptOptions ?? {});
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
throw new Error(`Failed to parse GeoJSON data: ${error}`);
|
|
96
|
+
}
|
|
97
|
+
return new VectorSource({ features });
|
|
98
|
+
})();
|
|
95
99
|
const layer = new VectorLayer({
|
|
96
100
|
properties: {
|
|
97
101
|
name: mergedOptions.layerName,
|
|
98
102
|
layerName: mergedOptions.layerName
|
|
99
103
|
},
|
|
100
|
-
source
|
|
104
|
+
source,
|
|
101
105
|
zIndex: mergedOptions.zIndex
|
|
102
106
|
});
|
|
103
|
-
//
|
|
104
|
-
|
|
107
|
+
// 如果不是URL,设置要素样式
|
|
108
|
+
if (!isUrl) {
|
|
109
|
+
this.setFeatureStyles(features, mergedOptions);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
// 如果是URL,需要在数据加载后设置样式
|
|
113
|
+
source.once('featuresloadend', () => {
|
|
114
|
+
const loadedFeatures = source.getFeatures();
|
|
115
|
+
this.setFeatureStyles(loadedFeatures, mergedOptions);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
105
118
|
layer.setVisible(mergedOptions.visible);
|
|
106
119
|
this.map.addLayer(layer);
|
|
107
120
|
// 如果需要适应视图
|
|
108
|
-
if (mergedOptions.fitView) {
|
|
121
|
+
if (mergedOptions.fitView && !isUrl) {
|
|
109
122
|
this.fitViewToLayer(layer);
|
|
110
123
|
}
|
|
124
|
+
else if (mergedOptions.fitView && isUrl) {
|
|
125
|
+
// 如果是URL,需要在数据加载后适应视图
|
|
126
|
+
source.once('featuresloadend', () => {
|
|
127
|
+
this.fitViewToLayer(layer);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
111
130
|
return layer;
|
|
112
131
|
}
|
|
113
132
|
/**
|
|
@@ -119,6 +138,16 @@ export default class Polygon {
|
|
|
119
138
|
features.forEach(feature => {
|
|
120
139
|
feature.set('type', options.layerName);
|
|
121
140
|
feature.set('layerName', options.layerName);
|
|
141
|
+
// 如果传入了自定义样式,直接使用
|
|
142
|
+
if (options.style) {
|
|
143
|
+
if (typeof options.style === 'function') {
|
|
144
|
+
feature.setStyle(options.style(feature));
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
feature.setStyle(options.style);
|
|
148
|
+
}
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
122
151
|
const fillColor = options.fillColorCallBack ? options.fillColorCallBack(feature) : options.fillColor;
|
|
123
152
|
const featureStyle = new Style({
|
|
124
153
|
stroke: new Stroke({
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "my-openlayer",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"types": "index.d.ts",
|
|
@@ -28,9 +28,8 @@
|
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@turf/turf": "^7.2.0",
|
|
31
|
-
"ol": "^6.
|
|
32
|
-
"proj4": "^2.7.5"
|
|
33
|
-
"my-openlayer": "^1.0.9"
|
|
31
|
+
"ol": "^10.6.1",
|
|
32
|
+
"proj4": "^2.7.5"
|
|
34
33
|
},
|
|
35
34
|
"devDependencies": {
|
|
36
35
|
"@types/proj4": "^2.5.2",
|
|
@@ -42,6 +41,6 @@
|
|
|
42
41
|
"vue-tsc": "^2.0.6"
|
|
43
42
|
},
|
|
44
43
|
"peerDependencies": {
|
|
45
|
-
"ol": "^6.
|
|
44
|
+
"ol": "^10.6.1"
|
|
46
45
|
}
|
|
47
46
|
}
|
package/types.d.ts
CHANGED
|
@@ -2,7 +2,8 @@ import BaseLayer from "ol/layer/Base";
|
|
|
2
2
|
import TileLayer from "ol/layer/Tile";
|
|
3
3
|
import { WMTS } from "ol/source";
|
|
4
4
|
import View from "ol/View";
|
|
5
|
-
import Feature from "ol/Feature";
|
|
5
|
+
import Feature, { FeatureLike } from "ol/Feature";
|
|
6
|
+
import { Style } from "ol/style";
|
|
6
7
|
export interface FeatureData {
|
|
7
8
|
type: string;
|
|
8
9
|
properties: any;
|
|
@@ -83,6 +84,8 @@ export interface BaseOptions {
|
|
|
83
84
|
mapClipData?: MapJSONData;
|
|
84
85
|
/** 投影选项 */
|
|
85
86
|
projectionOptOptions?: any;
|
|
87
|
+
/** 自定义样式函数 */
|
|
88
|
+
style?: Style | Style[] | ((feature: FeatureLike) => Style | Style[]);
|
|
86
89
|
}
|
|
87
90
|
/**
|
|
88
91
|
* 样式选项接口 - 图形样式相关配置
|