Skip to content

Commit d9c4178

Browse files
committed
【fix】修复leaflet平面坐标系设置分辨率后移动端双指缩放级别错乱的问题
1 parent 2b0ca55 commit d9c4178

File tree

2 files changed

+37
-50
lines changed

2 files changed

+37
-50
lines changed

src/leaflet/core/NonEarthCRS.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* Copyright© 2000 - 2020 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 L from "leaflet";
4+
import L from 'leaflet';
55

66
L.Projection = {};
77

@@ -14,7 +14,6 @@ L.Projection = {};
1414
* @param {L.bounds} bounds - 坐标范围
1515
*/
1616
export var NonProjection = L.Class.extend({
17-
1817
initialize: function (bounds) {
1918
this.bounds = bounds;
2019
},
@@ -29,7 +28,7 @@ export var NonProjection = L.Class.extend({
2928
});
3029

3130
export var nonProjection = function (bounds) {
32-
return new NonProjection(bounds)
31+
return new NonProjection(bounds);
3332
};
3433

3534
/**
@@ -43,18 +42,15 @@ export var nonProjection = function (bounds) {
4342
* @param {Array.<number>} [options.resolutions] - 分辨率。
4443
*/
4544
export var NonEarthCRS = L.Class.extend({
46-
47-
/**
45+
/**
4846
* @member {Object} [L.CRS.NonEarthCRS.prototype.includes=L.CRS]
4947
* @description 包含的坐标对象。
5048
*/
5149
includes: L.CRS,
5250

5351
initialize: function (options) {
5452
if (options.origin) {
55-
this.transformation =
56-
new L.Transformation(1, -options.origin.x,
57-
-1, options.origin.y);
53+
this.transformation = new L.Transformation(1, -options.origin.x, -1, options.origin.y);
5854
}
5955
this.projection = L.Projection.NonProjection(options.bounds);
6056
this.bounds = options.bounds;
@@ -69,12 +65,14 @@ export var NonEarthCRS = L.Class.extend({
6965
* @returns {number} 得到的比例尺。
7066
*/
7167
scale: function (zoom) {
68+
let defaultScale;
7269
if (!this.resolutions || this.resolutions.length === 0) {
73-
var width = Math.max(this.bounds.getSize().x, this.bounds.getSize().y);
74-
var defaultScale = 1.0 / (width / 256);
75-
return defaultScale * Math.pow(2, zoom);
70+
const width = Math.max(this.bounds.getSize().x, this.bounds.getSize().y);
71+
defaultScale = 1.0 / (width / 256);
72+
} else {
73+
defaultScale = 1.0 / this.resolutions[0];
7674
}
77-
return 1.0 / this.resolutions[zoom];
75+
return defaultScale * Math.pow(2, zoom);
7876
},
7977

8078
/**
@@ -84,19 +82,15 @@ export var NonEarthCRS = L.Class.extend({
8482
* @returns {number} 返回空间范围值。
8583
*/
8684
zoom: function (scale) {
85+
let defaultScale;
8786
if (!this.resolutions || this.resolutions.length === 0) {
88-
var width = Math.max(this.bounds.getSize().x, this.bounds.getSize().y);
89-
var defaultScale = 1 / (width / 256);
90-
return Math.log(scale / defaultScale) / Math.LN2;
87+
const width = Math.max(this.bounds.getSize().x, this.bounds.getSize().y);
88+
defaultScale = 1.0 / (width / 256);
89+
} else {
90+
defaultScale = 1.0 / this.resolutions[0];
9191
}
92-
for (var i = 0; i < this.resolutions.length; i++) {
93-
if (1.0 / this.resolutions[i] == scale) {
94-
return i
95-
}
96-
}
97-
return -1;
92+
return Math.log(scale / defaultScale) / Math.LN2;
9893
},
99-
10094
/**
10195
* @function L.CRS.NonEarthCRS.prototype.distance
10296
* @description 通过两个坐标点计算之间的距离。
@@ -114,8 +108,8 @@ export var NonEarthCRS = L.Class.extend({
114108
infinite: false
115109
});
116110
export var nonEarthCRS = function (options) {
117-
return new NonEarthCRS(options)
111+
return new NonEarthCRS(options);
118112
};
119113
L.Projection.NonProjection = nonProjection;
120114

121-
L.CRS.NonEarthCRS = nonEarthCRS;
115+
L.CRS.NonEarthCRS = nonEarthCRS;

test/leaflet/core/NonEarthCRSSpec.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {nonEarthCRS} from '../../../src/leaflet/core/NonEarthCRS';
2-
import {nonProjection} from '../../../src/leaflet/core/NonEarthCRS';
1+
import { nonEarthCRS } from '../../../src/leaflet/core/NonEarthCRS';
2+
import { nonProjection } from '../../../src/leaflet/core/NonEarthCRS';
33

44
describe('leaflet_NonEarthCRS', () => {
55
it('initialize', () => {
@@ -29,7 +29,7 @@ describe('leaflet_NonEarthCRS', () => {
2929

3030
it('CRS_initialize', () => {
3131
var options = {
32-
origin: {x: 30, y: 50},
32+
origin: { x: 30, y: 50 },
3333
bounds: L.bounds([-180, -90], [180, 90]),
3434
resolutions: [1000, 100000]
3535
};
@@ -46,40 +46,33 @@ describe('leaflet_NonEarthCRS', () => {
4646

4747
it('CRS_scale', () => {
4848
var options = {
49-
origin: {x: 30, y: 50},
49+
origin: { x: 30, y: 50 },
5050
bounds: L.bounds([-180, -90], [180, 90]),
51-
resolutions: [100, 100000]
51+
resolutions: [1, 0.5]
5252
};
5353
var nonEarthCRSObject = nonEarthCRS(options);
54-
var scale1 = nonEarthCRSObject.scale(0);
55-
expect(scale1).toEqual(0.01);
54+
expect(nonEarthCRSObject.scale(0)).toEqual(1);
55+
expect(nonEarthCRSObject.scale(1)).toEqual(2);
56+
expect(nonEarthCRSObject.scale(0.5)).toBeCloseTo(1.414, 0.001);
5657
nonEarthCRSObject.resolutions = [];
57-
var scale2 = nonEarthCRSObject.scale(1);
58-
expect(scale2).not.toBeNaN();
58+
expect(nonEarthCRSObject.scale(1)).toBeCloseTo(1.422, 0.001);
5959
});
6060

6161
it('CRS_zoom', () => {
6262
var options1 = {
63-
origin: {x: 30, y: 50},
63+
origin: { x: 30, y: 50 },
6464
bounds: L.bounds([-180, -90], [180, 90]),
65-
resolutions: [100, 100000]
65+
resolutions: [1, 0.5]
6666
};
6767
var nonEarthCRS1 = nonEarthCRS(options1);
68-
var zoom1 = nonEarthCRS1.zoom(0.1);
69-
expect(zoom1).toEqual(-1);
70-
71-
var options2 = {
72-
origin: {x: 30, y: 50},
73-
bounds: L.bounds([-180, -90], [180, 90]),
74-
resolutions: [100, 100000]
75-
};
76-
var nonEarthCRS2 = nonEarthCRS(options2);
77-
var zoom2 = nonEarthCRS2.zoom(0.01);
78-
expect(zoom2).toEqual(0);
68+
expect(nonEarthCRS1.zoom(0.1)).toBeLessThan(0);
69+
expect(nonEarthCRS1.zoom(1)).toEqual(0);
70+
expect(nonEarthCRS1.zoom(2)).toEqual(1);
71+
expect(nonEarthCRS1.zoom(1.5)).toBeCloseTo(0.5849, 0.0001);
7972

8073
var options3 = {
81-
origin: {x: 30, y: 50},
82-
bounds: L.bounds([-128, -90], [128, 90]),
74+
origin: { x: 30, y: 50 },
75+
bounds: L.bounds([-128, -90], [128, 90])
8376
};
8477
var nonEarthCRS3 = nonEarthCRS(options3);
8578
var zoom3 = nonEarthCRS3.zoom(16);
@@ -90,12 +83,12 @@ describe('leaflet_NonEarthCRS', () => {
9083
var latlng1 = L.latLng(50.5, 30.5);
9184
var latlng2 = L.latLng(40, 60.5);
9285
var options = {
93-
origin: {x: 30, y: 50},
86+
origin: { x: 30, y: 50 },
9487
bounds: L.bounds([-180, -90], [180, 90]),
9588
resolutions: [100, 100000]
9689
};
9790
var nonEarthCRSObject = nonEarthCRS(options);
9891
var distance = nonEarthCRSObject.distance(latlng1, latlng2);
9992
expect(distance).not.toBeNaN();
10093
});
101-
});
94+
});

0 commit comments

Comments
 (0)