Skip to content

Commit

Permalink
feat: contains method for LatLngBounds (#498)
Browse files Browse the repository at this point in the history
Little utility I frequently used in my project, so I decided to create a
pull request to include it in the library, making it helpful to others.

---------

Co-authored-by: Joscha <[email protected]>
  • Loading branch information
ishafiul and josxha authored Oct 20, 2024
1 parent 5a3daa2 commit 3c18004
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions maplibre_gl_platform_interface/lib/src/location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ class LatLngBounds {
return <dynamic>[southwest.toJson(), northeast.toJson()];
}

/// Determines whether a given geographical point (`LatLng`) is within the
/// bounds defined by two other geographical points: `southwest` (lower-left corner)
/// and `northeast` (upper-right corner).
///
bool contains(LatLng point) {
final isLatitudeInBounds = point.latitude >= southwest.latitude &&
point.latitude <= northeast.latitude;

final bool isLongitudeInBounds;

if (southwest.longitude <= northeast.longitude) {
isLongitudeInBounds = point.longitude >= southwest.longitude &&
point.longitude <= northeast.longitude;
} else {
isLongitudeInBounds = point.longitude >= southwest.longitude ||
point.longitude <= northeast.longitude;
}
return isLatitudeInBounds && isLongitudeInBounds;
}

@visibleForTesting
static LatLngBounds? fromList(dynamic json) {
if (json == null) {
Expand Down

0 comments on commit 3c18004

Please sign in to comment.