Skip to content

Commit 1c8b58d

Browse files
committed
【fix】修复lealfet tianditulayer在L.control.layers合用时报错的问题
1 parent 2a959a2 commit 1c8b58d

File tree

4 files changed

+155
-4
lines changed

4 files changed

+155
-4
lines changed

src/leaflet/mapping/ImageMapLayer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export var ImageMapLayer = Layer.extend({
340340

341341
var zoom = this._map.getZoom();
342342
var bounds = this._map.getBounds();
343-
if (zoom > this._map.options.maxZoom || zoom < this._map.options.zoom) {
343+
if (zoom > (this._map.options.maxZoom|| 18) || zoom < (this._map.options.minZoom || 0)) {
344344
if (this._currentImage) {
345345
this._currentImage._map.removeLayer(this._currentImage);
346346
this._currentImage = null;

src/leaflet/mapping/TiandituTileLayer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ export var TiandituTileLayer = WMTSLayer.extend({
5555
this.options.maxZoom = this.layerZoomMap[this.options.layerType] - 1;
5656
WMTSLayer.prototype.initialize.call(this, this.options.url, this.options);
5757
L.stamp(this);
58-
},
59-
onAdd: function (map) {
60-
this.options.tilematrixSet = map.options.crs.code === "EPSG:4326" ? "c" : "w";
6158
if (this.options.key) {
6259
this._url = `${this._url}tk=${this.options.key}`;
6360
}
61+
},
62+
onAdd: function (map) {
63+
this.options.tilematrixSet = map.options.crs.code === "EPSG:4326" ? "c" : "w";
6464
this._url = this._url.replace("{layer}", this.options.layer).replace("{proj}", this.options.tilematrixSet);
6565
WMTSLayer.prototype.onAdd.call(this, map);
6666
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import { tiandituTileLayer } from '../../../src/leaflet/mapping/TiandituTileLayer';
2+
import { mockCreateTile } from '../../tool/mock_leaflet';
3+
import { TianDiTu_WGS84CRS, TianDiTu_MercatorCRS } from '../../../src/leaflet/core/ExtendsCRS';
4+
5+
describe('leaflet_TiandituTileLayer', () => {
6+
var originalTimeout, map, layer,testDiv;
7+
beforeAll(() => {
8+
testDiv = document.createElement('div');
9+
testDiv.setAttribute('id', 'map');
10+
testDiv.style.styleFloat = 'left';
11+
testDiv.style.marginLeft = '8px';
12+
testDiv.style.marginTop = '50px';
13+
testDiv.style.width = '500px';
14+
testDiv.style.height = '500px';
15+
mockCreateTile();
16+
document.body.appendChild(testDiv);
17+
map = L.map('map', {
18+
center: [0, 0],
19+
maxZoom: 17,
20+
zoom: 1,
21+
crs: TianDiTu_MercatorCRS
22+
});
23+
});
24+
beforeEach(() => {
25+
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
26+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000;
27+
});
28+
afterEach(() => {
29+
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
30+
if (layer) {
31+
layer.remove();
32+
layer = null;
33+
}
34+
});
35+
afterAll(() => {
36+
document.body.removeChild(testDiv);
37+
map.remove();
38+
});
39+
40+
it('initialize', () => {
41+
layer = tiandituTileLayer({
42+
key: '123456'
43+
}).addTo(map);
44+
expect(layer).not.toBeNull();
45+
expect(layer.options.layer).toBe('vec');
46+
expect(layer.options.isLabel).toBeFalse;
47+
expect(layer.options.tilematrixSet).toBe('w');
48+
expect(layer.options.maxZoom).toBe(17);
49+
expect(layer.options.requestEncoding).toBe('KVP');
50+
expect(layer.options.key).toBe('123456');
51+
expect(layer._url).toBe('https://t{s}.tianditu.gov.cn/vec_w/wmts?tk=123456');
52+
});
53+
54+
it('initialize_img', () => {
55+
layer = tiandituTileLayer({
56+
layerType: 'img',
57+
isLabel: true,
58+
key: '123456'
59+
}).addTo(map);
60+
expect(layer).not.toBeNull();
61+
expect(layer.options.layer).toBe('cia');
62+
expect(layer.options.isLabel).toBeTrue;
63+
expect(layer.options.tilematrixSet).toBe('w');
64+
expect(layer.options.maxZoom).toBe(17);
65+
expect(layer.options.requestEncoding).toBe('KVP');
66+
expect(layer.options.key).toBe('123456');
67+
expect(layer._url).toBe('https://t{s}.tianditu.gov.cn/cia_w/wmts?tk=123456');
68+
});
69+
70+
it('initialize_ter', () => {
71+
layer = tiandituTileLayer({
72+
layerType: 'ter',
73+
key: '123456'
74+
}).addTo(map);
75+
expect(layer).not.toBeNull();
76+
expect(layer.options.layer).toBe('ter');
77+
expect(layer.options.isLabel).toBeFalse;
78+
expect(layer.options.tilematrixSet).toBe('w');
79+
expect(layer.options.maxZoom).toBe(13);
80+
expect(layer.options.requestEncoding).toBe('KVP');
81+
expect(layer.options.key).toBe('123456');
82+
expect(layer._url).toBe('https://t{s}.tianditu.gov.cn/ter_w/wmts?tk=123456');
83+
});
84+
85+
it('initialize_4326', () => {
86+
map.options.crs = TianDiTu_WGS84CRS;
87+
layer = tiandituTileLayer({
88+
layerType: 'ter',
89+
key: '123456'
90+
}).addTo(map);
91+
expect(layer).not.toBeNull();
92+
expect(layer.options.layer).toBe('ter');
93+
expect(layer.options.isLabel).toBeFalse;
94+
expect(layer.options.tilematrixSet).toBe('c');
95+
expect(layer.options.maxZoom).toBe(13);
96+
expect(layer.options.requestEncoding).toBe('KVP');
97+
expect(layer.options.key).toBe('123456');
98+
expect(layer._url).toBe('https://t{s}.tianditu.gov.cn/ter_c/wmts?tk=123456');
99+
map.options.crs = TianDiTu_MercatorCRS;
100+
});
101+
102+
it('initialize_url', () => {
103+
layer = tiandituTileLayer({
104+
url: 'https://my.tianditu/{layer}_{proj}/wmts?',
105+
key: '123456'
106+
}).addTo(map);
107+
expect(layer).not.toBeNull();
108+
expect(layer.options.layer).toBe('vec');
109+
expect(layer.options.isLabel).toBeFalse;
110+
expect(layer.options.tilematrixSet).toBe('w');
111+
expect(layer._url).toBe('https://my.tianditu/vec_w/wmts?tk=123456');
112+
});
113+
114+
it('getTileUrl', () => {
115+
var coords = { x: 0, y: 0, z: 0 };
116+
layer = tiandituTileLayer({
117+
layerType: 'img',
118+
isLabel: true,
119+
key: '123456'
120+
}).addTo(map);
121+
expect(layer).not.toBeNull();
122+
expect(layer.options.layer).toBe('cia');
123+
expect(layer.options.isLabel).toBeTrue;
124+
expect(layer.options.tilematrixSet).toBe('w');
125+
expect(layer.options.maxZoom).toBe(17);
126+
expect(layer.options.requestEncoding).toBe('KVP');
127+
expect(layer.options.key).toBe('123456');
128+
expect(layer._url).toBe('https://t{s}.tianditu.gov.cn/cia_w/wmts?tk=123456');
129+
expect(layer.getTileUrl(coords)).toBe(
130+
'https://t0.tianditu.gov.cn/cia_w/wmts?tk=123456&service=WMTS&request=GetTile&version=1.0.0&style=default&tilematrixSet=w&format=tiles&width=256&height=256&layer=cia&tilematrix=2&tilerow=0&tilecol=0'
131+
);
132+
});
133+
134+
it('getTileUrl_remove', () => {
135+
var coords = { x: 0, y: 0, z: 0 };
136+
layer = tiandituTileLayer({
137+
layerType: 'img',
138+
isLabel: true,
139+
key: '123456'
140+
}).addTo(map);
141+
expect(layer.getTileUrl(coords)).toBe(
142+
'https://t0.tianditu.gov.cn/cia_w/wmts?tk=123456&service=WMTS&request=GetTile&version=1.0.0&style=default&tilematrixSet=w&format=tiles&width=256&height=256&layer=cia&tilematrix=2&tilerow=0&tilecol=0'
143+
);
144+
layer.remove();
145+
layer.addTo(map);
146+
expect(layer.getTileUrl(coords)).toBe(
147+
'https://t0.tianditu.gov.cn/cia_w/wmts?tk=123456&service=WMTS&request=GetTile&version=1.0.0&style=default&tilematrixSet=w&format=tiles&width=256&height=256&layer=cia&tilematrix=2&tilerow=0&tilecol=0'
148+
);
149+
});
150+
});

test/test-main-leaflet.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import './leaflet/mapping/ImageMapLayerSpec.js';
1111
import './leaflet/mapping/TiledMapLayerSpec.js';
1212
import './leaflet/mapping/TileLayer.WMTSSpec.js';
1313
import './leaflet/mapping/WebMapSpec.js';
14+
import './leaflet/mapping/TiandituTileLayerSpec';
1415

1516
/*leaflet -- overlay*/
1617

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy