Skip to content

Commit

Permalink
DistanceTool support custom format label content (#2151)
Browse files Browse the repository at this point in the history
  • Loading branch information
deyihu authored Dec 19, 2023
1 parent a8bdd7f commit bf1ede2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/map/tool/AreaTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const options = {
};

/**
* A map tool to help measure area on the map
* A map tool to help measure area on the map .it is extends DistanceTool
* @category maptool
* @extends DistanceTool
* @example
Expand Down Expand Up @@ -74,6 +74,10 @@ class AreaTool extends DistanceTool {
area = map.getProjection().measureArea(toMeasure);
}
this._lastMeasure = area;
const result = this._formatLabelContent(area);
if (result) {
return result;
}
let units;
if (this.options['language'] === 'zh-CN') {
units = [' 平方米', ' 平方公里', ' 平方英尺', ' 平方英里'];
Expand Down
16 changes: 15 additions & 1 deletion src/map/tool/DistanceTool.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArrayHasData, UID } from '../../core/util';
import { isArrayHasData, isFunction, UID } from '../../core/util';
import { extendSymbol } from '../../core/util/style';
import Size from '../../geo/Size';
import Geometry from '../../geometry/Geometry';
Expand All @@ -16,10 +16,12 @@ import DrawTool from './DrawTool';
* @property {Object} options.vertexSymbol - symbol of the vertice
* @property {Object} options.labelOptions - construct options of the vertice labels.
* @property {Number} options.decimalPlaces - The decimal places of the measured value
* @property {Function} options.formatLabelContent - Content function for custom measurement result labels
* @memberOf DistanceTool
* @instance
*/
const options = {
'formatLabelContent': null,
'decimalPlaces': 2,
'mode': 'LineString',
'language': 'zh-CN', //'en-US'
Expand Down Expand Up @@ -177,6 +179,14 @@ class DistanceTool extends DrawTool {
return this;
}

_formatLabelContent(params) {
const formatLabelContent = this.options.formatLabelContent;
if (formatLabelContent && isFunction(formatLabelContent)) {
return formatLabelContent.call(this, params) + '';
}
return null;
}

_measure(toMeasure) {
const map = this.getMap();
let length;
Expand All @@ -186,6 +196,10 @@ class DistanceTool extends DrawTool {
length = map.getProjection().measureLength(toMeasure);
}
this._lastMeasure = length;
const result = this._formatLabelContent(length);
if (result) {
return result;
}
let units;
if (this.options['language'] === 'zh-CN') {
units = [' 米', ' 公里', ' 英尺', ' 英里'];
Expand Down

0 comments on commit bf1ede2

Please sign in to comment.