Skip to content

Commit b54bdf0

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

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

Diff for: src/common/iServer/InitMapServiceBase.js

+12-6
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
@@ -149,13 +149,19 @@ export function getTileset(tilesets = [], targets) {
149149

150150
/**
151151
* @private
152-
* @function scalesToResolutions
152+
* @function extentToResolutions
153153
* @description mapboxgl maplibregl 获取地图resolutions。
154154
* @returns {Array} resolutions
155155
*/
156-
export function scalesToResolutions(bounds, maxZoom = 22, tileSize = 512) {
156+
export function extentToResolutions(bounds, maxZoom = 22, tileSize = 512) {
157157
var resolutions = [];
158-
const maxReolution = Math.abs(bounds.left - bounds.right) / tileSize;
158+
var left = bounds.left;
159+
var right = bounds.right;
160+
if (Array.isArray(bounds)) {
161+
left = bounds[0];
162+
right = bounds[2];
163+
}
164+
const maxReolution = Math.abs(left - right) / tileSize;
159165
for (let i = 0; i < maxZoom; i++) {
160166
resolutions.push(maxReolution / Math.pow(2, i));
161167
}
@@ -175,7 +181,7 @@ export function getTileset(tilesets = [], targets) {
175181
* @param {Object} extent - extent。
176182
* @returns {number} zoom
177183
*/
178-
export function getZoom({ scale, dpi, coordUnit }, extent) {
179-
const resolutions = scalesToResolutions(extent);
184+
export function getZoom({ scale, dpi, coordUnit }, extent) {
185+
const resolutions = extentToResolutions(extent);
180186
return getZoomByResolution(scaleToResolution(scale, dpi, coordUnit), resolutions);
181187
}

Diff for: test/common/iServer/InitMapServiceBaseSpec.js

+42
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+
});

Diff for: test/test-main-common.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import './common/iServer/GetLayersInfoServiceSpec.js';
5050
import './common/iServer/GetLayersLegendInfoServiceSpec.js';
5151
import './common/iServer/GridSpec.js';
5252
import './common/iServer/ImageSpec.js';
53+
import './common/iServer/InitMapServiceBaseSpec.js';
5354
import './common/iServer/InterpolationAnalystServiceSpec.js';
5455
import './common/iServer/LabelImageCellSpec.js';
5556
import './common/iServer/LabelMixedStyleSpec.js';

0 commit comments

Comments
 (0)