my-openlayer 0.0.18 → 0.1.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/dist/MyOl.d.ts CHANGED
@@ -4,7 +4,7 @@ import Polygon from "./core/Polygon";
4
4
  import Point from "./core/Point";
5
5
  import Line from "./core/Line";
6
6
  import MapTools from "./core/MapTools";
7
- import { OptionsType, MapInitType, MapbaseType } from './types';
7
+ import { MapInitType } from './types';
8
8
  export default class MyOl {
9
9
  map: Map;
10
10
  private baseLayers;
@@ -32,29 +32,6 @@ export default class MyOl {
32
32
  */
33
33
  getPolygon(): Polygon;
34
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
35
  /**
59
36
  * 获取 地图 点 操作
60
37
  * @returns Point
@@ -65,13 +42,6 @@ export default class MyOl {
65
42
  * @returns Line
66
43
  */
67
44
  getLine(): Line;
68
- /**
69
- * 设置闪烁点
70
- * @param twinkleList 闪烁点数据
71
- * @param className 闪烁点样式
72
- * @param key 闪烁点索引
73
- */
74
- setFlashWarnPoint(twinkleList: any[], className: string, key: string): void;
75
45
  /**
76
46
  * 获取 地图 工具 操作
77
47
  * @returns MapTools
@@ -93,4 +63,10 @@ export default class MyOl {
93
63
  * @param callback 回调函数
94
64
  */
95
65
  mapOnEvent(eventType: string | undefined, callback: (feature?: any, e?: any) => void, clickType?: 'point' | 'line' | 'polygon' | undefined): void;
66
+ /**
67
+ * 隐藏&展示图层
68
+ * @param layerName 图层名称
69
+ * @param show 是否显示
70
+ */
71
+ showMapLayer(layerName: string, show?: boolean): boolean;
96
72
  }
package/dist/MyOl.js CHANGED
@@ -17,10 +17,16 @@ class MyOl {
17
17
  constructor(id, options) {
18
18
  options.center = options.center || MyOl.DefaultOptions.center;
19
19
  this.options = { ...MyOl.DefaultOptions, ...options };
20
+ let layers = [];
21
+ if (Array.isArray(options.layers)) {
22
+ layers = options.layers;
23
+ }
24
+ else {
25
+ }
20
26
  this.map = new Map({
21
27
  target: id, // 地图容器
22
28
  view: MyOl.getView(this.options), // 视图
23
- layers: this.options.layers || [],
29
+ layers: layers,
24
30
  controls: defaultControls({
25
31
  zoom: false,
26
32
  rotate: false,
@@ -75,40 +81,80 @@ class MyOl {
75
81
  return this.polygon;
76
82
  }
77
83
  getMapBaseLayers() {
84
+ if (Array.isArray(this.options.layers)) {
85
+ console.warn('已设置默认底图,MapBaseLayers中的switchBaseLayer方法将失效');
86
+ }
87
+ const options = {
88
+ layers: this.options.layers,
89
+ annotation: this.options.annotation,
90
+ zIndex: 1,
91
+ mapClip: !!this.options.mapClipData,
92
+ mapClipData: this.options.mapClipData,
93
+ token: this.options.token || ''
94
+ };
78
95
  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
- });
96
+ this.baseLayers = new MapBaseLayers(this.map, options);
85
97
  return this.baseLayers;
86
98
  }
99
+ // ╔══════════╗
100
+ // ║ 地图 点 ║
101
+ // ╚══════════╝
87
102
  /**
88
- * 设置底图
103
+ * 获取 地图 点 操作
104
+ * @returns Point
89
105
  */
90
- setMapLayer(type = 'tianditu') {
91
- // 添加影像底图
92
- this.getMapBaseLayers().addMapLayer(type);
106
+ getPoint() {
107
+ if (!this.point)
108
+ this.point = new Point(this.map);
109
+ return this.point;
93
110
  }
94
- setZhujiMapLayer(show) {
95
- this.getMapBaseLayers().addZhujiLayer(show);
111
+ // ╔══════════╗
112
+ // ║ 地图 线 ║
113
+ // ╚══════════╝
114
+ /**
115
+ * 获取 地图 线 操作
116
+ * @returns Line
117
+ */
118
+ getLine() {
119
+ if (!this.line)
120
+ this.line = new Line(this.map);
121
+ return this.line;
96
122
  }
123
+ // ╔════════════╗
124
+ // ║ 地图 工具 ║
125
+ // ╚════════════╝
97
126
  /**
98
- * 设置地图边界
127
+ * 获取 地图 工具 操作
128
+ * @returns MapTools
99
129
  */
100
- addPolygonToMap(data, type = 'fuyangqu', options) {
101
- this.getPolygon().addPolygonMapLayer(data, type, options);
130
+ getTools() {
131
+ if (!this.mapTools)
132
+ this.mapTools = new MapTools(this.map);
133
+ return this.mapTools;
134
+ }
135
+ restPosition(duration = 3000) {
136
+ if (!this.options.center)
137
+ return console.error('未设置中心点');
138
+ this.locationAction(this.options.center[0], this.options.center[1], this.options.zoom, duration);
102
139
  }
103
140
  /**
104
- * 设置 图片致地图
105
- * @param layerName 图层名称
106
- * @param img 图片地址
107
- * @param extent 图片范围
108
- * @param options 图片配置
141
+ * 地图定位
142
+ * @param lgtd 经度
143
+ * @param lttd 纬度
144
+ * @param zoom 缩放级别
145
+ * @param duration 动画时间
109
146
  */
110
- setImgLayer(layerName, img, extent, options = {}) {
111
- this.getPolygon().addImgLayer(layerName, img, extent, options);
147
+ locationAction(lgtd, lttd, zoom = 20, duration = 3000) {
148
+ this.getPoint().locationAction(lgtd, lttd, zoom, duration);
149
+ }
150
+ /**
151
+ * 地图监听事件
152
+ * @param eventType 事件类型
153
+ * @param clickType 点击类型
154
+ * @param callback 回调函数
155
+ */
156
+ mapOnEvent(eventType = "def", callback, clickType) {
157
+ MapTools.mapOnEvent(this.map, eventType, callback, clickType);
112
158
  }
113
159
  /**
114
160
  * 隐藏&展示图层
@@ -167,75 +213,6 @@ class MyOl {
167
213
  }
168
214
  return true;
169
215
  }
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
216
  }
240
217
  MyOl.DefaultOptions = {
241
218
  layers: undefined,
@@ -2,28 +2,31 @@
2
2
  * 地图底图图层
3
3
  */
4
4
  import Map from "ol/Map";
5
+ import { Tile as TileLayer } from "ol/layer";
6
+ import { WMTS } from "ol/source";
5
7
  import WMTSTileGrid from "ol/tilegrid/WMTS";
6
- import { MapbaseType, MapJSONData } from "../types";
7
- interface MapLayersOptions {
8
- zIndex?: number;
9
- mapClip?: boolean;
10
- mapClipData?: MapJSONData;
11
- token?: string;
12
- }
8
+ import XYZ from "ol/source/XYZ";
9
+ import { MapLayersOptions, TiandituType, AnnotationLayerOptions } from "../types";
13
10
  export default class MapBaseLayers {
14
11
  private map;
15
- private mapLayers;
16
12
  private readonly options;
17
- private zhujiMapLayer;
13
+ private readonly layers;
18
14
  constructor(map: Map, options: MapLayersOptions);
19
- addMapLayer(type: MapbaseType): void;
15
+ switchBaseLayer(type: TiandituType): void;
16
+ initLayer(): void;
17
+ addAnnotationLayer(options: AnnotationLayerOptions): void;
18
+ static addAnnotationLayer(map: Map, options: AnnotationLayerOptions): TileLayer<XYZ>;
19
+ addMapLayer(): void;
20
20
  createLayer(layer: any): any;
21
- getTileGrid(length: number): WMTSTileGrid;
22
- getTiandiTuLayer(tiandituType?: 'img_c' | 'ter_c'): any;
23
- getTerrainLayer(): any;
21
+ static getTiandiTuLayer(options: {
22
+ type: TiandituType;
23
+ token: string;
24
+ zIndex: number;
25
+ visible: boolean;
26
+ }): TileLayer<WMTS>;
24
27
  /**
25
28
  * 地图注记
26
29
  */
27
- addZhujiLayer(show: boolean): void;
30
+ static getAnnotationLayer(options: AnnotationLayerOptions): TileLayer<XYZ>;
31
+ static getTileGrid(length: number): WMTSTileGrid;
28
32
  }
29
- export {};
@@ -8,45 +8,96 @@ import { WMTS } from "ol/source";
8
8
  import WMTSTileGrid from "ol/tilegrid/WMTS";
9
9
  import XYZ from "ol/source/XYZ";
10
10
  import MapTools from "./MapTools";
11
- const LAYER_TYPES = {
12
- TIANDITU: 'tianditu',
13
- TERRAIN: 'terrain'
14
- };
15
11
  export default class MapBaseLayers {
16
12
  constructor(map, options) {
17
- this.mapLayers = {
18
- tianditu: null,
19
- shengting: null,
20
- terrain: null
21
- };
22
13
  this.options = {
23
14
  zIndex: 0,
15
+ annotation: false,
24
16
  mapClip: false,
25
17
  mapClipData: undefined,
26
- token: ''
27
18
  };
19
+ this.layers = {};
28
20
  this.map = map;
29
21
  this.options = { ...this.options, ...options };
30
- if (!this.options.token) {
31
- throw new Error('请配置token');
22
+ //如果没有配置底图,则默认使用天地图底图
23
+ if (!Array.isArray(this.options.layers)) {
24
+ this.layers = this.options.layers || {};
25
+ if (!this.options.token) {
26
+ throw new Error('请配置token后才能使用天地图底图');
27
+ }
28
+ else {
29
+ this.initLayer();
30
+ }
31
+ if (this.layers && Object.keys(this.layers).length !== 0) {
32
+ this.addMapLayer();
33
+ // this.switchBaseLayer(<string>Object.keys(this.layers)[0])
34
+ }
35
+ }
36
+ }
37
+ switchBaseLayer(type) {
38
+ if (Array.isArray(this.options.layers)) {
39
+ console.error('需要按照键值对的方式配置底图才可使用切换底图功能');
40
+ return;
32
41
  }
33
- else {
34
- this.addMapLayer('tianditu');
42
+ for (const key in this.layers) {
43
+ this.layers[key]?.forEach((layer) => {
44
+ layer.setVisible(key === type);
45
+ console.log(layer);
46
+ });
35
47
  }
36
48
  }
37
- addMapLayer(type) {
38
- const layerGenerators = {
39
- [LAYER_TYPES.TIANDITU]: this.getTiandiTuLayer,
40
- [LAYER_TYPES.TERRAIN]: this.getTerrainLayer
41
- };
42
- const layerGenerator = layerGenerators[type];
43
- if (layerGenerator) {
44
- this.mapLayers[type] = layerGenerator.call(this);
49
+ initLayer() {
50
+ if (this.layers && this.options.token) {
51
+ const { token, zIndex = 10 } = this.options;
52
+ this.layers.vec_c = [MapBaseLayers.getTiandiTuLayer({ type: 'vec_c', token, zIndex, visible: false })];
53
+ this.layers.img_c = [MapBaseLayers.getTiandiTuLayer({ type: 'img_c', token, zIndex, visible: false })];
54
+ this.layers.ter_c = [MapBaseLayers.getTiandiTuLayer({ type: 'ter_c', token, zIndex, visible: false })];
55
+ if (this.options.annotation) {
56
+ this.layers.vec_c.push(MapBaseLayers.getAnnotationLayer({
57
+ type: 'cva_c',
58
+ token,
59
+ zIndex: zIndex + 1,
60
+ visible: false
61
+ }));
62
+ this.layers.img_c.push(MapBaseLayers.getAnnotationLayer({
63
+ type: 'cia_c',
64
+ token,
65
+ zIndex: zIndex + 1,
66
+ visible: false
67
+ }));
68
+ this.layers.ter_c.push(MapBaseLayers.getAnnotationLayer({
69
+ type: 'cta_c',
70
+ token,
71
+ zIndex: zIndex + 1,
72
+ visible: false
73
+ }));
74
+ }
45
75
  }
46
- for (const key in this.mapLayers) {
47
- this.mapLayers[key] && this.mapLayers[key].setVisible(type === key);
76
+ }
77
+ //添加注记图层
78
+ addAnnotationLayer(options) {
79
+ MapBaseLayers.addAnnotationLayer(this.map, options);
80
+ }
81
+ //添加注记图层
82
+ static addAnnotationLayer(map, options) {
83
+ const layer = MapBaseLayers.getAnnotationLayer({
84
+ type: options.type,
85
+ token: options.token,
86
+ zIndex: options.zIndex,
87
+ visible: options.visible
88
+ });
89
+ map.addLayer(layer);
90
+ return layer;
91
+ }
92
+ addMapLayer() {
93
+ if (this.layers) {
94
+ for (const key in this.layers) {
95
+ this.layers[key]?.forEach((layer) => {
96
+ layer = this.createLayer(layer);
97
+ this.map.addLayer(layer);
98
+ });
99
+ }
48
100
  }
49
- this.mapLayers[type] && this.map.addLayer(this.mapLayers[type]);
50
101
  }
51
102
  createLayer(layer) {
52
103
  if (this.options.mapClip && this.options.mapClipData) {
@@ -54,29 +105,11 @@ export default class MapBaseLayers {
54
105
  }
55
106
  return layer;
56
107
  }
57
- getTileGrid(length) {
58
- const projection = getProjection('EPSG:4326');
59
- const projectionExtent = projection?.getExtent();
60
- const size = getWidth(projectionExtent) / 256;
61
- const resolutions = new Array(length);
62
- const matrixIds = new Array(length);
63
- for (let i = 0; i < length; i += 1) {
64
- const pow = Math.pow(2, i);
65
- resolutions[i] = size / pow;
66
- matrixIds[i] = i;
67
- }
68
- return new WMTSTileGrid({
69
- origin: getTopLeft(projectionExtent),
70
- resolutions,
71
- matrixIds
72
- });
73
- }
74
108
  //img_c 影像底图 ter_c 地形底图
75
- getTiandiTuLayer(tiandituType = 'img_c') {
76
- const layer = new TileLayer({
109
+ static getTiandiTuLayer(options) {
110
+ return new TileLayer({
77
111
  source: new WMTS({
78
- url: `https://t{0-7}.tianditu.gov.cn/${tiandituType}/wmts?tk=${this.options.token}`,
79
- // subdomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'],
112
+ url: `https://t{0-7}.tianditu.gov.cn/${options.type}/wmts?tk=${options.token}`,
80
113
  layer: 'img',
81
114
  style: 'default',
82
115
  matrixSet: 'c',
@@ -84,28 +117,38 @@ export default class MapBaseLayers {
84
117
  wrapX: true,
85
118
  tileGrid: this.getTileGrid(19)
86
119
  }),
87
- zIndex: this.options.zIndex
120
+ zIndex: options.zIndex,
121
+ visible: options.visible
88
122
  });
89
- return this.createLayer(layer);
90
- }
91
- getTerrainLayer() {
92
- return this.getTiandiTuLayer('ter_c');
93
123
  }
94
124
  /**
95
125
  * 地图注记
96
126
  */
97
- addZhujiLayer(show) {
98
- console.log('[注记加载]');
99
- if (!this.zhujiMapLayer) {
100
- this.zhujiMapLayer = new TileLayer({
101
- source: new XYZ({
102
- url: `http://t{0-7}.tianditu.gov.cn/DataServer?T=cia_c&tk=${this.options.token}&x={x}&y={y}&l={z}`,
103
- projection: 'EPSG:4326'
104
- }),
105
- zIndex: 11
106
- });
107
- this.map.addLayer(this.createLayer(this.zhujiMapLayer));
127
+ static getAnnotationLayer(options) {
128
+ return new TileLayer({
129
+ source: new XYZ({
130
+ url: `http://t{0-7}.tianditu.gov.cn/DataServer?T=${options.type}&tk=${options.token}&x={x}&y={y}&l={z}`,
131
+ projection: 'EPSG:4326'
132
+ }),
133
+ zIndex: options.zIndex,
134
+ visible: options.visible
135
+ });
136
+ }
137
+ static getTileGrid(length) {
138
+ const projection = getProjection('EPSG:4326');
139
+ const projectionExtent = projection?.getExtent();
140
+ const size = getWidth(projectionExtent) / 256;
141
+ const resolutions = new Array(length);
142
+ const matrixIds = new Array(length);
143
+ for (let i = 0; i < length; i += 1) {
144
+ const pow = Math.pow(2, i);
145
+ resolutions[i] = size / pow;
146
+ matrixIds[i] = i;
108
147
  }
109
- this.zhujiMapLayer.setVisible(show);
148
+ return new WMTSTileGrid({
149
+ origin: getTopLeft(projectionExtent),
150
+ resolutions,
151
+ matrixIds
152
+ });
110
153
  }
111
154
  }
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import BaseLayer from "ol/layer/Base";
1
2
  export interface Feature {
2
3
  type: string;
3
4
  properties: any;
@@ -12,7 +13,9 @@ export interface MapJSONData {
12
13
  features: Feature[];
13
14
  }
14
15
  export interface MapInitType {
15
- layers?: any[];
16
+ layers?: BaseLayer[] | {
17
+ [key: string]: BaseLayer[];
18
+ };
16
19
  zoom?: number;
17
20
  center?: number[];
18
21
  view?: any;
@@ -21,8 +24,30 @@ export interface MapInitType {
21
24
  extent?: number[];
22
25
  mapClipData?: MapJSONData;
23
26
  token?: string;
27
+ annotation?: boolean;
28
+ }
29
+ export type AnnotationType = 'cva_c' | 'cia_c' | 'cta_c';
30
+ export type TiandituType = 'vec_c' | 'img_c' | 'ter_c' | string;
31
+ export interface MapLayers {
32
+ vec_c?: BaseLayer[];
33
+ img_c?: BaseLayer[];
34
+ ter_c?: BaseLayer[];
35
+ [key: string]: BaseLayer[] | undefined;
36
+ }
37
+ export interface MapLayersOptions {
38
+ layers?: BaseLayer[] | MapLayers;
39
+ zIndex?: number;
40
+ mapClip?: boolean;
41
+ mapClipData?: MapJSONData;
42
+ token?: string;
43
+ annotation?: boolean;
44
+ }
45
+ export interface AnnotationLayerOptions {
46
+ type: AnnotationType;
47
+ token: string;
48
+ zIndex: number;
49
+ visible: boolean;
24
50
  }
25
- export type MapbaseType = 'tianditu' | 'shengting' | 'terrain';
26
51
  export interface OptionsType {
27
52
  type?: string;
28
53
  nameKey?: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "0.0.18",
4
+ "version": "0.1.0",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",