my-openlayer 0.0.15 → 0.0.17

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/dist/MyOl.d.ts ADDED
@@ -0,0 +1,96 @@
1
+ import View from "ol/View";
2
+ import Map from "ol/Map";
3
+ import Polygon from "./core/Polygon";
4
+ import Point from "./core/Point";
5
+ import Line from "./core/Line";
6
+ import MapTools from "./core/MapTools";
7
+ import { OptionsType, MapInitType, MapbaseType } from './types';
8
+ export default class MyOl {
9
+ map: Map;
10
+ private baseLayers;
11
+ private polygon;
12
+ private mapTools;
13
+ private point;
14
+ private line;
15
+ private readonly options;
16
+ static DefaultOptions: MapInitType;
17
+ constructor(id: string, options: MapInitType);
18
+ /**
19
+ * 获取视图
20
+ * @param options 视图配置
21
+ * @param options.center 中心点
22
+ * @param options.zoom 缩放级别
23
+ * @param options.minZoom 最小缩放级别
24
+ * @param options.maxZoom 最大缩放级别
25
+ * @param options.extent 视图范围
26
+ * @returns View
27
+ */
28
+ static getView(options?: MapInitType): View;
29
+ /**
30
+ * 获取 地图 面 操作
31
+ * @returns Polygon
32
+ */
33
+ getPolygon(): Polygon;
34
+ getMapBaseLayers(): any;
35
+ /**
36
+ * 设置底图
37
+ */
38
+ setMapLayer(type?: MapbaseType): void;
39
+ setZhujiMapLayer(show: boolean): void;
40
+ /**
41
+ * 设置地图边界
42
+ */
43
+ addPolygonToMap(data: any, type: string | undefined, options: OptionsType): void;
44
+ /**
45
+ * 设置 图片致地图
46
+ * @param layerName 图层名称
47
+ * @param img 图片地址
48
+ * @param extent 图片范围
49
+ * @param options 图片配置
50
+ */
51
+ setImgLayer(layerName: string, img: string, extent: number[], options?: {}): void;
52
+ /**
53
+ * 隐藏&展示图层
54
+ * @param layerName 图层名称
55
+ * @param show 是否显示
56
+ */
57
+ showMapLayer(layerName: string, show?: boolean): boolean;
58
+ /**
59
+ * 获取 地图 点 操作
60
+ * @returns Point
61
+ */
62
+ getPoint(): Point;
63
+ /**
64
+ * 获取 地图 线 操作
65
+ * @returns Line
66
+ */
67
+ getLine(): Line;
68
+ /**
69
+ * 设置闪烁点
70
+ * @param twinkleList 闪烁点数据
71
+ * @param className 闪烁点样式
72
+ * @param key 闪烁点索引
73
+ */
74
+ setFlashWarnPoint(twinkleList: any[], className: string, key: string): void;
75
+ /**
76
+ * 获取 地图 工具 操作
77
+ * @returns MapTools
78
+ */
79
+ getTools(): MapTools;
80
+ restPosition(duration?: number): void;
81
+ /**
82
+ * 地图定位
83
+ * @param lgtd 经度
84
+ * @param lttd 纬度
85
+ * @param zoom 缩放级别
86
+ * @param duration 动画时间
87
+ */
88
+ locationAction(lgtd: number, lttd: number, zoom?: number, duration?: number): void;
89
+ /**
90
+ * 地图监听事件
91
+ * @param eventType 事件类型
92
+ * @param clickType 点击类型
93
+ * @param callback 回调函数
94
+ */
95
+ mapOnEvent(eventType: string | undefined, callback: (feature?: any, e?: any) => void, clickType?: 'point' | 'line' | 'polygon' | undefined): void;
96
+ }
package/dist/MyOl.js ADDED
@@ -0,0 +1,248 @@
1
+ "use strict";
2
+ import { register as olProj4Register } from 'ol/proj/proj4';
3
+ import { Projection as olProjProjection, addProjection as olProjAddProjection, fromLonLat as olProjFromLonLat } from 'ol/proj';
4
+ import View from "ol/View";
5
+ import Map from "ol/Map";
6
+ import Polygon from "./core/Polygon";
7
+ import Point from "./core/Point";
8
+ import Line from "./core/Line";
9
+ import MapBaseLayers from "./core/MapBaseLayers";
10
+ import proj4 from "proj4";
11
+ import MapTools from "./core/MapTools";
12
+ import { defaults as defaultControls } from 'ol/control';
13
+ // import { Pixel } from "ol/pixel";
14
+ // import { FeatureLike } from "ol/Feature";
15
+ // import { MapBrowserEvent } from "ol";
16
+ class MyOl {
17
+ constructor(id, options) {
18
+ options.center = options.center || MyOl.DefaultOptions.center;
19
+ this.options = { ...MyOl.DefaultOptions, ...options };
20
+ this.map = new Map({
21
+ target: id, // 地图容器
22
+ view: MyOl.getView(this.options), // 视图
23
+ layers: this.options.layers || [],
24
+ controls: defaultControls({
25
+ zoom: false,
26
+ rotate: false,
27
+ attribution: false
28
+ }).extend([])
29
+ });
30
+ }
31
+ /**
32
+ * 获取视图
33
+ * @param options 视图配置
34
+ * @param options.center 中心点
35
+ * @param options.zoom 缩放级别
36
+ * @param options.minZoom 最小缩放级别
37
+ * @param options.maxZoom 最大缩放级别
38
+ * @param options.extent 视图范围
39
+ * @returns View
40
+ */
41
+ static getView(options = MyOl.DefaultOptions) {
42
+ proj4.defs("EPSG:4490", "+proj=longlat +ellps=GRS80 +no_defs");
43
+ proj4.defs("EPSG:4549", "+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs");
44
+ olProj4Register(proj4);
45
+ const cgsc2000 = new olProjProjection({
46
+ code: "EPSG:4490",
47
+ extent: [-180, -90, 180, 90],
48
+ worldExtent: [-180, -90, 180, 90],
49
+ units: "degrees"
50
+ });
51
+ olProjAddProjection(cgsc2000);
52
+ // debugger
53
+ // 视图配置
54
+ const viewOptions = {
55
+ projection: cgsc2000, // 坐标系
56
+ center: olProjFromLonLat(options.center, cgsc2000), // 中心点
57
+ zoom: options.zoom || 10, // 缩放级别
58
+ minZoom: options.minZoom || 8,
59
+ maxZoom: options.maxZoom || 20
60
+ };
61
+ if (options.extent)
62
+ viewOptions.extent = options.extent;
63
+ return new View(viewOptions);
64
+ }
65
+ // ╔══════════╗
66
+ // ║ 地图 面 ║
67
+ // ╚══════════╝
68
+ /**
69
+ * 获取 地图 面 操作
70
+ * @returns Polygon
71
+ */
72
+ getPolygon() {
73
+ if (!this.polygon)
74
+ this.polygon = new Polygon(this.map);
75
+ return this.polygon;
76
+ }
77
+ getMapBaseLayers() {
78
+ if (!this.baseLayers)
79
+ this.baseLayers = new MapBaseLayers(this.map, {
80
+ zIndex: 1,
81
+ mapClip: !!this.options.mapClipData,
82
+ mapClipData: this.options.mapClipData,
83
+ token: this.options.token || ''
84
+ });
85
+ return this.baseLayers;
86
+ }
87
+ /**
88
+ * 设置底图
89
+ */
90
+ setMapLayer(type = 'tianditu') {
91
+ // 添加影像底图
92
+ this.getMapBaseLayers().addMapLayer(type);
93
+ }
94
+ setZhujiMapLayer(show) {
95
+ this.getMapBaseLayers().addZhujiLayer(show);
96
+ }
97
+ /**
98
+ * 设置地图边界
99
+ */
100
+ addPolygonToMap(data, type = 'fuyangqu', options) {
101
+ this.getPolygon().addPolygonMapLayer(data, type, options);
102
+ }
103
+ /**
104
+ * 设置 图片致地图
105
+ * @param layerName 图层名称
106
+ * @param img 图片地址
107
+ * @param extent 图片范围
108
+ * @param options 图片配置
109
+ */
110
+ setImgLayer(layerName, img, extent, options = {}) {
111
+ this.getPolygon().addImgLayer(layerName, img, extent, options);
112
+ }
113
+ /**
114
+ * 隐藏&展示图层
115
+ * @param layerName 图层名称
116
+ * @param show 是否显示
117
+ */
118
+ showMapLayer(layerName, show = true) {
119
+ if (!layerName) {
120
+ console.error("缺少图层名称");
121
+ return false;
122
+ }
123
+ let layers = this.getTools().getLayerByLayerName(layerName);
124
+ if (!Array.isArray(layers))
125
+ layers = [layers];
126
+ // 无图层返回不继续操作
127
+ if (layers.length === 0)
128
+ return false;
129
+ const zoom = this.map.getView().getZoom();
130
+ for (const layer of layers) {
131
+ if (layer) {
132
+ if (layer.getVisible() === show)
133
+ return false; // 相同则不处理
134
+ const layerName = layer.values_.layerName;
135
+ let isMoreLayer = false;
136
+ // 若为打开,查找是否按层级显示的图层
137
+ if (show) {
138
+ for (let i = 2; i <= 5; i++) {
139
+ if (["reservoir_frgrd_" + i, "city_frgrd_" + i, "rain_frgrd_" + i].includes(layerName)) {
140
+ let minZoom = 10;
141
+ switch (i) {
142
+ case 2:
143
+ minZoom = 10;
144
+ break;
145
+ case 3:
146
+ minZoom = 11;
147
+ break;
148
+ case 4:
149
+ minZoom = 12;
150
+ break;
151
+ case 5:
152
+ minZoom = 13;
153
+ break;
154
+ }
155
+ isMoreLayer = true; // 有进入,单下面未进入则代表不显示与下面的continue使用
156
+ if (zoom && (zoom > minZoom && zoom < 20)) {
157
+ layer.setVisible(true);
158
+ break;
159
+ }
160
+ }
161
+ }
162
+ }
163
+ if (!isMoreLayer) {
164
+ layer.setVisible(show);
165
+ }
166
+ }
167
+ }
168
+ return true;
169
+ }
170
+ // ╔══════════╗
171
+ // ║ 地图 点 ║
172
+ // ╚══════════╝
173
+ /**
174
+ * 获取 地图 点 操作
175
+ * @returns Point
176
+ */
177
+ getPoint() {
178
+ if (!this.point)
179
+ this.point = new Point(this.map);
180
+ return this.point;
181
+ }
182
+ // ╔══════════╗
183
+ // ║ 地图 线 ║
184
+ // ╚══════════╝
185
+ /**
186
+ * 获取 地图 线 操作
187
+ * @returns Line
188
+ */
189
+ getLine() {
190
+ if (!this.line)
191
+ this.line = new Line(this.map);
192
+ return this.line;
193
+ }
194
+ /**
195
+ * 设置闪烁点
196
+ * @param twinkleList 闪烁点数据
197
+ * @param className 闪烁点样式
198
+ * @param key 闪烁点索引
199
+ */
200
+ setFlashWarnPoint(twinkleList, className, key) {
201
+ this.getPoint().setTwinkleLayer(twinkleList, className, key);
202
+ }
203
+ // ╔════════════╗
204
+ // ║ 地图 工具 ║
205
+ // ╚════════════╝
206
+ /**
207
+ * 获取 地图 工具 操作
208
+ * @returns MapTools
209
+ */
210
+ getTools() {
211
+ if (!this.mapTools)
212
+ this.mapTools = new MapTools(this.map);
213
+ return this.mapTools;
214
+ }
215
+ restPosition(duration = 3000) {
216
+ if (!this.options.center)
217
+ return console.error('未设置中心点');
218
+ this.locationAction(this.options.center[0], this.options.center[1], this.options.zoom, duration);
219
+ }
220
+ /**
221
+ * 地图定位
222
+ * @param lgtd 经度
223
+ * @param lttd 纬度
224
+ * @param zoom 缩放级别
225
+ * @param duration 动画时间
226
+ */
227
+ locationAction(lgtd, lttd, zoom = 20, duration = 3000) {
228
+ this.getPoint().locationAction(lgtd, lttd, zoom, duration);
229
+ }
230
+ /**
231
+ * 地图监听事件
232
+ * @param eventType 事件类型
233
+ * @param clickType 点击类型
234
+ * @param callback 回调函数
235
+ */
236
+ mapOnEvent(eventType = "def", callback, clickType) {
237
+ MapTools.mapOnEvent(this.map, eventType, callback, clickType);
238
+ }
239
+ }
240
+ MyOl.DefaultOptions = {
241
+ layers: undefined,
242
+ zoom: 10,
243
+ center: [119.81, 29.969],
244
+ minZoom: 8,
245
+ maxZoom: 20,
246
+ extent: undefined
247
+ };
248
+ export default MyOl;
@@ -1,4 +1,4 @@
1
- import MyOl from "../index";
1
+ import Map from "ol/Map";
2
2
  interface Options {
3
3
  Vue: any;
4
4
  Template: any;
@@ -10,11 +10,11 @@ interface Options {
10
10
  zIndex?: number;
11
11
  }
12
12
  export default class DomPoint {
13
- private myOlMap;
13
+ private map;
14
14
  private app;
15
15
  private anchor;
16
16
  private dom;
17
- constructor(map: MyOl, options: Options);
17
+ constructor(map: Map, options: Options);
18
18
  setVisible(visible: boolean): void;
19
19
  remove(): void;
20
20
  }
@@ -2,10 +2,10 @@
2
2
  import Overlay from "ol/Overlay";
3
3
  export default class DomPoint {
4
4
  constructor(map, options) {
5
- this.myOlMap = map;
5
+ this.map = map;
6
6
  const { Vue, Template, lgtd, lttd, props, } = options;
7
7
  this.dom = document.createElement('div');
8
- this.myOlMap.map.getViewport().appendChild(this.dom);
8
+ this.map.getViewport().appendChild(this.dom);
9
9
  if (Vue.version.startsWith('3')) {
10
10
  this.app = Vue.createApp(Object.assign(Template, {
11
11
  props: { ...props }
@@ -24,13 +24,13 @@ export default class DomPoint {
24
24
  stopEvent: false
25
25
  });
26
26
  this.anchor.setPosition([lgtd, lttd]);
27
- this.myOlMap.map.addOverlay(this.anchor);
27
+ this.map.addOverlay(this.anchor);
28
28
  }
29
29
  setVisible(visible) {
30
30
  this.dom.style.visibility = visible ? 'visible' : 'hidden';
31
31
  }
32
32
  remove() {
33
33
  this.app.unmount();
34
- this.myOlMap.map.removeOverlay(this.anchor);
34
+ this.map.removeOverlay(this.anchor);
35
35
  }
36
36
  }
@@ -1,19 +1,20 @@
1
1
  import Map from "ol/Map";
2
- import MyOl from "../index";
2
+ import VectorSource from "ol/source/Vector";
3
+ import VectorLayer from "ol/layer/Vector";
4
+ import { Style } from "ol/style";
3
5
  import { OptionsType, MapJSONData } from "../types";
4
6
  export default class Line {
5
7
  private map;
6
- private myOlMap;
7
8
  riverLayerList: any[];
8
9
  riverLayerShow: boolean;
9
10
  [propertyName: string]: any;
10
- constructor(myOlMap: MyOl, map: Map);
11
- addLineCommon(data: MapJSONData, options: OptionsType): any;
11
+ constructor(map: Map);
12
+ addLineCommon(data: MapJSONData, options: OptionsType): VectorLayer<VectorSource<import("ol/geom").Geometry>>;
12
13
  addRiverLayersByZoom(fyRiverJson: MapJSONData, options?: OptionsType): void;
13
14
  showRiverLayer(show: boolean): void;
14
15
  showRiverLayerByZoom(): void;
15
16
  addRiverWidthByLev(arr?: any): void;
16
17
  setFeatureAttr(feature: {
17
18
  get: (arg0: string) => any;
18
- }): any;
19
+ }): Style;
19
20
  }
package/dist/core/Line.js CHANGED
@@ -2,11 +2,11 @@ import VectorSource from "ol/source/Vector";
2
2
  import GeoJSON from "ol/format/GeoJSON";
3
3
  import VectorLayer from "ol/layer/Vector";
4
4
  import { Stroke, Style } from "ol/style";
5
+ import MapTools from "./MapTools";
5
6
  export default class Line {
6
- constructor(myOlMap, map) {
7
+ constructor(map) {
7
8
  this.riverLayerList = [];
8
9
  this.riverLayerShow = false;
9
- this.myOlMap = myOlMap;
10
10
  this.map = map;
11
11
  }
12
12
  addLineCommon(data, options) {
@@ -71,7 +71,7 @@ export default class Line {
71
71
  this.map.addLayer(riverLayer);
72
72
  }
73
73
  this.showRiverLayerByZoom();
74
- this.myOlMap.mapOnEvent('moveend', () => {
74
+ MapTools.mapOnEvent(this.map, 'moveend', () => {
75
75
  this.showRiverLayerByZoom();
76
76
  });
77
77
  }
@@ -2,6 +2,7 @@
2
2
  * 地图底图图层
3
3
  */
4
4
  import Map from "ol/Map";
5
+ import WMTSTileGrid from "ol/tilegrid/WMTS";
5
6
  import { MapbaseType, MapJSONData } from "../types";
6
7
  interface MapLayersOptions {
7
8
  zIndex?: number;
@@ -17,7 +18,7 @@ export default class MapBaseLayers {
17
18
  constructor(map: Map, options: MapLayersOptions);
18
19
  addMapLayer(type: MapbaseType): void;
19
20
  createLayer(layer: any): any;
20
- getTileGrid(length: number): any;
21
+ getTileGrid(length: number): WMTSTileGrid;
21
22
  getTiandiTuLayer(tiandituType?: 'img_c' | 'ter_c'): any;
22
23
  getTerrainLayer(): any;
23
24
  /**
@@ -13,4 +13,12 @@ export default class MapTools {
13
13
  */
14
14
  static setMapClip(baseLayer: any, data: MapJSONData): any;
15
15
  removeLayer(layerName: string): void;
16
+ /**
17
+ * 地图监听事件
18
+ * @param map
19
+ * @param eventType 事件类型
20
+ * @param clickType 点击类型
21
+ * @param callback 回调函数
22
+ */
23
+ static mapOnEvent(map: Map, eventType: string | undefined, callback: (feature?: any, e?: any) => void, clickType?: 'point' | 'line' | 'polygon' | undefined): void;
16
24
  }
@@ -63,4 +63,44 @@ export default class MapTools {
63
63
  removeLayer(layerName) {
64
64
  this.map.removeLayer(this.getLayerByLayerName(layerName));
65
65
  }
66
+ /**
67
+ * 地图监听事件
68
+ * @param map
69
+ * @param eventType 事件类型
70
+ * @param clickType 点击类型
71
+ * @param callback 回调函数
72
+ */
73
+ static mapOnEvent(map, eventType = "def", callback, clickType) {
74
+ const clickTypeObj = {
75
+ point: ['point'],
76
+ line: ['line'],
77
+ polygon: ['polygon', 'MultiPolygon']
78
+ };
79
+ if (eventType === "click") {
80
+ map.on("click", (e) => {
81
+ // 获取点位 feature
82
+ const pixel = map.getEventPixel(e.originalEvent);
83
+ const features = map.getFeaturesAtPixel(pixel);
84
+ let feature = undefined;
85
+ if (features.length > 0)
86
+ feature = features[0];
87
+ callback(feature, { features, pixel });
88
+ });
89
+ }
90
+ else if (eventType === 'moveend') {
91
+ map.on('moveend', function () {
92
+ const zoom = map.getView().getZoom();
93
+ if (zoom) {
94
+ callback(zoom);
95
+ }
96
+ });
97
+ }
98
+ else if (eventType === 'hover') {
99
+ map.on('pointermove', (e) => {
100
+ const pixel = map.getEventPixel(e.originalEvent);
101
+ const features = map.getFeaturesAtPixel(pixel);
102
+ callback({ features, pixel });
103
+ });
104
+ }
105
+ }
66
106
  }
@@ -1,10 +1,10 @@
1
1
  import Map from "ol/Map";
2
- import MyOl from "../index";
2
+ import VectorLayer from "ol/layer/Vector";
3
+ import VectorSource from "ol/source/Vector";
3
4
  import { OptionsType, PointData } from '../types';
4
5
  export default class Point {
5
- private myOlMap;
6
6
  private map;
7
- constructor(myOlMap: MyOl, map: Map);
7
+ constructor(map: Map);
8
8
  /**
9
9
  *
10
10
  * @param pointData
@@ -15,7 +15,7 @@ export default class Point {
15
15
  * hasImg: Boolean 是否显示图标
16
16
  * }
17
17
  */
18
- addPoint(pointData: PointData[], type: string, options: OptionsType): any;
18
+ addPoint(pointData: PointData[], type: string, options: OptionsType): VectorLayer<VectorSource<import("ol/geom").Geometry>>;
19
19
  addClusterPoint(pointData: any[], type: string | undefined, options: OptionsType): void;
20
20
  /**
21
21
  * 添加点 - 闪烁
@@ -43,7 +43,7 @@ export default class Point {
43
43
  * 设置dom元素为点位
44
44
  */
45
45
  setDomPoint(id: string, lgtd: number, lttd: number): void;
46
- setDomPointVue(pointInfoList: any[], template: any): {
46
+ setDomPointVue(pointInfoList: any[], template: any, Vue: any): {
47
47
  setVisible: (visible: boolean) => void;
48
48
  remove: () => void;
49
49
  };
@@ -10,9 +10,9 @@ import { Cluster } from 'ol/source';
10
10
  import * as turf from 'turf';
11
11
  import GeoJSON from "ol/format/GeoJSON";
12
12
  import DomPoint from "./DomPoint";
13
+ import MapTools from "./MapTools";
13
14
  export default class Point {
14
- constructor(myOlMap, map) {
15
- this.myOlMap = myOlMap;
15
+ constructor(map) {
16
16
  this.map = map;
17
17
  }
18
18
  /**
@@ -142,7 +142,7 @@ export default class Point {
142
142
  }
143
143
  // 在流域中心添加闪烁点位
144
144
  setTwinkleLayerFromPolygon(twinkleList, className, key, json) {
145
- this.myOlMap.getTools().removeLayer('twinklePoint');
145
+ new MapTools(this.map).removeLayer('twinklePoint');
146
146
  // 计算多边形的中心点坐标
147
147
  const calculatePolygonCenter = (polygonCoordinates) => {
148
148
  const polygon = turf.polygon(polygonCoordinates[0]);
@@ -272,9 +272,10 @@ export default class Point {
272
272
  this.map.addOverlay(anchor);
273
273
  }
274
274
  }
275
- setDomPointVue(pointInfoList, template) {
275
+ setDomPointVue(pointInfoList, template, Vue) {
276
276
  const layer = pointInfoList.map((pointInfo) => {
277
- return new DomPoint(this.myOlMap, {
277
+ return new DomPoint(this.map, {
278
+ Vue,
278
279
  Template: template,
279
280
  lgtd: pointInfo.lgtd,
280
281
  lttd: pointInfo.lttd,
@@ -1,12 +1,13 @@
1
1
  import Map from "ol/Map";
2
- import MyOl from "../index";
2
+ import VectorLayer from "ol/layer/Vector";
3
+ import VectorSource from "ol/source/Vector";
4
+ import { Geometry } from "ol/geom";
3
5
  import { OptionsType, MapJSONData } from '../types';
4
6
  export default class Polygon {
5
- private myOlMap;
6
7
  map: Map;
7
8
  private colorMap;
8
9
  [key: string]: any;
9
- constructor(myOlMap: MyOl, map: Map);
10
+ constructor(map: Map);
10
11
  /**
11
12
  * 获取等级颜色
12
13
  * @param lev
@@ -19,7 +20,7 @@ export default class Polygon {
19
20
  * @param options 图层配置
20
21
  */
21
22
  addPolygonMapLayer(data: MapJSONData, type: string | undefined, options: OptionsType): void;
22
- addPolygonLayerCommon(dataJSON: MapJSONData, options?: OptionsType): any;
23
+ addPolygonLayerCommon(dataJSON: MapJSONData, options?: OptionsType): VectorLayer<VectorSource<Geometry>>;
23
24
  /**
24
25
  * 根据数据数组更新某个面颜色
25
26
  * @param layerName 图层名称
@@ -10,14 +10,13 @@ import Feature from "ol/Feature";
10
10
  import ImageStatic from "ol/source/ImageStatic";
11
11
  import MapTools from "./MapTools";
12
12
  export default class Polygon {
13
- constructor(myOlMap, map) {
13
+ constructor(map) {
14
14
  this.colorMap = {
15
15
  '0': 'rgba(255, 0, 0, 0.6)',
16
16
  '1': 'rgba(245, 154, 35, 0.6)',
17
17
  '2': 'rgba(255, 238, 0, 0.6)',
18
18
  '3': 'rgba(1, 111, 255, 0.6)'
19
19
  };
20
- this.myOlMap = myOlMap;
21
20
  this.map = map;
22
21
  }
23
22
  /**
@@ -71,7 +70,7 @@ export default class Polygon {
71
70
  //fyBasinJson中的id的key需要跟options中的nameKey一致
72
71
  addPolygonLayerCommon(dataJSON, options = {}) {
73
72
  if (options.type != null) {
74
- this.myOlMap.getTools().removeLayer(options.type);
73
+ new MapTools(this.map).removeLayer(options.type);
75
74
  }
76
75
  const layer = new VectorLayer({
77
76
  name: options.type,
@@ -283,7 +282,7 @@ export default class Polygon {
283
282
  return this[layerName];
284
283
  }
285
284
  removePolygonLayer(layerName) {
286
- this.myOlMap.getTools().removeLayer(layerName);
285
+ new MapTools(this.map).removeLayer(layerName);
287
286
  this[layerName] = null;
288
287
  }
289
288
  }
package/dist/index.d.ts CHANGED
@@ -1,95 +1,9 @@
1
- import Map from "ol/Map";
2
- import Polygon from "./core/Polygon";
3
- import Point from "./core/Point";
1
+ import MyOl from "./MyOl";
4
2
  import Line from "./core/Line";
3
+ import Point from "./core/Point";
5
4
  import MapTools from "./core/MapTools";
6
- import { OptionsType, MapInitType, MapbaseType } from './types';
7
- export default class MyOl {
8
- map: Map;
9
- private baseLayers;
10
- private polygon;
11
- private mapTools;
12
- private point;
13
- private line;
14
- private readonly options;
15
- static DefaultOptions: MapInitType;
16
- constructor(id: string, options: MapInitType);
17
- /**
18
- * 获取视图
19
- * @param options 视图配置
20
- * @param options.center 中心点
21
- * @param options.zoom 缩放级别
22
- * @param options.minZoom 最小缩放级别
23
- * @param options.maxZoom 最大缩放级别
24
- * @param options.extent 视图范围
25
- * @returns View
26
- */
27
- static getView(options?: MapInitType): any;
28
- /**
29
- * 获取 地图 面 操作
30
- * @returns Polygon
31
- */
32
- getPolygon(): Polygon;
33
- getMapBaseLayers(): any;
34
- /**
35
- * 设置底图
36
- */
37
- setMapLayer(type?: MapbaseType): void;
38
- setZhujiMapLayer(show: boolean): void;
39
- /**
40
- * 设置地图边界
41
- */
42
- addPolygonToMap(data: any, type: string | undefined, options: OptionsType): void;
43
- /**
44
- * 设置 图片致地图
45
- * @param layerName 图层名称
46
- * @param img 图片地址
47
- * @param extent 图片范围
48
- * @param options 图片配置
49
- */
50
- setImgLayer(layerName: string, img: string, extent: number[], options?: {}): void;
51
- /**
52
- * 隐藏&展示图层
53
- * @param layerName 图层名称
54
- * @param show 是否显示
55
- */
56
- showMapLayer(layerName: string, show?: boolean): boolean;
57
- /**
58
- * 获取 地图 点 操作
59
- * @returns Point
60
- */
61
- getPoint(): Point;
62
- /**
63
- * 获取 地图 线 操作
64
- * @returns Line
65
- */
66
- getLine(): Line;
67
- /**
68
- * 设置闪烁点
69
- * @param twinkleList 闪烁点数据
70
- * @param className 闪烁点样式
71
- * @param key 闪烁点索引
72
- */
73
- setFlashWarnPoint(twinkleList: any[], className: string, key: string): void;
74
- /**
75
- * 获取 地图 工具 操作
76
- * @returns MapTools
77
- */
78
- getTools(): MapTools;
79
- restPosition(duration?: number): void;
80
- /**
81
- * 地图定位
82
- * @param lgtd 经度
83
- * @param lttd 纬度
84
- * @param zoom 缩放级别
85
- * @param duration 动画时间
86
- */
87
- locationAction(lgtd: number, lttd: number, zoom?: number, duration?: number): void;
88
- /**
89
- * 地图监听事件
90
- * @param eventType 事件类型
91
- * @param clickType 点击类型
92
- * @param callback 回调函数
93
- */
94
- mapOnEvent(eventType: string | undefined, callback: (feature?: any, e?: any) => void, clickType?: 'point' | 'line' | 'polygon' | undefined): void;
95
- }
5
+ import Polygon from "./core/Polygon";
6
+ import MapBaseLayers from "./core/MapBaseLayers";
7
+ import DomPoint from "./core/DomPoint";
8
+ export { Line, Point, MapTools, Polygon, MapBaseLayers, DomPoint };
9
+ export default MyOl;
package/dist/index.js CHANGED
@@ -1,276 +1,9 @@
1
- "use strict";
2
- import { register as olProj4Register } from 'ol/proj/proj4';
3
- import { Projection as olProjProjection, addProjection as olProjAddProjection, fromLonLat as olProjFromLonLat } from 'ol/proj';
4
- import View from "ol/View";
5
- import Map from "ol/Map";
6
- import Polygon from "./core/Polygon";
7
- import Point from "./core/Point";
1
+ import MyOl from "./MyOl";
8
2
  import Line from "./core/Line";
9
- import MapBaseLayers from "./core/MapBaseLayers";
10
- import proj4 from "proj4";
3
+ import Point from "./core/Point";
11
4
  import MapTools from "./core/MapTools";
12
- import { defaults as defaultControls } from 'ol/control';
13
- class MyOl {
14
- constructor(id, options) {
15
- options.center = options.center || MyOl.DefaultOptions.center;
16
- this.options = { ...MyOl.DefaultOptions, ...options };
17
- this.map = new Map({
18
- target: id, // 地图容器
19
- view: MyOl.getView(this.options), // 视图
20
- layers: this.options.layers || [],
21
- controls: defaultControls({
22
- zoom: false,
23
- rotate: false,
24
- attribution: false
25
- }).extend([])
26
- });
27
- }
28
- /**
29
- * 获取视图
30
- * @param options 视图配置
31
- * @param options.center 中心点
32
- * @param options.zoom 缩放级别
33
- * @param options.minZoom 最小缩放级别
34
- * @param options.maxZoom 最大缩放级别
35
- * @param options.extent 视图范围
36
- * @returns View
37
- */
38
- static getView(options = MyOl.DefaultOptions) {
39
- proj4.defs("EPSG:4490", "+proj=longlat +ellps=GRS80 +no_defs");
40
- proj4.defs("EPSG:4549", "+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs");
41
- olProj4Register(proj4);
42
- const cgsc2000 = new olProjProjection({
43
- code: "EPSG:4490",
44
- extent: [-180, -90, 180, 90],
45
- worldExtent: [-180, -90, 180, 90],
46
- units: "degrees"
47
- });
48
- olProjAddProjection(cgsc2000);
49
- // debugger
50
- // 视图配置
51
- const viewOptions = {
52
- projection: cgsc2000, // 坐标系
53
- center: olProjFromLonLat(options.center, cgsc2000), // 中心点
54
- zoom: options.zoom || 10, // 缩放级别
55
- minZoom: options.minZoom || 8,
56
- maxZoom: options.maxZoom || 20
57
- };
58
- if (options.extent)
59
- viewOptions.extent = options.extent;
60
- return new View(viewOptions);
61
- }
62
- // ╔══════════╗
63
- // ║ 地图 面 ║
64
- // ╚══════════╝
65
- /**
66
- * 获取 地图 面 操作
67
- * @returns Polygon
68
- */
69
- getPolygon() {
70
- if (!this.polygon)
71
- this.polygon = new Polygon(this, this.map);
72
- return this.polygon;
73
- }
74
- getMapBaseLayers() {
75
- if (!this.baseLayers)
76
- this.baseLayers = new MapBaseLayers(this.map, {
77
- zIndex: 1,
78
- mapClip: !!this.options.mapClipData,
79
- mapClipData: this.options.mapClipData,
80
- token: this.options.token || ''
81
- });
82
- return this.baseLayers;
83
- }
84
- /**
85
- * 设置底图
86
- */
87
- setMapLayer(type = 'tianditu') {
88
- // 添加影像底图
89
- this.getMapBaseLayers().addMapLayer(type);
90
- }
91
- setZhujiMapLayer(show) {
92
- this.getMapBaseLayers().addZhujiLayer(show);
93
- }
94
- /**
95
- * 设置地图边界
96
- */
97
- addPolygonToMap(data, type = 'fuyangqu', options) {
98
- this.getPolygon().addPolygonMapLayer(data, type, options);
99
- }
100
- /**
101
- * 设置 图片致地图
102
- * @param layerName 图层名称
103
- * @param img 图片地址
104
- * @param extent 图片范围
105
- * @param options 图片配置
106
- */
107
- setImgLayer(layerName, img, extent, options = {}) {
108
- this.getPolygon().addImgLayer(layerName, img, extent, options);
109
- }
110
- /**
111
- * 隐藏&展示图层
112
- * @param layerName 图层名称
113
- * @param show 是否显示
114
- */
115
- showMapLayer(layerName, show = true) {
116
- if (!layerName) {
117
- console.error("缺少图层名称");
118
- return false;
119
- }
120
- let layers = this.getTools().getLayerByLayerName(layerName);
121
- if (!Array.isArray(layers))
122
- layers = [layers];
123
- // 无图层返回不继续操作
124
- if (layers.length === 0)
125
- return false;
126
- const zoom = this.map.getView().getZoom();
127
- for (const layer of layers) {
128
- if (layer) {
129
- if (layer.getVisible() === show)
130
- return false; // 相同则不处理
131
- const layerName = layer.values_.layerName;
132
- let isMoreLayer = false;
133
- // 若为打开,查找是否按层级显示的图层
134
- if (show) {
135
- for (let i = 2; i <= 5; i++) {
136
- if (["reservoir_frgrd_" + i, "city_frgrd_" + i, "rain_frgrd_" + i].includes(layerName)) {
137
- let minZoom = 10;
138
- switch (i) {
139
- case 2:
140
- minZoom = 10;
141
- break;
142
- case 3:
143
- minZoom = 11;
144
- break;
145
- case 4:
146
- minZoom = 12;
147
- break;
148
- case 5:
149
- minZoom = 13;
150
- break;
151
- }
152
- isMoreLayer = true; // 有进入,单下面未进入则代表不显示与下面的continue使用
153
- if (zoom && (zoom > minZoom && zoom < 20)) {
154
- layer.setVisible(true);
155
- break;
156
- }
157
- }
158
- }
159
- }
160
- if (!isMoreLayer) {
161
- layer.setVisible(show);
162
- }
163
- }
164
- }
165
- return true;
166
- }
167
- // ╔══════════╗
168
- // ║ 地图 点 ║
169
- // ╚══════════╝
170
- /**
171
- * 获取 地图 点 操作
172
- * @returns Point
173
- */
174
- getPoint() {
175
- if (!this.point)
176
- this.point = new Point(this, this.map);
177
- return this.point;
178
- }
179
- // ╔══════════╗
180
- // ║ 地图 线 ║
181
- // ╚══════════╝
182
- /**
183
- * 获取 地图 线 操作
184
- * @returns Line
185
- */
186
- getLine() {
187
- if (!this.line)
188
- this.line = new Line(this, this.map);
189
- return this.line;
190
- }
191
- /**
192
- * 设置闪烁点
193
- * @param twinkleList 闪烁点数据
194
- * @param className 闪烁点样式
195
- * @param key 闪烁点索引
196
- */
197
- setFlashWarnPoint(twinkleList, className, key) {
198
- this.getPoint().setTwinkleLayer(twinkleList, className, key);
199
- }
200
- // ╔════════════╗
201
- // ║ 地图 工具 ║
202
- // ╚════════════╝
203
- /**
204
- * 获取 地图 工具 操作
205
- * @returns MapTools
206
- */
207
- getTools() {
208
- if (!this.mapTools)
209
- this.mapTools = new MapTools(this.map);
210
- return this.mapTools;
211
- }
212
- restPosition(duration = 3000) {
213
- if (!this.options.center)
214
- return console.error('未设置中心点');
215
- this.locationAction(this.options.center[0], this.options.center[1], this.options.zoom, duration);
216
- }
217
- /**
218
- * 地图定位
219
- * @param lgtd 经度
220
- * @param lttd 纬度
221
- * @param zoom 缩放级别
222
- * @param duration 动画时间
223
- */
224
- locationAction(lgtd, lttd, zoom = 20, duration = 3000) {
225
- this.getPoint().locationAction(lgtd, lttd, zoom, duration);
226
- }
227
- /**
228
- * 地图监听事件
229
- * @param eventType 事件类型
230
- * @param clickType 点击类型
231
- * @param callback 回调函数
232
- */
233
- mapOnEvent(eventType = "def", callback, clickType) {
234
- const clickTypeObj = {
235
- point: ['point'],
236
- line: ['line'],
237
- polygon: ['polygon', 'MultiPolygon']
238
- };
239
- const map = this.map;
240
- if (eventType === "click") {
241
- map.on("click", (e) => {
242
- // 获取点位 feature
243
- const pixel = map.getEventPixel(e.originalEvent);
244
- const features = map.getFeaturesAtPixel(pixel);
245
- let feature = undefined;
246
- if (features.length > 0)
247
- feature = features[0];
248
- callback(feature, { features, pixel });
249
- });
250
- }
251
- else if (eventType === 'moveend') {
252
- map.on('moveend', function () {
253
- const zoom = map.getView().getZoom();
254
- if (zoom) {
255
- callback(zoom);
256
- }
257
- });
258
- }
259
- else if (eventType === 'hover') {
260
- map.on('pointermove', (e) => {
261
- const pixel = map.getEventPixel(e.originalEvent);
262
- const features = map.getFeaturesAtPixel(pixel);
263
- callback({ features, pixel });
264
- });
265
- }
266
- }
267
- }
268
- MyOl.DefaultOptions = {
269
- layers: undefined,
270
- zoom: 10,
271
- center: [119.81, 29.969],
272
- minZoom: 8,
273
- maxZoom: 20,
274
- extent: undefined
275
- };
5
+ import Polygon from "./core/Polygon";
6
+ import MapBaseLayers from "./core/MapBaseLayers";
7
+ import DomPoint from "./core/DomPoint";
8
+ export { Line, Point, MapTools, Polygon, MapBaseLayers, DomPoint };
276
9
  export default MyOl;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "0.0.15",
4
+ "version": "0.0.17",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",