Fix handling of "Fit" zoom values in setHash (issue 5581)

Tentative patch to fix #5581.
I've refactored the code a bit to avoid this.