my-openlayer 0.1.3 → 0.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.
package/README.md CHANGED
@@ -94,7 +94,8 @@ point.addPoint([
94
94
  lttd: 29.969,
95
95
  name: '测试点位'
96
96
  }
97
- ], 'test-point', {
97
+ ], {
98
+ type: 'test-point',
98
99
  nameKey: 'name', // 名称字段
99
100
  img: 'marker.png', // 图标路径
100
101
  hasImg: true, // 是否显示图标
package/dist/MyOl.d.ts CHANGED
@@ -64,10 +64,4 @@ export default class MyOl {
64
64
  * @param callback 回调函数
65
65
  */
66
66
  mapOnEvent(eventType: string | undefined, callback: (feature?: any, e?: any) => void, clickType?: 'point' | 'line' | 'polygon' | undefined): void;
67
- /**
68
- * 隐藏&展示图层
69
- * @param layerName 图层名称
70
- * @param show 是否显示
71
- */
72
- showMapLayer(layerName: string, show?: boolean): boolean;
73
67
  }
package/dist/MyOl.js CHANGED
@@ -21,8 +21,6 @@ class MyOl {
21
21
  if (Array.isArray(options.layers)) {
22
22
  layers = options.layers;
23
23
  }
24
- else {
25
- }
26
24
  this.map = new Map({
27
25
  target: id, // 地图容器
28
26
  view: MyOl.getView(this.options), // 视图
@@ -55,7 +53,6 @@ class MyOl {
55
53
  units: "degrees"
56
54
  });
57
55
  olProjAddProjection(cgsc2000);
58
- // debugger
59
56
  // 视图配置
60
57
  const viewOptions = {
61
58
  projection: cgsc2000, // 坐标系
@@ -156,63 +153,6 @@ class MyOl {
156
153
  mapOnEvent(eventType = "def", callback, clickType) {
157
154
  MapTools.mapOnEvent(this.map, eventType, callback, clickType);
158
155
  }
159
- /**
160
- * 隐藏&展示图层
161
- * @param layerName 图层名称
162
- * @param show 是否显示
163
- */
164
- showMapLayer(layerName, show = true) {
165
- if (!layerName) {
166
- console.error("缺少图层名称");
167
- return false;
168
- }
169
- let layers = this.getTools().getLayerByLayerName(layerName);
170
- if (!Array.isArray(layers))
171
- layers = [layers];
172
- // 无图层返回不继续操作
173
- if (layers.length === 0)
174
- return false;
175
- const zoom = this.map.getView().getZoom();
176
- for (const layer of layers) {
177
- if (layer) {
178
- if (layer.getVisible() === show)
179
- return false; // 相同则不处理
180
- const layerName = layer.values_.layerName;
181
- let isMoreLayer = false;
182
- // 若为打开,查找是否按层级显示的图层
183
- if (show) {
184
- for (let i = 2; i <= 5; i++) {
185
- if (["reservoir_frgrd_" + i, "city_frgrd_" + i, "rain_frgrd_" + i].includes(layerName)) {
186
- let minZoom = 10;
187
- switch (i) {
188
- case 2:
189
- minZoom = 10;
190
- break;
191
- case 3:
192
- minZoom = 11;
193
- break;
194
- case 4:
195
- minZoom = 12;
196
- break;
197
- case 5:
198
- minZoom = 13;
199
- break;
200
- }
201
- isMoreLayer = true; // 有进入,单下面未进入则代表不显示与下面的continue使用
202
- if (zoom && (zoom > minZoom && zoom < 20)) {
203
- layer.setVisible(true);
204
- break;
205
- }
206
- }
207
- }
208
- }
209
- if (!isMoreLayer) {
210
- layer.setVisible(show);
211
- }
212
- }
213
- }
214
- return true;
215
- }
216
156
  }
217
157
  MyOl.DefaultOptions = {
218
158
  layers: undefined,
@@ -12,8 +12,8 @@ interface Options {
12
12
  export default class DomPoint {
13
13
  private map;
14
14
  private app;
15
- private anchor;
16
- private dom;
15
+ private readonly anchor;
16
+ private readonly dom;
17
17
  constructor(map: Map, options: Options);
18
18
  setVisible(visible: boolean): void;
19
19
  remove(): void;
@@ -9,7 +9,7 @@ export default class Line {
9
9
  riverLayerShow: boolean;
10
10
  [propertyName: string]: any;
11
11
  constructor(map: Map);
12
- addLineCommon(data: MapJSONData, options: OptionsType): VectorLayer<VectorSource<import("ol/geom").Geometry>>;
12
+ addLine(data: MapJSONData, options: OptionsType): VectorLayer<VectorSource<import("ol/geom").Geometry>>;
13
13
  addRiverLayersByZoom(fyRiverJson: MapJSONData, options?: OptionsType): void;
14
14
  showRiverLayer(show: boolean): void;
15
15
  showRiverLayerByZoom(): void;
package/dist/core/Line.js CHANGED
@@ -9,7 +9,7 @@ export default class Line {
9
9
  this.riverLayerShow = false;
10
10
  this.map = map;
11
11
  }
12
- addLineCommon(data, options) {
12
+ addLine(data, options) {
13
13
  const layer = new VectorLayer({
14
14
  name: options.type,
15
15
  layerName: options.type,
@@ -22,11 +22,11 @@ export default class Line {
22
22
  return new Style({
23
23
  stroke: new Stroke({
24
24
  color: options.strokeColor || 'rgba(3, 122, 255, 1)',
25
- width: options.strokeWidth || 3
25
+ width: options.strokeWidth || 2
26
26
  }),
27
27
  });
28
28
  },
29
- zIndex: options.zIndex ?? 3
29
+ zIndex: options.zIndex ?? 15
30
30
  });
31
31
  this[options.type + 'Layer'] = layer;
32
32
  this.map.addLayer(layer);
@@ -64,7 +64,7 @@ export default class Line {
64
64
  })
65
65
  });
66
66
  },
67
- zIndex: options.zIndex ?? 6
67
+ zIndex: options.zIndex ?? 15
68
68
  });
69
69
  riverLayer.setVisible(false);
70
70
  this.riverLayerList.push(riverLayer);
@@ -102,7 +102,7 @@ export default class Line {
102
102
  features: (new GeoJSON()).readFeatures(arr)
103
103
  }),
104
104
  style: (feature) => this.setFeatureAttr(feature),
105
- zIndex: 3
105
+ zIndex: 15
106
106
  });
107
107
  this.map.addLayer(riverLayer);
108
108
  }
@@ -30,7 +30,7 @@ export default class MapBaseLayers {
30
30
  }
31
31
  if (this.layers && Object.keys(this.layers).length !== 0) {
32
32
  this.addMapLayer();
33
- // this.switchBaseLayer(<string>Object.keys(this.layers)[0])
33
+ this.switchBaseLayer(Object.keys(this.layers)[0]);
34
34
  }
35
35
  }
36
36
  }
@@ -42,13 +42,13 @@ export default class MapBaseLayers {
42
42
  for (const key in this.layers) {
43
43
  this.layers[key]?.forEach((layer) => {
44
44
  layer.setVisible(key === type);
45
- console.log(layer);
45
+ // console.log(layer)
46
46
  });
47
47
  }
48
48
  }
49
49
  initLayer() {
50
50
  if (this.layers && this.options.token) {
51
- const { token, zIndex = 10 } = this.options;
51
+ const { token, zIndex = 9 } = this.options;
52
52
  this.layers.vec_c = [MapBaseLayers.getTiandiTuLayer({ type: 'vec_c', token, zIndex, visible: false })];
53
53
  this.layers.img_c = [MapBaseLayers.getTiandiTuLayer({ type: 'img_c', token, zIndex, visible: false })];
54
54
  this.layers.ter_c = [MapBaseLayers.getTiandiTuLayer({ type: 'ter_c', token, zIndex, visible: false })];
@@ -1,18 +1,43 @@
1
1
  import Map from "ol/Map";
2
2
  import { MapJSONData } from "../types";
3
+ import VectorLayer from "ol/layer/Vector";
4
+ import VectorSource from "ol/source/Vector";
5
+ import BaseLayer from "ol/layer/Base";
6
+ import ImageLayer from "ol/layer/Image";
7
+ import ImageSource from "ol/source/Image";
3
8
  export default class MapTools {
4
- private map;
9
+ private readonly map;
5
10
  constructor(map: Map);
6
11
  /**
7
12
  * 根据名称获取图层
8
13
  * @param layerName 图层名称
9
14
  */
10
- getLayerByLayerName(layerName: string): any;
15
+ getLayerByLayerName(layerName: string): (BaseLayer | VectorLayer<VectorSource<import("ol/geom").Geometry>> | ImageLayer<ImageSource>)[];
16
+ static getLayerByLayerName(map: Map, layerName: string): (BaseLayer | VectorLayer<VectorSource<import("ol/geom").Geometry>> | ImageLayer<ImageSource>)[];
11
17
  /**
12
18
  * 设置地图裁剪
13
19
  */
14
20
  static setMapClip(baseLayer: any, data: MapJSONData): any;
21
+ /**
22
+ * 移除图层
23
+ * @param layerName 图层名称
24
+ */
15
25
  removeLayer(layerName: string): void;
26
+ /**
27
+ * 移除图层
28
+ * @param map 地图对象
29
+ * @param layerName 图层名称
30
+ */
31
+ static removeLayer(map: Map, layerName: string): void;
32
+ setLayerVisible(layerName: string, visible: boolean): void;
33
+ /**
34
+ * 设置图层可见性
35
+ * @param map
36
+ * @param layerName 图层名称
37
+ * @param visible 是否可见
38
+ */
39
+ static setLayerVisible: (map: Map, layerName: string, visible: boolean) => void;
40
+ mapOnEvent(eventType: string, callback: (feature?: any, e?: any) => void, clickType?: 'point' | 'line' | 'polygon' | undefined): void;
16
41
  /**
17
42
  * 地图监听事件
18
43
  * @param map
@@ -4,7 +4,7 @@ import VectorSource from "ol/source/Vector";
4
4
  import GeoJSON from "ol/format/GeoJSON";
5
5
  import { Fill, Style } from "ol/style";
6
6
  import { getVectorContext } from "ol/render";
7
- export default class MapTools {
7
+ class MapTools {
8
8
  constructor(map) {
9
9
  this.map = map;
10
10
  }
@@ -14,17 +14,19 @@ export default class MapTools {
14
14
  */
15
15
  getLayerByLayerName(layerName) {
16
16
  if (!this.map)
17
- return null;
17
+ return [];
18
+ return MapTools.getLayerByLayerName(this.map, layerName);
19
+ }
20
+ static getLayerByLayerName(map, layerName) {
18
21
  const targetLayer = [];
19
- const layers = this.map.getLayers().getArray();
22
+ const layers = map.getLayers().getArray();
20
23
  Object.keys(layers).forEach(function (key) {
21
24
  const _layerName = layers[key]['values_'].layerName;
22
- // if (_layerName && _layerName.toString().includes(layerName)) {
23
25
  if (_layerName && _layerName === layerName) {
24
26
  targetLayer.push(layers[key]);
25
27
  }
26
28
  });
27
- return targetLayer.length === 1 ? targetLayer[0] : targetLayer;
29
+ return targetLayer;
28
30
  }
29
31
  /**
30
32
  * 设置地图裁剪
@@ -60,8 +62,35 @@ export default class MapTools {
60
62
  });
61
63
  return baseLayer;
62
64
  }
65
+ /**
66
+ * 移除图层
67
+ * @param layerName 图层名称
68
+ */
63
69
  removeLayer(layerName) {
64
- this.map.removeLayer(this.getLayerByLayerName(layerName));
70
+ if (!this.map)
71
+ return;
72
+ MapTools.removeLayer(this.map, layerName);
73
+ }
74
+ /**
75
+ * 移除图层
76
+ * @param map 地图对象
77
+ * @param layerName 图层名称
78
+ */
79
+ static removeLayer(map, layerName) {
80
+ const layers = MapTools.getLayerByLayerName(map, layerName);
81
+ layers.forEach(layer => {
82
+ map.removeLayer(layer);
83
+ });
84
+ }
85
+ setLayerVisible(layerName, visible) {
86
+ if (!this.map)
87
+ return;
88
+ MapTools.setLayerVisible(this.map, layerName, visible);
89
+ }
90
+ mapOnEvent(eventType, callback, clickType) {
91
+ if (!this.map)
92
+ return;
93
+ MapTools.mapOnEvent(this.map, eventType, callback, clickType);
65
94
  }
66
95
  /**
67
96
  * 地图监听事件
@@ -104,3 +133,16 @@ export default class MapTools {
104
133
  }
105
134
  }
106
135
  }
136
+ /**
137
+ * 设置图层可见性
138
+ * @param map
139
+ * @param layerName 图层名称
140
+ * @param visible 是否可见
141
+ */
142
+ MapTools.setLayerVisible = (map, layerName, visible) => {
143
+ const layers = MapTools.getLayerByLayerName(map, layerName);
144
+ layers.forEach(layer => {
145
+ layer.setVisible(visible);
146
+ });
147
+ };
148
+ export default MapTools;
@@ -1,7 +1,7 @@
1
1
  import Map from "ol/Map";
2
2
  import VectorLayer from "ol/layer/Vector";
3
3
  import VectorSource from "ol/source/Vector";
4
- import { OptionsType, PointData } from '../types';
4
+ import { MapJSONData, OptionsType, PointData } from '../types';
5
5
  export default class Point {
6
6
  private map;
7
7
  constructor(map: Map);
@@ -16,13 +16,8 @@ export default class Point {
16
16
  * }
17
17
  */
18
18
  addPoint(pointData: PointData[], options: OptionsType): VectorLayer<VectorSource<import("ol/geom").Geometry>>;
19
- addClusterPoint(pointData: any[], options: OptionsType): void;
20
- /**
21
- * 添加点 - 闪烁
22
- *
23
- */
24
- addFlashWarnPoint(img: any): void;
25
- setTwinkleLayerFromPolygon(twinkleList: any[], className: string, key: string, json: any): void;
19
+ addClusterPoint(pointData: PointData[], options: OptionsType): void;
20
+ setTwinkleLayerFromPolygon(twinkleList: any[], className: string, key: string, json: MapJSONData): void;
26
21
  /**
27
22
  * 设置闪烁点
28
23
  * @param twinkleList 闪烁点数据 - 二维数组 [[],[]]
@@ -55,8 +55,8 @@ export default class Point {
55
55
  src: options.img,
56
56
  scale: options.scale ?? 1,
57
57
  };
58
- if (options.color) {
59
- iconOptions.color = options.color;
58
+ if (options.iconColor) {
59
+ iconOptions.color = options.iconColor;
60
60
  }
61
61
  style.image = new Icon(iconOptions);
62
62
  }
@@ -68,7 +68,7 @@ export default class Point {
68
68
  source: new VectorSource({
69
69
  features: pointFeatureList
70
70
  }),
71
- zIndex: options.zIndex || 4,
71
+ zIndex: options.zIndex || 21,
72
72
  });
73
73
  PointVectorLayer.setVisible(options.visible === undefined ? true : options.visible);
74
74
  this.map.addLayer(PointVectorLayer);
@@ -94,52 +94,39 @@ export default class Point {
94
94
  layerName: options.type,
95
95
  source: clusterSource,
96
96
  style: function (feature) {
97
- const aviValue = feature.get('features')[0].get('name');
98
- return new Style({
99
- image: new Icon({
100
- src: options.img,
101
- }),
102
- text: new Text({
103
- text: aviValue,
104
- font: '12px Calibri,sans-serif',
97
+ const name = feature.get('features')[0].get(options.nameKey);
98
+ const style = {};
99
+ if (options.nameKey) {
100
+ style.text = new Text({
101
+ text: name,
102
+ font: options.textFont || '12px Calibri,sans-serif',
105
103
  fill: new Fill({
106
- color: '#FFF'
104
+ color: options.textFillColor || '#FFF'
107
105
  }),
108
106
  stroke: new Stroke({
109
- color: '#000',
110
- width: 3
107
+ color: options.textStrokeColor || '#000',
108
+ width: options.textStrokeWidth || 3
111
109
  }),
112
- offsetY: 20,
113
- }),
114
- });
110
+ offsetY: options.textOffsetY || 20,
111
+ });
112
+ }
113
+ if (options.hasImg || options.hasImg === undefined) {
114
+ const iconOptions = {
115
+ src: options.img,
116
+ scale: options.scale ?? 1,
117
+ };
118
+ if (options.iconColor) {
119
+ iconOptions.color = options.iconColor;
120
+ }
121
+ style.image = new Icon(iconOptions);
122
+ }
123
+ return new Style(style);
115
124
  },
116
- zIndex: options.zIndex || 4,
125
+ zIndex: options.zIndex || 21,
117
126
  });
118
127
  clusterLayer.setVisible(options.visible === undefined ? true : options.visible);
119
128
  this.map.addLayer(clusterLayer);
120
129
  }
121
- /**
122
- * 添加点 - 闪烁
123
- *
124
- */
125
- addFlashWarnPoint(img) {
126
- const flashIconLayer = new VectorLayer({
127
- source: new VectorSource({
128
- features: [
129
- new Feature({
130
- geometry: new olPoint([119.81, 29.969]),
131
- })
132
- ]
133
- }),
134
- style: new Style({
135
- image: new Icon({
136
- src: img,
137
- })
138
- }),
139
- zIndex: 99
140
- });
141
- this.map.addLayer(flashIconLayer);
142
- }
143
130
  // 在流域中心添加闪烁点位
144
131
  setTwinkleLayerFromPolygon(twinkleList, className, key, json) {
145
132
  new MapTools(this.map).removeLayer('twinklePoint');
@@ -190,7 +177,7 @@ export default class Point {
190
177
  })
191
178
  });
192
179
  },
193
- zIndex: 6
180
+ zIndex: 21
194
181
  });
195
182
  this.map.addLayer(basinLayer);
196
183
  this.setTwinkleLayer(twinkleList, className, key, (twinkleItem) => {
@@ -1,8 +1,9 @@
1
1
  import Map from "ol/Map";
2
2
  import VectorLayer from "ol/layer/Vector";
3
3
  import VectorSource from "ol/source/Vector";
4
+ import { Image as ImageLayer } from "ol/layer";
4
5
  import { Geometry } from "ol/geom";
5
- import { OptionsType, MapJSONData } from '../types';
6
+ import { OptionsType, MapJSONData, PointData, HeatMapOptions } from '../types';
6
7
  export default class Polygon {
7
8
  map: Map;
8
9
  private colorMap;
@@ -18,21 +19,21 @@ export default class Polygon {
18
19
  * @param data 图层数据
19
20
  * @param options 图层配置
20
21
  */
21
- addBorderPolygonLayer(data: MapJSONData, options: OptionsType): void;
22
- addPolygonLayerCommon(dataJSON: MapJSONData, options?: OptionsType): VectorLayer<VectorSource<Geometry>>;
22
+ addBorderPolygon(data: MapJSONData, options?: OptionsType): void;
23
+ addPolygon(dataJSON: MapJSONData, options?: OptionsType): VectorLayer<VectorSource<Geometry>>;
23
24
  /**
24
25
  * 根据数据数组更新某个面颜色
25
26
  * @param layerName 图层名称
26
- * @param colorObj 数据
27
+ * @param colorObj 数据 不传或者传{}则重置所有颜色
27
28
  * colorObj:{
28
29
  * 对应geojson文件中的索引字段[propName]: 'rgba(255, 0, 0, 0.6)', // 颜色
29
30
  * ...
30
31
  * }
31
32
  * @param options 配置项
32
33
  */
33
- updateFeatureColors(layerName: string, colorObj: {
34
+ updateFeatureColors(layerName: string, colorObj?: {
34
35
  [propName: string]: string;
35
- }, options: OptionsType): void;
36
+ }, options?: OptionsType): void;
36
37
  /**
37
38
  * 设置外围蒙版图层
38
39
  *
@@ -46,6 +47,7 @@ export default class Polygon {
46
47
  fillColor?: string;
47
48
  strokeWidth?: number;
48
49
  strokeColor?: string;
50
+ zIndex?: number;
49
51
  }): void;
50
52
  /**
51
53
  * 添加图片图层
@@ -54,6 +56,7 @@ export default class Polygon {
54
56
  * @param extent 图片范围(对角线坐标) [minx, miny, maxx, maxy]
55
57
  * @param options 图层配置
56
58
  */
57
- addImgLayer(layerName: string, img: string | undefined, extent: number[], options?: OptionsType): any;
59
+ addImage(layerName: string, img?: string, extent?: number[], options?: OptionsType): import("ol/layer/Base").default | VectorLayer<VectorSource<Geometry>> | ImageLayer<import("ol/source").Image>;
60
+ addHeatmap(layerName: string, pointData: PointData[], options: HeatMapOptions): void;
58
61
  removePolygonLayer(layerName: string): void;
59
62
  }
@@ -3,8 +3,8 @@ import VectorLayer from "ol/layer/Vector";
3
3
  import VectorSource from "ol/source/Vector";
4
4
  import GeoJSON from "ol/format/GeoJSON";
5
5
  import { Fill, Stroke, Style, Text } from "ol/style";
6
- import { Image as ImageLayer } from "ol/layer";
7
- import { Geometry, LinearRing } from "ol/geom";
6
+ import { Image as ImageLayer, Heatmap } from "ol/layer";
7
+ import { Geometry, LinearRing, Point } from "ol/geom";
8
8
  import { fromExtent } from "ol/geom/Polygon";
9
9
  import Feature from "ol/Feature";
10
10
  import ImageStatic from "ol/source/ImageStatic";
@@ -31,18 +31,20 @@ export default class Polygon {
31
31
  * @param data 图层数据
32
32
  * @param options 图层配置
33
33
  */
34
- addBorderPolygonLayer(data, options) {
34
+ addBorderPolygon(data, options) {
35
+ options = options ?? {};
35
36
  options.type = options.type ?? 'border';
36
37
  options.fillColor = options.fillColor ?? 'rgba(255, 255, 255, 0)';
37
- this.addPolygonLayerCommon(data, options);
38
+ this.addPolygon(data, options);
38
39
  if (options.mask)
39
40
  this.setOutLayer(data);
40
41
  }
41
42
  // 添加分区
42
43
  //fyBasinJson中的id的key需要跟options中的nameKey一致
43
- addPolygonLayerCommon(dataJSON, options = {}) {
44
+ addPolygon(dataJSON, options) {
45
+ options = options ?? {};
44
46
  if (options.type != null) {
45
- new MapTools(this.map).removeLayer(options.type);
47
+ MapTools.removeLayer(this.map, options.type);
46
48
  }
47
49
  const layer = new VectorLayer({
48
50
  name: options.type,
@@ -50,29 +52,52 @@ export default class Polygon {
50
52
  source: new VectorSource({
51
53
  features: (new GeoJSON()).readFeatures(dataJSON, options.projectionOptOptions ?? {})
52
54
  }),
53
- style: function (feature) {
54
- feature.set('type', options.type);
55
- feature.set('layerName', options.type);
56
- return new Style({
55
+ // style: function (feature: any) {
56
+ // feature.set('type', options?.type)
57
+ // feature.set('layerName', options?.type)
58
+ // return new Style({
59
+ // stroke: new Stroke({
60
+ // color: options?.strokeColor ?? '#EBEEF5',
61
+ // width: options?.strokeWidth ?? 2,
62
+ // lineDash: options?.lineDash,
63
+ // lineDashOffset: options?.lineDashOffset
64
+ // }),
65
+ // fill: new Fill({ color: options?.fillColor || 'rgba(255, 255, 255, 0.3)' }),
66
+ // text: new Text({
67
+ // text: options?.textVisible && options.nameKey ? feature.values_[options.nameKey] : "",
68
+ // font: options?.textFont ?? '14px Calibri,sans-serif',
69
+ // fill: new Fill({ color: options?.textFillColor ?? '#FFF' }),
70
+ // stroke: new Stroke({
71
+ // color: options?.textStrokeColor ?? '#409EFF',
72
+ // width: options?.textStrokeWidth ?? 2
73
+ // })
74
+ // })
75
+ // })
76
+ // },
77
+ zIndex: options.zIndex ?? 11
78
+ });
79
+ const features = layer.getSource()?.getFeatures();
80
+ features?.forEach(feature => {
81
+ feature.set('type', options?.type);
82
+ feature.set('layerName', options?.type);
83
+ feature.setStyle(new Style({
84
+ stroke: new Stroke({
85
+ color: options?.strokeColor ?? '#EBEEF5',
86
+ width: options?.strokeWidth ?? 2,
87
+ lineDash: options?.lineDash,
88
+ lineDashOffset: options?.lineDashOffset
89
+ }),
90
+ fill: new Fill({ color: options?.fillColor || 'rgba(255, 255, 255, 0.3)' }),
91
+ text: new Text({
92
+ text: options?.textVisible && options.nameKey ? feature.get(options.nameKey) : "",
93
+ font: options?.textFont ?? '14px Calibri,sans-serif',
94
+ fill: new Fill({ color: options?.textFillColor ?? '#FFF' }),
57
95
  stroke: new Stroke({
58
- color: options.strokeColor ?? '#EBEEF5',
59
- width: options.strokeWidth ?? 3,
60
- lineDash: options.lineDash,
61
- lineDashOffset: options.lineDashOffset
62
- }),
63
- fill: new Fill({ color: options.fillColor || 'rgba(255, 255, 255, 0.3)' }),
64
- text: new Text({
65
- text: options.nameKey ? feature.values_[options.nameKey] : "",
66
- font: options.textFont ?? '14px Calibri,sans-serif',
67
- fill: new Fill({ color: options.textFillColor ?? '#FFF' }),
68
- stroke: new Stroke({
69
- color: options.textStrokeColor ?? '#409EFF',
70
- width: options.textStrokeWidth ?? 2
71
- })
96
+ color: options?.textStrokeColor ?? '#409EFF',
97
+ width: options?.textStrokeWidth ?? 2
72
98
  })
73
- });
74
- },
75
- zIndex: options.zIndex ?? 2
99
+ })
100
+ }));
76
101
  });
77
102
  layer.setVisible(options.visible === undefined ? true : options.visible);
78
103
  this.map.addLayer(layer);
@@ -88,7 +113,7 @@ export default class Polygon {
88
113
  /**
89
114
  * 根据数据数组更新某个面颜色
90
115
  * @param layerName 图层名称
91
- * @param colorObj 数据
116
+ * @param colorObj 数据 不传或者传{}则重置所有颜色
92
117
  * colorObj:{
93
118
  * 对应geojson文件中的索引字段[propName]: 'rgba(255, 0, 0, 0.6)', // 颜色
94
119
  * ...
@@ -96,32 +121,33 @@ export default class Polygon {
96
121
  * @param options 配置项
97
122
  */
98
123
  updateFeatureColors(layerName, colorObj, options) {
99
- const layer = this.map.getLayers().getArray().find(layer => layer.get('name') === layerName);
124
+ const layer = MapTools.getLayerByLayerName(this.map, layerName)[0];
100
125
  if (layer instanceof VectorLayer) {
101
- const source = layer.getSource();
102
- const features = source.getFeatures();
126
+ const features = layer.getSource()?.getFeatures();
103
127
  features.forEach((feature) => {
104
- if (options.nameKey) {
105
- const name = feature['values_'][options.nameKey];
106
- const newColor = colorObj[name];
107
- if (newColor) {
108
- feature.setStyle(new Style({
128
+ if (options?.nameKey || (!colorObj || Object.keys(colorObj).length === 0)) {
129
+ const name = options?.nameKey ? feature.get(options.nameKey) : '';
130
+ const newColor = colorObj?.[name];
131
+ // const oldStyle = feature.getStyle()
132
+ // console.log(oldStyle)
133
+ // if (newColor) {
134
+ feature.setStyle(new Style({
135
+ stroke: new Stroke({
136
+ color: options?.strokeColor ?? '#EBEEF5',
137
+ width: options?.strokeWidth ?? 2
138
+ }),
139
+ fill: new Fill({ color: newColor || options?.fillColor || 'rgba(255, 255, 255, 0.3)' }),
140
+ text: new Text({
141
+ text: options?.textVisible === false ? "" : name,
142
+ font: options?.textFont ?? '14px Calibri,sans-serif',
143
+ fill: new Fill({ color: options?.textFillColor || '#FFF' }),
109
144
  stroke: new Stroke({
110
- color: options.strokeColor ?? '#EBEEF5',
111
- width: options.strokeWidth ?? 3
112
- }),
113
- fill: new Fill({ color: newColor }),
114
- text: new Text({
115
- text: options.textVisible === false ? "" : name,
116
- font: options.textFont ?? '14px Calibri,sans-serif',
117
- fill: new Fill({ color: options.textFillColor || '#FFF' }),
118
- stroke: new Stroke({
119
- color: options.textStrokeColor ?? '#409EFF',
120
- width: options.textStrokeWidth ?? 2
121
- })
145
+ color: options?.textStrokeColor ?? '#409EFF',
146
+ width: options?.textStrokeWidth ?? 2
122
147
  })
123
- }));
124
- }
148
+ })
149
+ }));
150
+ // }
125
151
  }
126
152
  });
127
153
  }
@@ -203,7 +229,7 @@ export default class Polygon {
203
229
  const vtLayer = new VectorLayer({
204
230
  source: vtSource,
205
231
  style: shadeStyle,
206
- zIndex: 99
232
+ zIndex: options?.zIndex ?? 12
207
233
  });
208
234
  this.map.addLayer(vtLayer);
209
235
  const features = new GeoJSON().readFeatures(data);
@@ -223,38 +249,60 @@ export default class Polygon {
223
249
  * @param extent 图片范围(对角线坐标) [minx, miny, maxx, maxy]
224
250
  * @param options 图层配置
225
251
  */
226
- addImgLayer(layerName, img, extent, options = { zIndex: 3 }) {
252
+ addImage(layerName, img, extent, options) {
253
+ let imageLayer = MapTools.getLayerByLayerName(this.map, layerName)[0];
227
254
  if (img && extent) {
228
255
  const source = new ImageStatic({
229
256
  url: img,
230
257
  imageExtent: extent
231
258
  });
232
- if (this[layerName]) {
233
- this[layerName].setSource(source);
259
+ if (imageLayer && imageLayer instanceof ImageLayer) {
260
+ imageLayer.setSource(source);
234
261
  }
235
262
  else {
236
- let imageLayer = new ImageLayer();
263
+ imageLayer = new ImageLayer();
237
264
  imageLayer.set('name', layerName);
238
265
  imageLayer.set('layerName', layerName);
239
- imageLayer.setSource(source);
240
- imageLayer.setZIndex(options.zIndex ?? 3);
241
- imageLayer.setOpacity(options.opacity ?? 1);
242
- if (options.visible !== undefined)
243
- imageLayer.setVisible(options.visible);
244
- if (options.mapClip && options.mapClipData) {
245
- imageLayer = MapTools.setMapClip(imageLayer, options.mapClipData);
266
+ if (imageLayer instanceof ImageLayer)
267
+ imageLayer.setSource(source);
268
+ imageLayer.setZIndex(options?.zIndex ?? 11);
269
+ imageLayer.setOpacity(options?.opacity ?? 1);
270
+ if (options?.visible !== undefined)
271
+ imageLayer.setVisible(options?.visible);
272
+ if (options?.mapClip && options?.mapClipData) {
273
+ imageLayer = MapTools.setMapClip(imageLayer, options?.mapClipData);
246
274
  }
247
275
  this.map.addLayer(imageLayer);
248
- this[layerName] = imageLayer;
249
276
  }
250
277
  }
251
278
  else {
252
279
  this.removePolygonLayer(layerName);
253
280
  }
254
- return this[layerName];
281
+ return imageLayer;
282
+ }
283
+ addHeatmap(layerName, pointData, options) {
284
+ const heatmapLayer = new Heatmap({
285
+ source: new VectorSource(),
286
+ weight: function (fea) {
287
+ return fea.get('weight');
288
+ },
289
+ blur: options.blur ?? 15,
290
+ radius: options.radius ?? 10,
291
+ zIndex: options.zIndex ?? 11,
292
+ opacity: options.opacity ?? 1,
293
+ });
294
+ heatmapLayer.set('layerName', layerName);
295
+ this.map.addLayer(heatmapLayer);
296
+ const max = Math.max(...pointData.map(item => item[options.valueKey]));
297
+ pointData.forEach((item) => {
298
+ heatmapLayer?.getSource().addFeature(new Feature({
299
+ geometry: new Point([item.lgtd, item.lttd]),
300
+ weight: item[options.valueKey] / max //热力值范围是【0,1】;热力值计算 = 找出数据集中的最大值,然后用值除以最大值
301
+ }));
302
+ });
255
303
  }
256
304
  removePolygonLayer(layerName) {
257
- new MapTools(this.map).removeLayer(layerName);
305
+ MapTools.removeLayer(this.map, layerName);
258
306
  this[layerName] = null;
259
307
  }
260
308
  }
package/dist/types.d.ts CHANGED
@@ -48,6 +48,15 @@ export interface AnnotationLayerOptions {
48
48
  zIndex?: number;
49
49
  visible?: boolean;
50
50
  }
51
+ export interface HeatMapOptions {
52
+ radius?: number;
53
+ blur?: number;
54
+ gradient?: string[];
55
+ opacity?: number;
56
+ visible?: boolean;
57
+ zIndex?: number;
58
+ valueKey: string;
59
+ }
51
60
  export interface OptionsType {
52
61
  type?: string;
53
62
  nameKey?: string;
@@ -72,6 +81,8 @@ export interface OptionsType {
72
81
  opacity?: number;
73
82
  mapClip?: boolean;
74
83
  mapClipData?: MapJSONData;
84
+ mask?: boolean;
85
+ iconColor?: string;
75
86
  [propName: string]: any;
76
87
  }
77
88
  export interface PointData {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "0.1.3",
4
+ "version": "0.1.5",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",