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

feat: contains method for LatLngBounds #498

Merged
merged 4 commits into from
Oct 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ||
josxha marked this conversation as resolved.
Show resolved Hide resolved
point.longitude <= northeast.longitude;
}
return isLatitudeInBounds && isLongitudeInBounds;
}
josxha marked this conversation as resolved.
Show resolved Hide resolved

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