コンテンツにスキップ

GeoRectBounds(矩形範囲)

GeoRectBounds は、南西と北東の角の点で定義される矩形の地理的エリアを表します。地図領域の定義、グラウンドイメージの境界、ビューポート計算に使用されます。

コンストラクタ

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

プロパティ

角の点

  • southWest: GeoPoint?: 矩形の南西角(左下)
  • northEast: GeoPoint?: 矩形の北東角(右上)
  • isEmpty: Boolean: 境界が定義されていない場合は true を返します

計算されるプロパティ

  • center: GeoPoint?: 境界の中心点
  • toSpan(): GeoPoint?: GeoPointInterface として範囲(幅/高さ)を返します

基本的な使用方法

境界の作成

// 矩形エリアを定義
val bounds = GeoRectBounds(
southWest = GeoPoint.fromLatLong(37.7649, -122.4294),
northeast = GeoPoint.fromLatLong(37.7849, -122.4094)
)
// 空の境界(後で拡張)
val bounds = GeoRectBounds()

動的な境界の構築

境界の拡張

境界は新しい点を含むように動的に拡張できます:

fun extend(point: GeoPointInterface)
// 使用例
val bounds = GeoRectBounds() // 空からスタート
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) // 各点を含むように境界が拡大
}
println("最終境界: $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) // 両方のエリアを含む

点と境界のテスト

点を含むかどうか

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 は国際日付変更線(経度 ±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))

URL 値

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
// 使用例
val bounds1 = GeoRectBounds(/* ... */)
val bounds2 = GeoRectBounds(/* ... */)
val areEqual = bounds1.equalsTo(bounds2)

バウンズへのカメラフィット

GeoRectBounds が画面内に収まるようにカメラを移動・ズームするには fitBounds を使用します:

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

padding パラメーターは全辺にスクリーンスペースのインセット(ピクセル単位)を追加します。ズームレベルの計算は各プロバイダーがネイティブに行うため、プロバイダー間で結果が若干異なる場合があります。

複数のポイントからバウンズを構築してフィットする典型的なパターン:

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