openlayers icon indicating copy to clipboard operation
openlayers copied to clipboard

view.fit seems to not work consistently

Open JuxhinB opened this issue 2 years ago • 4 comments

Currently in a project I am working on we upgraded to v.9 from v.7.

We have a multi-tab page where you see some options then you can move to select areas in the map.

I make sure that after the map is visible and has loaded everything it needs , to fit my features in the view.

What happens is that 1 in 2 times the map seems to super-zoom, and is unable to understand the layer's extent.

To make things worst this bug is not present in dev but only occurs after project is deployed. I was left clueless for many days but for me it seems like something goes wrong during build.

Here are some packages from our project

 "dependencies": {
    ...
    "core-js": "^3.36.0",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-refresh": "^0.14.0",
    "react-router-dom": "6.22.3",
    "regenerator-runtime": "^0.14.1",
    "tslib": "^2.6.2"
  },
  "devDependencies": {
    ...
    "@babel/core": "^7.24.0",
    "@babel/preset-env": "^7.24.0",
    "@babel/preset-react": "^7.23.3",
    "@babel/preset-typescript": "^7.23.3",
    "@jest/globals": "29.7.0",
    "@nx/devkit": "18.0.8",
    "@nx/esbuild": "18.0.8",
    "@nx/eslint": "18.0.8",
    "@nx/eslint-plugin": "18.0.8",
    "@nx/jest": "18.0.8",
    "@nx/js": "18.0.8",
    "@nx/node": "18.0.8",
    "@nx/react": "18.0.8",
    "@nx/web": "18.0.8",
    "@nx/webpack": "18.0.8",
    "@nx/workspace": "18.0.8",
    "@pmmmwh/react-refresh-webpack-plugin": "^0.5.11",
    "@svgr/webpack": "8.0.1",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "14.0.0",
    "@testing-library/user-event": "^14.4.3",
    "@typescript-eslint/eslint-plugin": "7.2.0",
    "@typescript-eslint/parser": "7.2.0",
    "babel-jest": "29.7.0",
    "babel-loader": "^9.1.3",
    "esbuild": "0.19.12",
    "eslint": "8.57.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-airbnb-typescript": "^18.0.0",
    "eslint-config-prettier": "9.1.0",
    "eslint-import-resolver-typescript": "^3.6.1",
    "eslint-plugin-import": "2.29.1",
    "eslint-plugin-jsx-a11y": "6.8.0",
    "eslint-plugin-react": "7.34.0",
    "eslint-plugin-react-hooks": "4.6.0",
    "eslint-plugin-simple-import-sort": "^12.0.0",
    "http-proxy": "^1.18.1",
    "jest": "29.7.0",
    "jest-environment-jsdom": "^29.7.0",
    "nx": "18.0.8",
    "ts-jest": "29.1.2",
    "ts-node": "10.9.2",
    "typescript": "5.4.2",
    "url-loader": "^4.1.1"
  }

JuxhinB avatar Apr 17 '24 08:04 JuxhinB

Looks like you're using React. What you're reporting is the result of a common mistake, i.e. give the map a target which is not yet rendered or correctly sized by the time View#fit() is called. In your application, you either have to make sure the map's target has the desired size before calling View#fit(), or call View#fit() with the desired size as option, e.g.

view.fit(geometryOrExtent, {size: [800, 450]});

ahocevar avatar Apr 17 '24 08:04 ahocevar

I also noticed something weird related to this. Incorrect zooming happens when fit() is called immediately after initializing the map. But the target is definitely rendered and correctly sized.

What doesn't work in v9 (but worked in v7):

// console.log(target.getBoundingClientRect()); // prints correct dimensions
const map = new Map({ target });
map.getView().fit(extent); // zooms too far

But both of these work:

const mapRect = target.getBoundingClientRect();
const map = new Map({ target });
map.getView().fit(extent, {
    size: [mapRect.width, mapRect.height]
});
const map = new Map({ target });
setTimeout(() => map.getView().fit(extent));

But I can only reproduce this in our codebase, in a clean sandbox even the first one seems to work just fine.

janruo avatar Apr 18 '24 14:04 janruo

What if you use the recommended verbose syntax

map.getView().fit(extent, map.getSize());

?

ahocevar avatar Apr 18 '24 19:04 ahocevar

I found the reason, it's a bug on our side. We extend the default Map and among other things it contains:

class MyMap extends Map {

    _isResizable = true;

    constructor(options: MapOptions) {
        super(options);
    }

    override updateSize() {
        if (this._isResizable) {
            super.updateSize();
        }
    }
}

The _isResizable is there to be able to lock the size of the map. Apparently Map calls updateSize() at some point during its initialization, at which point this._isResizable is undefined, which blocks the map from knowing its own dimensions. So map.getSize() will also return undefined right after creating the map.

Changing the check to

if (this._isResizable !== false)

also fixes the issue.

janruo avatar Apr 19 '24 10:04 janruo