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

[BUG] LatLngBounds does not account for angular wrapping #1844

Open
pcba-dev opened this issue Mar 7, 2024 · 3 comments
Open

[BUG] LatLngBounds does not account for angular wrapping #1844

pcba-dev opened this issue Mar 7, 2024 · 3 comments
Labels
bug This issue reports broken functionality or another error needs triage This new bug report needs reproducing and prioritizing S: core Scoped to the core flutter_map functionality waiting for user response

Comments

@pcba-dev
Copy link

pcba-dev commented Mar 7, 2024

What is the bug?

I want check if some bounds are contained (LatLngBounds.contains) or overlapping (LatLngBounds.isOverlapping) another bound. In the case of bound boxes which wrap around the 180th meridian it fails since the current implementation does not account for it.

How can we reproduce it?

Define bounds that wrap around the 180th meridian, and check if they contain another bound that is indeed contained.

print(LatLngBounds(LatLng(0, 180), LatLng(45, -135)).containsBounds(LatLngBounds(LatLng(0, 180), LatLng(45, -150)))==true);
print(LatLngBounds(LatLng(0, 180), LatLng(45, -135)).containsBounds(LatLngBounds(LatLng(0, -165), LatLng(45, -135)))==true);
print(LatLngBounds(LatLng(0, 180), LatLng(45, -135)).containsBounds(LatLngBounds(LatLng(0, -165), LatLng(45, -150)))==true);

print(LatLngBounds(LatLng(0, 135), LatLng(45, -135)).containsBounds(LatLngBounds(LatLng(0, 150), LatLng(45, -135)))==true);
print(LatLngBounds(LatLng(0, 135), LatLng(45, -135)).containsBounds(LatLngBounds(LatLng(0, 180), LatLng(45, -135)))==true);
print(LatLngBounds(LatLng(0, 135), LatLng(45, -135)).containsBounds(LatLngBounds(LatLng(0, -165), LatLng(45, -135)))==true);

Do you have a potential solution?

    /// Flag indicating whether this bounds wrap around the 180th meridian. Longitudes are considered in the range (-180, 180].
  bool get wrapped => west >= 0 && east < 0;
 
  bool contains(final LatLongBounds other) {
    // First check if the other box is within the latitude bounds.
    if (other.south < south || other.north > north) {
      return false;
    }

    // Then check if the other box is within the longitude bounds considering the arc.
    final double eastVal = wrapped ? east   360 : east;
    final double otherWest = wrapped && other.west < 0 ? other.west   360 : other.west;
    final double otherEast = wrapped && other.east < 0 ? other.east   360 : other.east;
    return otherWest >= west &&
        otherWest <= eastVal &&
        otherEast >= west &&
        otherEast <= eastVal &&
        otherEast > otherWest;
  }
  
  bool overlaps(final LatLongBounds other) {
    // First check if the other box is within the latitude bounds.
    if (other.south > north || other.north < south) {
      return false;
    }

    // Wrap around if the range wraps around the 180th meridian.
    final double eastVal = wrapped ? east   360 : east;
    final double otherWest = wrapped && other.west < 0 ? other.west   360 : other.west;
    final double otherEast = wrapped && other.east < 0 ? other.east   360 : other.east;

    return (otherWest >= west && otherWest < eastVal) || (otherEast > west && otherEast <= eastVal);
  }

Platforms

any

Severity

Obtrusive: Prevents normal functioning but causes no errors in the console

@pcba-dev pcba-dev added bug This issue reports broken functionality or another error needs triage This new bug report needs reproducing and prioritizing labels Mar 7, 2024
@JaffaKetchup JaffaKetchup added P: 2 (soon™?) S: core Scoped to the core flutter_map functionality and removed needs triage This new bug report needs reproducing and prioritizing labels Mar 29, 2024
@JaffaKetchup
Copy link
Member

Related to #1689 / #1860 ?

@monsieurtanuki
Copy link
Contributor

In order to avoid regression, a solution would to add a named constructor like LatLngBounds.fromLTRB (cf. Rect) where the developer would be able to say "my left is 160 and my right is -140 so the middle is -170 (instead of 10)"

@JaffaKetchup
Copy link
Member

This needs re-triage after #1860 was merged.

@JaffaKetchup JaffaKetchup added needs triage This new bug report needs reproducing and prioritizing waiting for user response and removed P: 2 (soon™?) labels Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue reports broken functionality or another error needs triage This new bug report needs reproducing and prioritizing S: core Scoped to the core flutter_map functionality waiting for user response
Projects
Status: To do
Development

No branches or pull requests

3 participants