forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapvLayer.js
More file actions
208 lines (190 loc) · 7.1 KB
/
MapvLayer.js
File metadata and controls
208 lines (190 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import mapboxgl from 'mapbox-gl';
import '../core/Base';
import { MapvRenderer } from './mapv/MapvRenderer';
import { CommonUtil } from '@supermap/iclient-common';
/**
* @class mapboxgl.supermap.MapvLayer
* @category Visualization MapV
* @classdesc Mapv 图层。
* @param {mapboxgl.Map} map - mapboxgl 地图对象,将在下个版本弃用,请用 map.addLayer() 方法添加图层。
* @param {Mapv.DataSet} dataSet - MapV 图层数据集。
* @param {Object} mapVOptions - Mapv 参数。
* @param {string} [mapVOptions.layerID] - 图层 ID。默认使用 CommonUtil.createUniqueID("mapvLayer_") 创建专题图层 ID。
*/
export class MapvLayer {
constructor(map, dataSet, mapVOptions) {
this.map = map;
this.id = mapVOptions.layerID ? mapVOptions.layerID : CommonUtil.createUniqueID('mapvLayer_');
delete mapVOptions['layerID'];
this.mapVOptions = mapVOptions;
this.dataSet = dataSet;
this.visibility = true;
//保留之前的用法
if (this.map) {
this.map.addLayer(this);
}
}
onAdd(map) {
this.map = map;
this.renderer = new MapvRenderer(this.map, this, this.dataSet, this.mapVOptions);
this.canvas = this._createCanvas();
this.renderer._canvasUpdate();
this.mapContainer = map.getCanvasContainer();
this.mapContainer.appendChild(this.canvas);
this.mapContainer.style.perspective = this.map.transform.cameraToCenterDistance + 'px';
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.removeFromMap
* @description 移除图层。
*/
removeFromMap() {
this.mapContainer.removeChild(this.canvas);
this.renderer.destroy();
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.setVisibility
* @description 设置图层可见性,设置图层的隐藏,显示,重绘的相应的可见标记。
* @param {boolean} [visibility] - 是否显示图层(当前地图的 resolution 在最大最小 resolution 之间)。
*/
setVisibility(visibility) {
if (visibility !== this.visibility) {
this.visibility = visibility;
if (visibility) {
this.show();
} else {
this.hide();
}
}
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.moveTo
* @description 将图层移动到某个图层之前。
* @param {string} layerID - 待插入的图层 ID。
* @param {boolean} [before=true] - 是否将本图层插入到图层 id 为 layerID 的图层之前(如果为 false 则将本图层插入到图层 id 为 layerID 的图层之后)。
*/
moveTo(layerID, before) {
const layer = document.getElementById(this.canvas.id);
before = before !== undefined ? before : true;
if (before) {
const beforeLayer = document.getElementById(layerID);
if (layer && beforeLayer) {
beforeLayer.parentNode.insertBefore(layer, beforeLayer);
}
return;
}
const nextLayer = document.getElementById(layerID);
if (layer) {
if (nextLayer.nextSibling) {
nextLayer.parentNode.insertBefore(layer, nextLayer.nextSibling);
return;
}
nextLayer.parentNode.appendChild(layer);
}
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.getTopLeft
* @description 获取左上的距离。
*/
getTopLeft() {
var map = this.map;
var topLeft;
if (map) {
var bounds = map.getBounds();
topLeft = bounds.getNorthWest();
}
return topLeft;
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.addData
* @description 追加数据。
* @param {Object} data - 要追加的数据。
* @param {Object} options - 要追加的值。
*/
addData(data, options) {
this.renderer.addData(data, options);
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.update
* @description 更新图层。
* @param {Object} opt - 待更新的数据。
* @param {Object} opt.data - mapv 数据集。
* @param {Object} opt.options - mapv 绘制参数。
*/
update(opt) {
this.renderer.update(opt);
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.getData
* @description 获取数据。
* @returns {mapv.DataSet} mapv 数据集。
*/
getData() {
if (this.renderer) {
this.dataSet = this.renderer.getData();
}
return this.dataSet;
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.removeData
* @description 删除符合过滤条件的数据。
* @param {function} [filter] - 过滤条件。条件参数为数据项,返回值为 true,表示删除该元素;否则表示不删除。
* @example
* filter=function(data){
* if(data.id=="1"){
* return true
* }
* return false;
* }
*/
removeData(filter) {
this.renderer && this.renderer.removeData(filter);
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.clearData
* @description 清除数据。
*/
clearData() {
this.renderer.clearData();
}
show() {
if (this.renderer) {
this.renderer._show();
}
return this;
}
hide() {
if (this.renderer) {
this.renderer._hide();
}
return this;
}
_createCanvas() {
var canvas = document.createElement('canvas');
canvas.id = this.id;
canvas.style.position = 'absolute';
canvas.style.top = 0 + 'px';
canvas.style.left = 0 + 'px';
var global$2 = typeof window === 'undefined' ? {} : window;
var devicePixelRatio = this.devicePixelRatio = global$2.devicePixelRatio || 1;
canvas.width = parseInt(this.map.getCanvas().style.width) * devicePixelRatio;
canvas.height = parseInt(this.map.getCanvas().style.height) * devicePixelRatio;
if (!this.mapVOptions.context || this.mapVOptions.context == '2d') {
canvas.getContext('2d').scale(devicePixelRatio, devicePixelRatio);
}
canvas.style.width = this.map.getCanvas().style.width;
canvas.style.height = this.map.getCanvas().style.height;
return canvas;
}
/**
* @function mapboxgl.supermap.MapvLayer.prototype.setZIndex
* @description 设置 canvas 层级。
* @param {number} zIndex - canvas 层级。
*/
setZIndex(z) {
this.canvas.style.zIndex = z;
}
}
mapboxgl.supermap.MapvLayer = MapvLayer;