Create RectangularBounds Based on Latlng With Radius

November 24, 2019
// in meter
val radius = 30.0
// optional: to get distance to circle radius, not the edge
val distance = radius * sqrt(2.0)

val center = LatLng(3.1412, 101.68653)

val ne = SphericalUtil.computeOffset(center, distance, 45.0)
val sw = SphericalUtil.computeOffset(center, distance, 225.0)

val bounds = RectangularBounds.newInstance(sw, ne)

Dependencies

dependencies {
    implementation 'com.google.maps.android:android-maps-utils:0.5'
}

Kotlin extensions

fun LatLng.toBounds(distance: Double): RectangularBounds {
    val center = this

    val ne = SphericalUtil.computeOffset(center, distance, 45.0)
    val sw = SphericalUtil.computeOffset(center, distance, 225.0)
    return RectangularBounds.newInstance(sw, ne)
}

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.