Skip to content

Enhance OL map component in updating map position based on center #10521

@dsuren1

Description

@dsuren1

Description

The OL Map component requires a minor enhancement in updating the map position from props, especially the center. Currently, an exact match is performed, leading to a map view update even when the difference is <= 0.00000001. This value is reprojected, causing a minor fractional difference, which in turn triggers onMapViewChange, updating the props and leading to a multiple update.

const centerIsUpdated = newProps.center.y === currentCenter.y &&
newProps.center.x === currentCenter.x;
if (!centerIsUpdated) {
// let center = ol.proj.transform([newProps.center.x, newProps.center.y], 'EPSG:4326', newProps.projection);
let center = reproject({ x: newProps.center.x, y: newProps.center.y }, 'EPSG:4326', newProps.projection, true);
view.setCenter([center.x, center.y]);

To avoid unnecessary updates, a nearly equal match is more suitable in these scenarios instead of performing an exact match.

  const isNearlyEqual = function (a, b) {
    if (a === undefined || b === undefined) {
      return false;
    }
    return a.toFixed(8) - b.toFixed(8) <= 0.00000001;
  };

What kind of improvement you want to add? (check one with "x", remove the others)

  • Minor changes to existing features

Other useful information

This update is needed for downstream project where a similar scenario is observed when using certain CRS

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions