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

fitBounds broken #5375

Open
wipfli opened this issue Jan 20, 2025 · 8 comments
Open

fitBounds broken #5375

wipfli opened this issue Jan 20, 2025 · 8 comments
Labels
need more info Further information is requested

Comments

@wipfli
Copy link
Contributor

wipfli commented Jan 20, 2025

maplibre-gl-js version: 4.7.0

browser: chrome on ubuntu

Link to Demonstration

Correct behavior in v4.6.0:

https://jsbin.com/rexiseyeve/1/edit?html,output

Broken behavior in v4.7.0 (zooms to wrong location):

https://jsbin.com/ninokegeto/edit?html,output

Expected Behavior

Should zoom to bounds

Actual Behavior

Zooms somewhere else, maybe the antimeridian?

Probably related to #4620

@HarelM
Copy link
Collaborator

HarelM commented Jan 20, 2025

Thanks for taking the time to report this issue!
I think the problem is that the "direction" of the bounding box is reversed, and so the entire planet is shown, which I tend to think is the right behavior to allow selecting the non-smallest area as part of the above PR.
Calling the following fitBounds will do the requested behavior I believe (note that I have changed the order of the longitude between the two coordinates):

map.fitBounds([
        [9.031869499999999, 46.8322365],
        [9.289097166666666, 47.19989433333333]
      ], {
        padding: 50,
        maxZoom: 14,
      });

@HarelM HarelM added the need more info Further information is requested label Jan 20, 2025
@wipfli
Copy link
Contributor Author

wipfli commented Jan 20, 2025

The docs says

A LngLatBounds object, an array of LngLatLike objects in [sw, ne] order, or an array of numbers in [west, south, east, north] order.

so I guess you are right. But the change in behavior is still a bit inconvenient when updating the MapLibre version. If I understand correctly the old behavior was give me two points and I will make the map show both. So not exactly bounds but very convenient...

@wipfli
Copy link
Contributor Author

wipfli commented Jan 20, 2025

If I take two points and put them in an order, they might have one of the following four meanings for bounding box corners

  • [sw, ne]
  • [ne, sw]
  • [nw, se]
  • [se, nw]

If only the first option should be given to fitBounds, how can I convert the other 3 to the first?

@wipfli
Copy link
Contributor Author

wipfli commented Jan 20, 2025

OK I can just do this myself with something like this:

const getBoundingBox = (point1, point2) => {
  const [lng1, lat1] = point1;
  const [lng2, lat2] = point2;

  const sw = [Math.min(lng1, lng2), Math.min(lat1, lat2)];
  const ne = [Math.max(lng1, lng2), Math.max(lat1, lat2)];

  return [sw, ne];
};

and then I call

map.fitBounds(getBoundingBox(point1, point2));

That gives me roughly the v4.6.0 behavior I think.

@HarelM
Copy link
Collaborator

HarelM commented Jan 20, 2025

Exactly.

@wipfli
Copy link
Contributor Author

wipfli commented Jan 20, 2025

Feel free to close this issue @HarelM. But maybe we can add a link to this workaround in the changelog. What do you think?

@HarelM
Copy link
Collaborator

HarelM commented Jan 20, 2025

I think adding some code example is a better approach, or adding an example in the examples with this code and linking to it.
Linking to a GitHub issue is less preferred from my point of view.
Closing... Feel free to submit a PR.

@wipfli
Copy link
Contributor Author

wipfli commented Jan 20, 2025

But then again the above function does not work across the antimeridian. So tricky...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need more info Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants