diff --git a/ruoyi-ui/src/views/cesiumMap/index.vue b/ruoyi-ui/src/views/cesiumMap/index.vue index c5f5a32..fd6a625 100644 --- a/ruoyi-ui/src/views/cesiumMap/index.vue +++ b/ruoyi-ui/src/views/cesiumMap/index.vue @@ -2928,11 +2928,10 @@ export default { const rawMeters = Cesium.Cartesian3.distance(leftCartesian, rightCartesian) if (rawMeters > 0) { const metersPerPx = rawMeters / barPx - const scaleRatio = this.calculateScaleRatio(metersPerPx) - const pixelsPerCm = this.getPixelsPerCm() - const widthPx = Math.min(120, Math.max(40, Math.round(pixelsPerCm))) - const text = `1:${scaleRatio}${this.currentScaleUnit}` - return { text, widthPx, niceMeters: rawMeters } + const niceMeters = this.niceScaleValue(rawMeters) + const widthPx = Math.round((niceMeters / rawMeters) * barPx) + const text = this.formatScaleText(niceMeters) + return { text, widthPx, niceMeters } } } // 2D/WebMercator 备用:用整屏宽度对应的地理范围计算(四角 pick 得到视口矩形) @@ -2942,11 +2941,11 @@ export default { const widthMeters = Cesium.Cartesian3.distance(leftBottom, rightBottom) if (widthMeters > 0) { const metersPerPx = widthMeters / w - const scaleRatio = this.calculateScaleRatio(metersPerPx) - const pixelsPerCm = this.getPixelsPerCm() - const widthPx = Math.min(120, Math.max(40, Math.round(pixelsPerCm))) - const text = `1:${scaleRatio}${this.currentScaleUnit}` - return { text, widthPx, niceMeters: widthMeters } + const rawMeters = metersPerPx * barPx + const niceMeters = this.niceScaleValue(rawMeters) + const widthPx = Math.round(niceMeters / metersPerPx) + const text = this.formatScaleText(niceMeters) + return { text, widthPx, niceMeters } } } // 最后备用:从 2D 相机视锥估算(正交宽度 -> 米) @@ -2954,11 +2953,11 @@ export default { const frustumWidth = camera.frustum.right - camera.frustum.left if (frustumWidth > 0) { const metersPerPx = frustumWidth / w - const scaleRatio = this.calculateScaleRatio(metersPerPx) - const pixelsPerCm = this.getPixelsPerCm() - const widthPx = Math.min(120, Math.max(40, Math.round(pixelsPerCm))) - const text = `1:${scaleRatio}${this.currentScaleUnit}` - return { text, widthPx, niceMeters: frustumWidth } + const rawMeters = metersPerPx * barPx + const niceMeters = this.niceScaleValue(rawMeters) + const widthPx = Math.round(niceMeters / metersPerPx) + const text = this.formatScaleText(niceMeters) + return { text, widthPx, niceMeters } } } return null @@ -2991,6 +2990,15 @@ export default { } return best }, + /** 格式化比例尺文本 */ + formatScaleText(meters) { + if (meters >= 1000) { + const kmValue = (meters / 1000).toFixed(0) + return `${kmValue}km` + } else { + return `${meters}m` + } + }, updateScaleBar() { if (this.useCustomScale && this.customScaleText) { this.scaleBarText = `${this.customScaleText}${this.currentScaleUnit}` diff --git a/ruoyi-ui/src/views/dialogs/ScaleDialog.vue b/ruoyi-ui/src/views/dialogs/ScaleDialog.vue index c4d64fb..b6650ba 100644 --- a/ruoyi-ui/src/views/dialogs/ScaleDialog.vue +++ b/ruoyi-ui/src/views/dialogs/ScaleDialog.vue @@ -60,8 +60,8 @@ export default { return { formData: { scaleNumerator: 1, - scaleDenominator: 1000, - unit: 'm' + scaleDenominator: 20, + unit: 'km' }, rules: { scaleNumerator: [ @@ -97,8 +97,8 @@ export default { initFormData() { this.formData = { scaleNumerator: this.scale.scaleNumerator || 1, - scaleDenominator: this.scale.scaleDenominator || 1000, - unit: this.scale.unit || 'm' + scaleDenominator: this.scale.scaleDenominator || 20, + unit: this.scale.unit || 'km' }; }, closeDialog() {