Skip to content

Commit

Permalink
fix: update zoom level when scroll zooming (origo-map#1730)
Browse files Browse the repository at this point in the history
* fix: update zoom level when scroll zooming

* Update maputils.js
  • Loading branch information
jokd committed Apr 3, 2023
1 parent f042087 commit 5dfe2e9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/controls/print/print-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ const PrintComponent = function PrintComponent(options = {}) {
let descriptionAlign = `text-align-${descriptionAlignment}`;
let viewerMapTarget;
let today = new Date(Date.now());
let initZoom;
let printScale = 0;
let widthImage = 0;
let heightImage = 0;
const originalResolutions = viewer.getResolutions().map(item => item);
const originalGrids = new Map();
const deviceOnIos = isOnIos();
const view = map.getView();

// eslint-disable-next-line no-underscore-dangle
const olPixelRatio = map.pixelRatio_;
Expand Down Expand Up @@ -341,8 +343,28 @@ const PrintComponent = function PrintComponent(options = {}) {
printToolbar.setDisabled(false);
}

function updateScaleOnMove() {
if (initZoom !== view.getZoom()) {
initZoom = view.getZoom();
const res = view.getResolution();
const projection = view.getProjection();
const scale = res * getPointResolution(
projection,
resolution / 25.4,
view.getCenter()
);
let textContent = scale;
textContent = maputils.formatScale(scale * 1000);
printSettings.getScaleControl().setButtonText(textContent);
setScale(scale);
}
}

return Component({
name: 'printComponent',
getResolution() {
return resolution;
},
onInit() {
this.on('render', this.onRender);
this.addComponent(printSettings);
Expand Down Expand Up @@ -469,6 +491,7 @@ const PrintComponent = function PrintComponent(options = {}) {
}
unByKey(mapLoadListenRefs[0]);
unByKey(mapLoadListenRefs[1]);
unByKey(mapLoadListenRefs[2]);
// GH-1537: remove layers temporarily added for print and unhide layers hidden for print
viewer.getLayersByProperty('added-for-print', true).forEach((l) => viewer.removeLayer(l));
viewer.getLayersByProperty('hidden-for-print', true).forEach((l) => {
Expand Down Expand Up @@ -571,7 +594,8 @@ const PrintComponent = function PrintComponent(options = {}) {
function addMapLoadListeners() {
const startEvRef = map.on('loadstart', disablePrintToolbar);
const endEvRef = map.on('loadend', enablePrintToolbar);
return [startEvRef, endEvRef];
const endMoveRef = map.on('moveend', updateScaleOnMove);
return [startEvRef, endEvRef, endMoveRef];
}
mapLoadListenRefs = addMapLoadListeners();
printScale = 0;
Expand Down
1 change: 1 addition & 0 deletions src/controls/print/print-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const PrintSettings = function PrintSettings(options = {}) {

return Component({
close,
getScaleControl() { return setScaleControl; },
onInit() {
openButton = Button({
cls: 'padding-small icon-smaller round light box-shadow',
Expand Down
3 changes: 3 additions & 0 deletions src/controls/print/set-scale-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export default function SetScaleControl(map, options = {}) {
<div class="padding-smaller o-tooltip active">
${selectScale.render()}
</div>`;
},
setButtonText(buttonText) {
selectScale.setButtonText(buttonText);
}
});
}
4 changes: 4 additions & 0 deletions src/maputils.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ const maputils = {
const resolution = scale / (mpu * 39.37 * dpi);
return resolution;
},
formatScale: function formatScale(scale) {
const roundedScale = Math.round(scale / 10) * 10;
return `1:${numberFormatter(roundedScale)}`;
},
roundScale: function roundScale(scale) {
let scaleValue = scale;
const differens = scaleValue % 10;
Expand Down

0 comments on commit 5dfe2e9

Please sign in to comment.