Replace ScaleLine Units with typedef
This commit is contained in:
+12
-20
@@ -13,17 +13,9 @@ import {assert} from '../asserts.js';
|
|||||||
const UNITS_PROP = 'units';
|
const UNITS_PROP = 'units';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Units for the scale line. Supported values are `'degrees'`, `'imperial'`,
|
* @typedef {'degrees' | 'imperial' | 'nautical' | 'metric' | 'us'} Units
|
||||||
* `'nautical'`, `'metric'`, `'us'`.
|
* Units for the scale line.
|
||||||
* @enum {string}
|
|
||||||
*/
|
*/
|
||||||
export const Units = {
|
|
||||||
DEGREES: 'degrees',
|
|
||||||
IMPERIAL: 'imperial',
|
|
||||||
NAUTICAL: 'nautical',
|
|
||||||
METRIC: 'metric',
|
|
||||||
US: 'us',
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @const
|
* @const
|
||||||
@@ -57,7 +49,7 @@ const DEFAULT_DPI = 25.4 / 0.28;
|
|||||||
* should be re-rendered. This is called in a `requestAnimationFrame` callback.
|
* should be re-rendered. This is called in a `requestAnimationFrame` callback.
|
||||||
* @property {HTMLElement|string} [target] Specify a target if you want the control
|
* @property {HTMLElement|string} [target] Specify a target if you want the control
|
||||||
* to be rendered outside of the map's viewport.
|
* to be rendered outside of the map's viewport.
|
||||||
* @property {import("./ScaleLine.js").Units|string} [units='metric'] Units.
|
* @property {Units} [units='metric'] Units.
|
||||||
* @property {boolean} [bar=false] Render scalebars instead of a line.
|
* @property {boolean} [bar=false] Render scalebars instead of a line.
|
||||||
* @property {number} [steps=4] Number of steps the scalebar should use. Use even numbers
|
* @property {number} [steps=4] Number of steps the scalebar should use. Use even numbers
|
||||||
* for best results. Only applies when `bar` is `true`.
|
* for best results. Only applies when `bar` is `true`.
|
||||||
@@ -164,7 +156,7 @@ class ScaleLine extends Control {
|
|||||||
|
|
||||||
this.addChangeListener(UNITS_PROP, this.handleUnitsChanged_);
|
this.addChangeListener(UNITS_PROP, this.handleUnitsChanged_);
|
||||||
|
|
||||||
this.setUnits(options.units || Units.METRIC);
|
this.setUnits(options.units || 'metric');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
@@ -193,7 +185,7 @@ class ScaleLine extends Control {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the units to use in the scale line.
|
* Return the units to use in the scale line.
|
||||||
* @return {import("./ScaleLine.js").Units} The units
|
* @return {Units} The units
|
||||||
* to use in the scale line.
|
* to use in the scale line.
|
||||||
* @observable
|
* @observable
|
||||||
* @api
|
* @api
|
||||||
@@ -211,7 +203,7 @@ class ScaleLine extends Control {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the units to use in the scale line.
|
* Set the units to use in the scale line.
|
||||||
* @param {import("./ScaleLine.js").Units} units The units to use in the scale line.
|
* @param {Units} units The units to use in the scale line.
|
||||||
* @observable
|
* @observable
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -246,7 +238,7 @@ class ScaleLine extends Control {
|
|||||||
const projection = viewState.projection;
|
const projection = viewState.projection;
|
||||||
const units = this.getUnits();
|
const units = this.getUnits();
|
||||||
const pointResolutionUnits =
|
const pointResolutionUnits =
|
||||||
units == Units.DEGREES ? ProjUnits.DEGREES : ProjUnits.METERS;
|
units == 'degrees' ? ProjUnits.DEGREES : ProjUnits.METERS;
|
||||||
let pointResolution = getPointResolution(
|
let pointResolution = getPointResolution(
|
||||||
projection,
|
projection,
|
||||||
viewState.resolution,
|
viewState.resolution,
|
||||||
@@ -264,7 +256,7 @@ class ScaleLine extends Control {
|
|||||||
|
|
||||||
let nominalCount = minWidth * pointResolution;
|
let nominalCount = minWidth * pointResolution;
|
||||||
let suffix = '';
|
let suffix = '';
|
||||||
if (units == Units.DEGREES) {
|
if (units == 'degrees') {
|
||||||
const metersPerDegree = METERS_PER_UNIT[ProjUnits.DEGREES];
|
const metersPerDegree = METERS_PER_UNIT[ProjUnits.DEGREES];
|
||||||
nominalCount *= metersPerDegree;
|
nominalCount *= metersPerDegree;
|
||||||
if (nominalCount < metersPerDegree / 60) {
|
if (nominalCount < metersPerDegree / 60) {
|
||||||
@@ -276,7 +268,7 @@ class ScaleLine extends Control {
|
|||||||
} else {
|
} else {
|
||||||
suffix = '\u00b0'; // degrees
|
suffix = '\u00b0'; // degrees
|
||||||
}
|
}
|
||||||
} else if (units == Units.IMPERIAL) {
|
} else if (units == 'imperial') {
|
||||||
if (nominalCount < 0.9144) {
|
if (nominalCount < 0.9144) {
|
||||||
suffix = 'in';
|
suffix = 'in';
|
||||||
pointResolution /= 0.0254;
|
pointResolution /= 0.0254;
|
||||||
@@ -287,10 +279,10 @@ class ScaleLine extends Control {
|
|||||||
suffix = 'mi';
|
suffix = 'mi';
|
||||||
pointResolution /= 1609.344;
|
pointResolution /= 1609.344;
|
||||||
}
|
}
|
||||||
} else if (units == Units.NAUTICAL) {
|
} else if (units == 'nautical') {
|
||||||
pointResolution /= 1852;
|
pointResolution /= 1852;
|
||||||
suffix = 'NM';
|
suffix = 'NM';
|
||||||
} else if (units == Units.METRIC) {
|
} else if (units == 'metric') {
|
||||||
if (nominalCount < 0.001) {
|
if (nominalCount < 0.001) {
|
||||||
suffix = 'μm';
|
suffix = 'μm';
|
||||||
pointResolution *= 1000000;
|
pointResolution *= 1000000;
|
||||||
@@ -303,7 +295,7 @@ class ScaleLine extends Control {
|
|||||||
suffix = 'km';
|
suffix = 'km';
|
||||||
pointResolution /= 1000;
|
pointResolution /= 1000;
|
||||||
}
|
}
|
||||||
} else if (units == Units.US) {
|
} else if (units == 'us') {
|
||||||
if (nominalCount < 0.9144) {
|
if (nominalCount < 0.9144) {
|
||||||
suffix = 'in';
|
suffix = 'in';
|
||||||
pointResolution *= 39.37;
|
pointResolution *= 39.37;
|
||||||
|
|||||||
Reference in New Issue
Block a user