Skip to content

fitBounds not working correctly for large bounds #1069

@oliverheilig

Description

@oliverheilig

I'm experiencing issues with setting the map viewport with fitBounds when bounds is a large rectangle. I've looked into the code and it seems the fitBounds uses setView with L.latLngBounds(bounds).getCenter() as center. But this is not correct if the map uses a Mercator projection. If i want to set the map to a latitude range of (0, 85) your calculation uses 42.5 as center latitude. But in a mercator world the correct latitude is 66.5!

The correct calcuation would be:

  • project the LatLng bounds on the map projection bounds
  • calculate the center on the projected bounds
  • re-project the center on LatLng
  • use this point for setView

Don't know if this also affects other methods on the map.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions