8000 【fix】修复mapboxgl initmap 矢量瓦片时初始级别不对的问题 · SuperMap/iClient-JavaScript@07c0b99 · GitHub
[go: up one dir, main page]

Skip to content

Commit 07c0b99

Browse files
committed
【fix】修复mapboxgl initmap 矢量瓦片时初始级别不对的问题
1 parent 275e733 commit 07c0b99

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/common/iServer/InitMapServiceBase.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* Copyright© 2000 - 2024 SuperMap Software Co.Ltd. All rights reserved.
22
* This program are made available under the terms of the Apache License, Version 2.0
33
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
4-
import { scaleToResolution, getZoomByResolution } from '../util/MapCalculateUtil';
4+
import { scaleToResolution, getZoomByResolution } from '../util/MapCalculateUtil';
55

66
/**
77
* @private
@@ -113,9 +113,15 @@ export function getEpsgCode(prjCoordSys) {
113113
* @description mapboxgl maplibregl 获取地图resolutions。
114114
* @returns {Array} resolutions
115115
*/
116-
export function scalesToResolutions(bounds, maxZoom = 22, tileSize = 512) {
116+
export function extentToResolutions(bounds, maxZoom = 22, tileSize = 512) {
117117
var resolutions = [];
118-
const maxReolution = Math.abs(bounds.left - bounds.right) / tileSize;
118+
var left = bounds.left;
119+
var right = bounds.right;
120+
if (Array.isArray(bounds)) {
121+
left = bounds[0];
122+
right = bounds[2];
123+
}
124+
const maxReolution = Math.abs(left - right) / tileSize;
119125
for (let i = 0; i < maxZoom; i++) {
120126
resolutions.push(maxReolution / Math.pow(2, i));
121127
}
@@ -135,7 +141,7 @@ export function getEpsgCode(prjCoordSys) {
135141
* @param {Object} extent - extent。
136142
* @returns {number} zoom
137143
*/
138-
export function getZoom({ scale, dpi, coordUnit }, extent) {
139-
const resolutions = scalesToResolutions(extent);
144+
export function getZoom({ scale, dpi, coordUnit }, extent) {
145+
const resolutions = extentToResolutions(extent);
140146
return getZoomByResolution(scaleToResolution(scale, dpi, coordUnit), resolutions);
141147
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { extentToResolutions } from '../../../src/common/iServer/InitMapServiceBase';
2+
3+
describe('extentToResolutions', () => {
4+
it('should calculate resolutions for a given bounding box', () => {
5+
const bounds = { left: -180, right: 180 };
6+
const resolutions = extentToResolutions(bounds);
7+
expect(resolutions.length).toBe(22);
8+
expect(resolutions[0]).toBeCloseTo(0.703125);
9+
expect(resolutions[21]).toBeCloseTo(0.00000686328125);
10+
});
11+
12+
it('should handle array bounds input', () => {
13+
const bounds = [-180, -90, 180, 90];
14+
const resolutions = extentToResolutions(bounds);
15+
expect(resolutions.length).toBe(22);
16+
expect(resolutions[0]).toBeCloseTo(0.703125);
17+
expect(resolutions[21]).toBeCloseTo(0.00000686328125);
18+
});
19+
20+
it('should handle custom maxZoom and tileSize', () => {
21+
const bounds = { left: -180, right: 180 };
22+
const resolutions = extentToResolutions(bounds, 10, 256);
23+
expect(resolutions.length).toBe(10);
24+
expect(resolutions[0]).toBeCloseTo(1.40625);
25+
expect(resolutions[9]).toBeCloseTo(0.00146484375);
26+
});
27+
28+
it('should handle zero width bounds', () => {
29+
const bounds = { left: 0, right: 0 };
30+
const resolutions = extentToResolutions(bounds);
31+
expect(resolutions.length).toBe(22);
32+
expect(resolutions[0]).toBe(0);
33+
});
34+
35+
it('should handle negative width bounds', () => {
36+
const bounds = { left: 180, right: -180 };
37+
const resolutions = extentToResolutions(bounds);
38+
expect(resolutions.length).toBe(22);
39+
expect(resolutions[0]).toBeCloseTo(0.703125);
40+
expect(resolutions[21]).toBeCloseTo(0.00000686328125);
41+
});
42+
});

test/test-main-common.js

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import './common/iServer/GetLayersInfoServiceSpec.js';
4646
import './common/iServer/GetLayersLegendInfoServiceSpec.js';
4747
import './common/iServer/GridSpec.js';
4848
import './common/iServer/ImageSpec.js';
49+
import './common/iServer/InitMapServiceBaseSpec.js';
4950
import './common/iServer/InterpolationAnalystServiceSpec.js';
5051
import './common/iServer/LabelImageCellSpec.js';
5152
import './common/iServer/LabelMixedStyleSpec.js';

0 commit comments

Comments
 (0)
0