Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10521 - Update center comparison in OL map component #10525

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions web/client/components/map/openlayers/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,23 @@ class OpenlayersMap extends React.Component {
return new View(viewOptions);
};

isNearlyEqual = (a, b) => {
/**
* this implementation will update the map only if the movement
* between 8 decimals (coordinate precision in mm) in the reference system
* to avoid rounded value changes due to float mathematic operations or transformed value
*/
if (a === undefined || b === undefined) {
return false;
}
return a.toFixed(8) - b.toFixed(8) <= 0.00000001;
};

_updateMapPositionFromNewProps = (newProps) => {
var view = this.map.getView();
const currentCenter = this.props.center;
const centerIsUpdated = newProps.center.y === currentCenter.y &&
newProps.center.x === currentCenter.x;
const centerIsUpdated = this.isNearlyEqual(newProps.center.y, currentCenter.y) &&
this.isNearlyEqual(newProps.center.x, currentCenter.x);

if (!centerIsUpdated) {
// let center = ol.proj.transform([newProps.center.x, newProps.center.y], 'EPSG:4326', newProps.projection);
Expand Down
36 changes: 36 additions & 0 deletions web/client/components/map/openlayers/__tests__/Map-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1489,4 +1489,40 @@ describe('OpenlayersMap', () => {
expect(customHooRegister.getHook(MapUtils.ZOOM_TO_EXTENT_HOOK)).toBeTruthy();
});
});
it('update map position center based on nearly equal match when receiving new props', () => {
let map = ReactDOM.render(
<OpenlayersMap
center={{y: 43.9323233378, x: 10.3346776780}}
zoom={1}
projection="EPSG:4326"
measurement={{}}
/>
, document.getElementById("map"));
map = ReactDOM.render(
<OpenlayersMap
center={{y: 43.9323233388, x: 10.3346776790}}
zoom={1}
measurement={{}}
projection="EPSG:4326"
mapOptions={undefined}
/>
, document.getElementById("map")
);
expect(map).toBeTruthy();
// center is not modified
expect(map.map.getView().getCenter()).toEqual([10.3346776780, 43.9323233378]);

map = ReactDOM.render(
<OpenlayersMap
center={{y: 43.9323234388, x: 10.3346773790}}
zoom={1}
measurement={{}}
projection="EPSG:4326"
mapOptions={undefined}
/>
, document.getElementById("map")
);
// center is modified
expect(map.map.getView().getCenter()).toEqual([10.3346773790, 43.9323234388]);
});
});
Loading