Skip to content

GeoRectBounds

GeoRectBounds represents a rectangular geographic area defined by southwest and northeast corner points. It is used for defining map regions, ground image bounds, and viewport calculations.

Constructor

class GeoRectBounds(
southWest: GeoPoint? = null,
northEast: GeoPoint? = null
)

Properties

Corner Points

  • southWest: GeoPoint?: Southwest corner (bottom-left) of the rectangle
  • northEast: GeoPoint?: Northeast corner (top-right) of the rectangle
  • isEmpty: Boolean: Returns true if the bounds are not defined

Computed Properties

  • center: GeoPoint?: Center point of the bounds
  • toSpan(): GeoPoint?: Returns the span (width/height) as a GeoPointInterface

Basic Usage

Creating Bounds

// Define a rectangular area
val bounds = GeoRectBounds(
southWest = GeoPoint.fromLatLong(37.7649, -122.4294),
northeast = GeoPoint.fromLatLong(37.7849, -122.4094)
)
// Empty bounds (to be extended later)
val bounds = GeoRectBounds()

Dynamic Bounds Construction

Extending Bounds

The bounds can be dynamically extended to include new points:

fun extend(point: GeoPointInterface)
// Usage
val bounds = GeoRectBounds() // Start empty
val points = listOf(
GeoPoint.fromLatLong(37.7749, -122.4194),
GeoPoint.fromLatLong(37.7849, -122.4094),
GeoPoint.fromLatLong(37.7649, -122.4294)
)
points.forEach { point ->
bounds.extend(point) // Bounds grow to include each point
}
println("Final bounds: $bounds")

Union of Bounds

val bounds1 = GeoRectBounds(
southWest = GeoPoint.fromLatLong(37.7649, -122.4294),
northEast = GeoPoint.fromLatLong(37.7749, -122.4194)
)
val bounds2 = GeoRectBounds(
southWest = GeoPoint.fromLatLong(37.7749, -122.4194),
northEast = GeoPoint.fromLatLong(37.7849, -122.4094)
)
val combinedBounds = bounds1.union(bounds2) // Contains both areas

Point and Bounds Testing

Contains Point

fun contains(point: GeoPointInterface): Boolean
// Usage
val bounds = GeoRectBounds(
southWest = GeoPoint.fromLatLong(37.7649, -122.4294),
northEast = GeoPoint.fromLatLong(37.7849, -122.4094)
)
val testPoint = GeoPoint.fromLatLong(37.7749, -122.4194)
val isInside = bounds.contains(testPoint)
println("Point is inside bounds: $isInside")

Bounds Intersection

fun intersects(other: GeoRectBounds): Boolean
// Usage
val bounds1 = GeoRectBounds(/* ... */)
val bounds2 = GeoRectBounds(/* ... */)
val doIntersect = bounds1.intersects(bounds2)
println("Bounds intersect: $doIntersect")

Special Handling

International Date Line

GeoRectBounds handles rectangles that cross the International Date Line (longitude ±180°):

// Bounds crossing the date line
val pacificBounds = GeoRectBounds(
southWest = GeoPoint.fromLatLong(20, 170), // West of date line
northEast = GeoPoint.fromLatLong(40, -170) // East of date line
)
val pointInPacific = GeoPoint.fromLatLong(30, 175)
val containsPoint = pacificBounds.contains(pointInPacific) // true

Empty Bounds Handling

val emptyBounds = GeoRectBounds()
println(emptyBounds.isEmpty) // true
println(emptyBounds.center) // null
println(emptyBounds.toSpan()) // null
// Extending empty bounds
emptyBounds.extend(GeoPoint.fromLatLong(37.7749, -122.4194))
println(emptyBounds.isEmpty) // false

Utility Methods

String Representation

// For debugging
val bounds = GeoRectBounds(
southWest = GeoPoint.fromLatLong(37.7649, -122.4294),
northEast = GeoPoint.fromLatLong(37.7849, -122.4094)
)
println(bounds.toString())
// Output: ((37.7649, -122.4294), (37.7849, -122.4094))

URL Value

fun toUrlValue(precision: Int = 6): String
// Usage for API calls
val urlString = bounds.toUrlValue() // "37.764900,-122.429400,37.784900,-122.409400"
val preciseString = bounds.toUrlValue(precision = 8)

Equality Comparison

fun equalsTo(other: GeoRectBounds): Boolean
// Usage
val bounds1 = GeoRectBounds(/* ... */)
val bounds2 = GeoRectBounds(/* ... */)
val areEqual = bounds1.equalsTo(bounds2)

Fitting the Camera to Bounds

To move and zoom the camera so a GeoRectBounds is fully visible, use fitBounds:

val bounds = GeoRectBounds(
southWest = GeoPoint.fromLatLong(37.7649, -122.4294),
northEast = GeoPoint.fromLatLong(37.7849, -122.4094)
)
mapViewState.fitBounds(bounds, padding = 32)

The padding parameter adds screen-space inset in pixels on all sides. Each provider calculates the resulting zoom level natively, so the exact zoom may differ slightly between providers.

A common pattern is to build bounds from a set of points and then fit:

val bounds = GeoRectBounds()
markers.forEach { bounds.extend(it.position) }
if (!bounds.isEmpty) {
mapViewState.fitBounds(bounds, padding = 48)
}