diff --git a/src/ol/control/ScaleLine.js b/src/ol/control/ScaleLine.js index 76509844b9..f7d349f21a 100644 --- a/src/ol/control/ScaleLine.js +++ b/src/ol/control/ScaleLine.js @@ -201,20 +201,12 @@ class ScaleLine extends Control { ProjUnits.METERS; let pointResolution = getPointResolution(projection, viewState.resolution, center, pointResolutionUnits); - if (projection.getUnits() != ProjUnits.DEGREES && projection.getMetersPerUnit() - && pointResolutionUnits == ProjUnits.METERS) { - pointResolution *= projection.getMetersPerUnit(); - } let nominalCount = this.minWidth_ * pointResolution; let suffix = ''; if (units == Units.DEGREES) { const metersPerDegree = METERS_PER_UNIT[ProjUnits.DEGREES]; - if (projection.getUnits() == ProjUnits.DEGREES) { - nominalCount *= metersPerDegree; - } else { - pointResolution /= metersPerDegree; - } + nominalCount *= metersPerDegree; if (nominalCount < metersPerDegree / 60) { suffix = '\u2033'; // seconds pointResolution *= 3600; diff --git a/test/spec/ol/control/scaleline.test.js b/test/spec/ol/control/scaleline.test.js index 11747eef83..2b79613bc8 100644 --- a/test/spec/ol/control/scaleline.test.js +++ b/test/spec/ol/control/scaleline.test.js @@ -1,8 +1,10 @@ import Map from '../../../../src/ol/Map.js'; import View from '../../../../src/ol/View.js'; import ScaleLine, {render} from '../../../../src/ol/control/ScaleLine.js'; -import {fromLonLat} from '../../../../src/ol/proj.js'; +import {fromLonLat, clearAllProjections, addCommon} from '../../../../src/ol/proj.js'; import Projection from '../../../../src/ol/proj/Projection.js'; +import proj4 from 'proj4'; +import {register} from '../../../../src/ol/proj/proj4.js'; describe('ol.control.ScaleLine', function() { let map; @@ -244,6 +246,25 @@ describe('ol.control.ScaleLine', function() { }); describe('projections affect the scaleline', function() { + + beforeEach(function() { + proj4.defs('Indiana-East', 'PROJCS["IN83-EF",GEOGCS["LL83",DATUM["NAD83",' + + 'SPHEROID["GRS1980",6378137.000,298.25722210]],PRIMEM["Greenwich",0],' + + 'UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],' + + 'PARAMETER["false_easting",328083.333],' + + 'PARAMETER["false_northing",820208.333],' + + 'PARAMETER["scale_factor",0.999966666667],' + + 'PARAMETER["central_meridian",-85.66666666666670],' + + 'PARAMETER["latitude_of_origin",37.50000000000000],' + + 'UNIT["Foot_US",0.30480060960122]]'); + register(proj4); + }); + + afterEach(function() { + clearAllProjections(); + addCommon(); + }); + it('is rendered differently for different projections', function() { const ctrl = new ScaleLine(); ctrl.setMap(map); @@ -253,15 +274,47 @@ describe('ol.control.ScaleLine', function() { projection: 'EPSG:3857' })); map.renderSync(); - const innerHtml3857 = ctrl.element.innerHTML; + expect(ctrl.element.innerText).to.be('2000 km'); map.setView(new View({ center: [7, 52], zoom: 2, projection: 'EPSG:4326' })); map.renderSync(); - const innerHtml4326 = ctrl.element.innerHTML; - expect(innerHtml4326).to.not.be(innerHtml3857); + expect(ctrl.element.innerText).to.be('5000 km'); + map.setView(new View({ + center: fromLonLat([-85.685, 39.891], 'Indiana-East'), + zoom: 7, + projection: 'Indiana-East' + })); + map.renderSync(); + expect(ctrl.element.innerText).to.be('100 km'); + }); + + it('shows the same scale for different projections at higher resolutions', function() { + const ctrl = new ScaleLine(); + ctrl.setMap(map); + map.setView(new View({ + center: fromLonLat([-85.685, 39.891]), + zoom: 7, + projection: 'EPSG:3857' + })); + map.renderSync(); + expect(ctrl.element.innerText).to.be('100 km'); + map.setView(new View({ + center: [-85.685, 39.891], + zoom: 7, + projection: 'EPSG:4326' + })); + map.renderSync(); + expect(ctrl.element.innerText).to.be('100 km'); + map.setView(new View({ + center: fromLonLat([-85.685, 39.891], 'Indiana-East'), + zoom: 7, + projection: 'Indiana-East' + })); + map.renderSync(); + expect(ctrl.element.innerText).to.be('100 km'); }); it('Projection\'s metersPerUnit affect scale for non-degree units', function() {