Skip to content

Commit de440ef

Browse files
[fix]maplibregl getZoom
1 parent 121261c commit de440ef

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

src/common/iServer/InitMapServiceBase.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Copyright© 2000 - 2023 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';
45

56
/**
67
* @private
@@ -104,3 +105,37 @@ export function getEpsgCode(prjCoordSys) {
104105
}
105106
return 'EPSG:' + epsgCode;
106107
}
108+
109+
110+
/**
111+
* @private
112+
* @function createMapOptions
113+
* @description mapboxgl maplibregl 获取地图resolutions。
114+
* @returns {Array} resolutions
115+
*/
116+
export function scalesToResolutions(bounds, maxZoom = 22, tileSize = 512) {
117+
var resolutions = [];
118+
const maxReolution = Math.abs(bounds.left - bounds.right) / tileSize;
119+
for (let i = 0; i < maxZoom; i++) {
120+
resolutions.push(maxReolution / Math.pow(2, i));
121+
}
122+
return resolutions.sort(function (a, b) {
123+
return b - a;
124+
});
125+
}
126+
127+
/**
128+
* @private
129+
* @function getZoom
130+
* @description mapboxgl maplibregl 获取地图zoom。
131+
* @param {Object} resetServiceInfo - rest 地图服务信息。
132+
* @param {string} resetServiceInfo.scale - scale
133+
* @param {Object} resetServiceInfo.dpi - dpi
134+
* @param {Object} resetServiceInfo.coordUnit- coordUnit。
135+
* @param {Object} extent - extent。
136+
* @returns {number} zoom
137+
*/
138+
export function getZoom({ scale, dpi, coordUnit }, extent) {
139+
const resolutions = scalesToResolutions(extent);
140+
return getZoomByResolution(scaleToResolution(scale, dpi, coordUnit), resolutions);
141+
}

src/mapboxgl/mapping/InitMap.js

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import mapboxgl from 'mapbox-gl';
22
import { MapService } from '../services/MapService';
33
import { FetchRequest } from '@supermap/iclient-common/util/FetchRequest';
4-
import { InitMapServiceBase, isPlaneProjection } from '@supermap/iclient-common/iServer/InitMapServiceBase';
5-
import { scaleToResolution, getZoomByResolution } from '@supermap/iclient-common/util/MapCalculateUtil';
4+
import { InitMapServiceBase, isPlaneProjection, getZoom } from '@supermap/iclient-common/iServer/InitMapServiceBase';
65
import proj4 from 'proj4';
76

87
/**
@@ -241,35 +240,4 @@ async function createMapOptions(url, resetServiceInfo, options) {
241240
};
242241
}
243242

244-
/**
245-
* @private
246-
* @function createMapOptions
247-
* @description 获取地图resolutions。
248-
* @returns {Array} resolutions
249-
*/
250-
function scalesToResolutions(bounds, maxZoom = 22, tileSize = 512) {
251-
var resolutions = [];
252-
const maxReolution = Math.abs(bounds.left - bounds.right) / tileSize;
253-
for (let i = 0; i < maxZoom; i++) {
254-
resolutions.push(maxReolution / Math.pow(2, i));
255-
}
256-
return resolutions.sort(function (a, b) {
257-
return b - a;
258-
});
259-
}
260243

261-
/**
262-
* @private
263-
* @function getZoom
264-
* @description 获取地图zoom。
265-
* @param {Object} resetServiceInfo - rest 地图服务信息。
266-
* @param {string} resetServiceInfo.scale - scale
267-
* @param {Object} resetServiceInfo.dpi - dpi
268-
* @param {Object} resetServiceInfo.coordUnit- coordUnit。
269-
* @param {Object} extent - extent。
270-
* @returns {number} zoom
271-
*/
272-
function getZoom({ scale, dpi, coordUnit }, extent) {
273-
const resolutions = scalesToResolutions(extent);
274-
return getZoomByResolution(scaleToResolution(scale, dpi, coordUnit), resolutions);
275-
}

src/maplibregl/mapping/InitMap.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import maplibregl from 'maplibre-gl';
22
import { MapService } from '../services/MapService';
3-
import { InitMapServiceBase, isPlaneProjection } from '@supermap/iclient-common/iServer/InitMapServiceBase';
3+
import { InitMapServiceBase, isPlaneProjection, getZoom } from '@supermap/iclient-common/iServer/InitMapServiceBase';
44
import proj4 from 'proj4';
55
/**
66
* @function initMap
@@ -77,7 +77,7 @@ async function createMapOptions(url, resetServiceInfo, options) {
7777
}
7878
const sourceType = options.type || 'raster';
7979
const mapOptions = options.mapOptions || {};
80-
const { center } = resetServiceInfo;
80+
const { center, bounds, scale, dpi, coordUnit } = resetServiceInfo;
8181
const mapCenter = center ? proj4('EPSG:3857', 'EPSG:4326', [center.x, center.y]) : [0, 0];
8282
let tileUrl =
8383
sourceType === 'vector-tile'
@@ -90,9 +90,11 @@ async function createMapOptions(url, resetServiceInfo, options) {
9090
const transparent = mapOptions.transparent !== false;
9191
tileUrl += `/zxyTileImage.png?z={z}&x={x}&y={y}&width=${tileSize}&height=${tileSize}&transparent=${transparent}`;
9292
}
93+
const zoom = getZoom({ scale, dpi, coordUnit }, bounds);
9394
return {
9495
container: 'map',
9596
center: mapCenter,
97+
zoom,
9698
style:
9799
sourceType === 'raster'
98100
? {
@@ -117,4 +119,4 @@ async function createMapOptions(url, resetServiceInfo, options) {
117119
: tileUrl,
118120
...mapOptions
119121
};
120-
}
122+
}

0 commit comments

Comments
 (0)