|
9 | 9 | * thanks dereklieu, cloudybay |
10 | 10 | */ |
11 | 11 |
|
12 | | -import { getWrapNum, conversionDegree } from '@supermap/iclient-common'; |
| 12 | +import { getWrapNum, conversionDegree, CommonUtil } from '@supermap/iclient-common'; |
13 | 13 | import mapboxgl from 'mapbox-gl'; |
14 | 14 | /** |
15 | 15 | * @class mapboxgl.supermap.GraticuleLayer |
16 | 16 | * @category Visualization GraticuleLayer |
17 | 17 | * @classdesc 经纬网。 |
18 | 18 | * @version 10.1.1 |
19 | 19 | * @param {Object} options -经纬网参数。 |
| 20 | + * @param {string} [options.layerID] - 图层 ID。默认使用 CommonUtil.createUniqueID("graticuleLayer_") 创建图层 ID。 |
20 | 21 | * @param {boolean} [options.visible=true] - 是否显示经纬网。 |
21 | 22 | * @param {boolean} [options.showLabel=true] - 是否显示标签。 |
22 | 23 | * @param {number} [options.opacity=1] - 画布透明度。 |
@@ -82,9 +83,10 @@ const defaultOptions = { |
82 | 83 | latLabelStyle: defaultTextStyle |
83 | 84 | }; |
84 | 85 | export class GraticuleLayer { |
85 | | - constructor(options, sourceId = 'sm-graticule-layer') { |
86 | | - this.canvasId = 'sm-graticule-canvasid'; |
87 | | - this.sourceId = sourceId; |
| 86 | + constructor(options) { |
| 87 | + this.id = options.layerID ? options.layerID : CommonUtil.createUniqueID('graticuleLayer_'); |
| 88 | + this.sourceId = this.id; |
| 89 | + this.canvasId = this.id; |
88 | 90 | this.options = options; |
89 | 91 | this.resetEvent = this._reset.bind(this); |
90 | 92 | this.styleDataEevent = this._setLayerTop.bind(this); |
@@ -118,7 +120,7 @@ export class GraticuleLayer { |
118 | 120 | * @param {boolean} visible - 是否可见。 |
119 | 121 | */ |
120 | 122 | setVisibility(visible) { |
121 | | - const zoom = this.map.getZoom(); |
| 123 | + const zoom = this.map && this.map.getZoom(); |
122 | 124 | this.options.visible = typeof visible === 'boolean' ? visible : this.options.visible; |
123 | 125 | this.visible = |
124 | 126 | typeof visible === 'boolean' |
@@ -178,6 +180,9 @@ export class GraticuleLayer { |
178 | 180 | * @param {mapboxgl.supermap.GraticuleLayer.StrokeStyle} strokeStyle - 经纬线样式。 |
179 | 181 | */ |
180 | 182 | setStrokeStyle(strokeStyle) { |
| 183 | + if (!this.map || !this.map.getLayer(this.sourceId)) { |
| 184 | + return; |
| 185 | + } |
181 | 186 | this.options.strokeStyle = strokeStyle; |
182 | 187 | const { layout, paint } = this._transformStrokeStyle(strokeStyle); |
183 | 188 | for (let key in layout) { |
@@ -251,6 +256,7 @@ export class GraticuleLayer { |
251 | 256 | this.oldExtent = this.options.extent; |
252 | 257 | this._calcInterval(); |
253 | 258 | this.isRotate = false; |
| 259 | + this.visible = true; |
254 | 260 | this.features = this._getGraticuleFeatures(); |
255 | 261 | } |
256 | 262 |
|
@@ -831,6 +837,7 @@ export class GraticuleLayer { |
831 | 837 | return { paint: { 'line-color': strokeStyle || 'rgba(0,0,0,0.2)' } }; |
832 | 838 | } |
833 | 839 | const layout = { |
| 840 | + visibility: this.visible ? 'visible' : 'none', |
834 | 841 | 'line-join': strokeStyle.lineJoin || 'round', |
835 | 842 | 'line-cap': strokeStyle.lineCap || 'round' |
836 | 843 | }; |
|
0 commit comments