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.

class GeoRectBounds(
southWest: GeoPoint? = null,
northEast: GeoPoint? = null
)
  • 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
  • center: GeoPoint?: Center point of the bounds
  • toSpan(): GeoPoint?: Returns the span (width/height) as a GeoPointInterface
// 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()

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")
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
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")
fun intersects(other: GeoRectBounds): Boolean
// Usage
val bounds1 = GeoRectBounds(/* ... */)
val bounds2 = GeoRectBounds(/* ... */)
val doIntersect = bounds1.intersects(bounds2)
println("Bounds intersect: $doIntersect")

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
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
// 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))
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)
fun equalsTo(other: GeoRectBounds): Boolean
// Usage
val bounds1 = GeoRectBounds(/* ... */)
val bounds2 = GeoRectBounds(/* ... */)
val areEqual = bounds1.equalsTo(bounds2)