Skip to content

Commit 3c18004

Browse files
ishafiuljosxha
andauthored
feat: contains method for LatLngBounds (#498)
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]>
1 parent 5a3daa2 commit 3c18004

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

maplibre_gl_platform_interface/lib/src/location.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ class LatLngBounds {
8484
return <dynamic>[southwest.toJson(), northeast.toJson()];
8585
}
8686

87+
/// Determines whether a given geographical point (`LatLng`) is within the
88+
/// bounds defined by two other geographical points: `southwest` (lower-left corner)
89+
/// and `northeast` (upper-right corner).
90+
///
91+
bool contains(LatLng point) {
92+
final isLatitudeInBounds = point.latitude >= southwest.latitude &&
93+
point.latitude <= northeast.latitude;
94+
95+
final bool isLongitudeInBounds;
96+
97+
if (southwest.longitude <= northeast.longitude) {
98+
isLongitudeInBounds = point.longitude >= southwest.longitude &&
99+
point.longitude <= northeast.longitude;
100+
} else {
101+
isLongitudeInBounds = point.longitude >= southwest.longitude ||
102+
point.longitude <= northeast.longitude;
103+
}
104+
return isLatitudeInBounds && isLongitudeInBounds;
105+
}
106+
87107
@visibleForTesting
88108
static LatLngBounds? fromList(dynamic json) {
89109
if (json == null) {

0 commit comments

Comments
 (0)