diff --git a/src/common/util/MapCalculateUtil.js b/src/common/util/MapCalculateUtil.js index 891624dc4..e06ffcafa 100644 --- a/src/common/util/MapCalculateUtil.js +++ b/src/common/util/MapCalculateUtil.js @@ -22,23 +22,21 @@ import { Unit } from '../REST'; * ``` */ export var getMeterPerMapUnit = function(mapUnit) { - var earchRadiusInMeters = 6378137; - var meterPerMapUnit; - if (mapUnit === Unit.METER) { - meterPerMapUnit = 1; - } else if (mapUnit === Unit.DEGREE) { - // 每度表示多少米。 - meterPerMapUnit = (Math.PI * 2 * earchRadiusInMeters) / 360; - } else if (mapUnit === Unit.KILOMETER) { - meterPerMapUnit = 1.0e-3; - } else if (mapUnit === Unit.INCH) { - meterPerMapUnit = 1 / 2.5399999918e-2; - } else if (mapUnit === Unit.FOOT) { - meterPerMapUnit = 0.3048; - } else { - return meterPerMapUnit; - } - return meterPerMapUnit; + var earchRadiusInMeters = 6378137; + var meterPerMapUnit; + if (['m','meter','meters'].indexOf(mapUnit.toLocaleLowerCase())>-1) { + meterPerMapUnit = 1; + } else if (['degrees','deg','degree','dd'].indexOf(mapUnit.toLocaleLowerCase())>-1) { + // 每度表示多少米。 + meterPerMapUnit = (Math.PI * 2 * earchRadiusInMeters) / 360; + } else if (mapUnit === Unit.KILOMETER) { + meterPerMapUnit = 1.0e-3; + } else if (mapUnit === Unit.INCH) { + meterPerMapUnit = 1 / 2.5399999918e-2; + } else if (mapUnit === Unit.FOOT) { + meterPerMapUnit = 0.3048; + } + return meterPerMapUnit; }; /** diff --git a/test/common/util/MapCalculateUtilSpec.js b/test/common/util/MapCalculateUtilSpec.js new file mode 100644 index 000000000..80f2eb261 --- /dev/null +++ b/test/common/util/MapCalculateUtilSpec.js @@ -0,0 +1,24 @@ +import { getMeterPerMapUnit } from '../../../src/common/util/MapCalculateUtil'; + +describe('MapCalculateUtil', () => { + it('getMeterPerMapUnit m', () => { + expect(getMeterPerMapUnit('m')).toBe(1); + expect(getMeterPerMapUnit('M')).toBe(1); + expect(getMeterPerMapUnit('meter')).toBe(1); + expect(getMeterPerMapUnit('meters')).toBe(1); + expect(getMeterPerMapUnit('METER')).toBe(1); + expect(getMeterPerMapUnit('METERS')).toBe(1); + expect(getMeterPerMapUnit('METRE')).toBeUndefined() + }); + it('getMeterPerMapUnit dge', () => { + expect(getMeterPerMapUnit('degrees')).toBeCloseTo(111319.49079327358, 0.00001);; + expect(getMeterPerMapUnit('deg')).toBeCloseTo(111319.49079327358, 0.00001);; + expect(getMeterPerMapUnit('degree')).toBeCloseTo(111319.49079327358, 0.00001);; + expect(getMeterPerMapUnit('dd')).toBeCloseTo(111319.49079327358, 0.00001);; + expect(getMeterPerMapUnit('DEGREES')).toBeCloseTo(111319.49079327358, 0.00001);; + expect(getMeterPerMapUnit('DEG')).toBeCloseTo(111319.49079327358, 0.00001);; + expect(getMeterPerMapUnit('DEGREE')).toBeCloseTo(111319.49079327358, 0.00001);; + expect(getMeterPerMapUnit('DD')).toBeCloseTo(111319.49079327358, 0.00001);; + expect(getMeterPerMapUnit('degre')).toBeUndefined() +}); +}); diff --git a/test/test-main-common.js b/test/test-main-common.js index b362ac486..48b353831 100644 --- a/test/test-main-common.js +++ b/test/test-main-common.js @@ -216,3 +216,4 @@ import './common/components/chart/ChartViewSpec'; import './common/util/FetchRequestSpec'; import './common/lang/LangSpec.js'; +import './common/util/MapCalculateUtilSpec';